Skip to content

Commit cc72153

Browse files
author
Philippe Simons
committed
rewrite atomic using a global mutex
1 parent d75a3bc commit cc72153

File tree

5 files changed

+128
-80
lines changed

5 files changed

+128
-80
lines changed

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ set ( LIB_VERSION_REVISION 1 )
4949
set ( LIB_VERSION_INFO
5050
"${LIB_VERSION_CURRENT}.${LIB_VERSION_AGE}.${LIB_VERSION_REVISION}" )
5151

52+
# the default C standard to use for all targets
53+
set(CMAKE_C_STANDARD 90)
54+
55+
# the default C++ standard to use for all targets
56+
set(CMAKE_CXX_STANDARD 98)
57+
58+
# whether to use gnu extensions
59+
set(CMAKE_C_EXTENSIONS ON)
60+
set(CMAKE_CXX_EXTENSIONS OFF)
61+
5262
include_directories (
5363
android
5464
${CMAKE_BINARY_DIR}
@@ -185,7 +195,6 @@ endif()
185195

186196
target_compile_options(libfluidsynth-OBJ
187197
PRIVATE
188-
-std=gnu11
189198
-Wall
190199
-DHAVE_CONFIG_H
191200
-Wno-unused-variable

src/utils/fluid_atomic.h

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/utils/fluid_sys.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,81 @@
3434
// Do not include pthread on windows. It includes winsock.h, which collides with ws2tcpip.h from fluid_sys.h
3535
// It isn't need on Windows anyway.
3636
#include <pthread.h>
37+
38+
static pthread_mutex_t g_atomic_lock = PTHREAD_MUTEX_INITIALIZER;
39+
40+
int
41+
pthread_atomic_int_get(const volatile int *atomic)
42+
{
43+
int value;
44+
45+
pthread_mutex_lock (&g_atomic_lock);
46+
value = *atomic;
47+
pthread_mutex_unlock (&g_atomic_lock);
48+
49+
return value;
50+
}
51+
52+
void
53+
pthread_atomic_int_inc(volatile int *atomic)
54+
{
55+
pthread_mutex_lock (&g_atomic_lock);
56+
(*atomic)++;
57+
pthread_mutex_unlock (&g_atomic_lock);
58+
}
59+
60+
void
61+
pthread_atomic_int_set(volatile int *atomic,
62+
int value)
63+
{
64+
pthread_mutex_lock (&g_atomic_lock);
65+
*atomic = value;
66+
pthread_mutex_unlock (&g_atomic_lock);
67+
}
68+
69+
bool
70+
pthread_atomic_int_dec_and_test (volatile int *atomic)
71+
{
72+
bool is_zero;
73+
74+
pthread_mutex_lock (&g_atomic_lock);
75+
is_zero = --(*atomic) == 0;
76+
pthread_mutex_unlock (&g_atomic_lock);
77+
78+
return is_zero;
79+
}
80+
81+
bool
82+
pthread_atomic_int_compare_and_exchange(volatile int *atomic,
83+
int oldval,
84+
int newval)
85+
{
86+
bool success;
87+
88+
pthread_mutex_lock (&g_atomic_lock);
89+
90+
if ((success = (*atomic == oldval)))
91+
*atomic = newval;
92+
93+
pthread_mutex_unlock (&g_atomic_lock);
94+
95+
return success;
96+
}
97+
98+
int
99+
pthread_atomic_int_add(volatile int *atomic,
100+
int val)
101+
{
102+
int oldval;
103+
104+
pthread_mutex_lock (&g_atomic_lock);
105+
oldval = *atomic;
106+
*atomic = oldval + val;
107+
pthread_mutex_unlock (&g_atomic_lock);
108+
109+
return oldval;
110+
}
111+
37112
#endif
38113

39114
/* WIN32 HACK - Flag used to differentiate between a file descriptor and a socket.

src/utils/fluid_sys.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ char* fluid_get_windows_error(void);
172172
#define FLUID_INT_TO_POINTER(x) ((void *)(intptr_t)(x))
173173

174174
/* Endian detection */
175+
#include <stdbool.h>
175176
#define FLUID_IS_BIG_ENDIAN false
176177

177178
#define FLUID_LE32TOH(x) le32toh(x)
@@ -342,7 +343,45 @@ int fluid_thread_join(fluid_thread_t *thread);
342343

343344
/* Atomic operations */
344345

345-
#include "fluid_atomic.h"
346+
#if HAVE_PTHREAD_H
347+
348+
int pthread_atomic_int_get(const volatile int *atomic);
349+
void pthread_atomic_int_inc(volatile int *atomic);
350+
void pthread_atomic_int_set(volatile int *atomic, int value);
351+
bool pthread_atomic_int_dec_and_test (volatile int *atomic);
352+
bool pthread_atomic_int_compare_and_exchange(volatile int *atomic, int oldval, int newval);
353+
int pthread_atomic_int_add(volatile int *atomic, int val);
354+
355+
#define fluid_atomic_int_inc(_pi) pthread_atomic_int_inc(_pi)
356+
#define fluid_atomic_int_get(_pi) pthread_atomic_int_get(_pi)
357+
#define fluid_atomic_int_set(_pi, _val) pthread_atomic_int_set(_pi, _val)
358+
#define fluid_atomic_int_dec_and_test(_pi) pthread_atomic_int_dec_and_test(_pi)
359+
#define fluid_atomic_int_compare_and_exchange(_pi, _old, _new) \
360+
pthread_atomic_int_compare_and_exchange(_pi, _old, _new)
361+
#define fluid_atomic_int_add(_pi, _add) \
362+
pthread_atomic_int_add(_pi, _add)
363+
#define fluid_atomic_int_exchange_and_add(_pi, _add) \
364+
pthread_atomic_int_add(_pi, _add)
365+
366+
static FLUID_INLINE void
367+
fluid_atomic_float_set(fluid_atomic_float_t *fptr, float val)
368+
{
369+
int32_t ival;
370+
memcpy(&ival, &val, 4);
371+
fluid_atomic_int_set((fluid_atomic_int_t *)fptr, ival);
372+
}
373+
374+
static FLUID_INLINE float
375+
fluid_atomic_float_get(fluid_atomic_float_t *fptr)
376+
{
377+
int32_t ival;
378+
float fval;
379+
ival = fluid_atomic_int_get((fluid_atomic_int_t *)fptr);
380+
memcpy(&fval, &ival, 4);
381+
return fval;
382+
}
383+
384+
#endif
346385

347386
/* Sockets and I/O */
348387

src/utils/fluidsynth_priv.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,9 @@ typedef double fluid_real_t;
7777

7878

7979
/** Atomic types */
80-
typedef struct {
81-
volatile int value;
82-
} fluid_atomic_int_t;
83-
typedef struct {
84-
volatile unsigned value;
85-
} fluid_atomic_uint_t;
86-
typedef struct {
87-
volatile float value;
88-
} fluid_atomic_float_t;
80+
typedef int fluid_atomic_int_t;
81+
typedef unsigned int fluid_atomic_uint_t;
82+
typedef float fluid_atomic_float_t;
8983

9084
/***************************************************************
9185
*

0 commit comments

Comments
 (0)