24 |
|
#include <string.h> |
25 |
|
|
26 |
|
#include <SDL.h> |
27 |
– |
#include <SDL_mutex.h> |
27 |
|
|
28 |
|
#include "sysdeps.h" |
29 |
|
#include "main.h" |
137 |
|
|
138 |
|
|
139 |
|
/* |
141 |
– |
* Atomic operations |
142 |
– |
*/ |
143 |
– |
|
144 |
– |
#if HAVE_SPINLOCKS |
145 |
– |
static spinlock_t atomic_ops_lock = SPIN_LOCK_UNLOCKED; |
146 |
– |
#else |
147 |
– |
#define spin_lock(LOCK) |
148 |
– |
#define spin_unlock(LOCK) |
149 |
– |
#endif |
150 |
– |
|
151 |
– |
int atomic_add(int *var, int v) |
152 |
– |
{ |
153 |
– |
spin_lock(&atomic_ops_lock); |
154 |
– |
int ret = *var; |
155 |
– |
*var += v; |
156 |
– |
spin_unlock(&atomic_ops_lock); |
157 |
– |
return ret; |
158 |
– |
} |
159 |
– |
|
160 |
– |
int atomic_and(int *var, int v) |
161 |
– |
{ |
162 |
– |
spin_lock(&atomic_ops_lock); |
163 |
– |
int ret = *var; |
164 |
– |
*var &= v; |
165 |
– |
spin_unlock(&atomic_ops_lock); |
166 |
– |
return ret; |
167 |
– |
} |
168 |
– |
|
169 |
– |
int atomic_or(int *var, int v) |
170 |
– |
{ |
171 |
– |
spin_lock(&atomic_ops_lock); |
172 |
– |
int ret = *var; |
173 |
– |
*var |= v; |
174 |
– |
spin_unlock(&atomic_ops_lock); |
175 |
– |
return ret; |
176 |
– |
} |
177 |
– |
|
178 |
– |
|
179 |
– |
/* |
140 |
|
* Memory management helpers |
141 |
|
*/ |
142 |
|
|
835 |
|
*/ |
836 |
|
|
837 |
|
struct B2_mutex { |
838 |
< |
B2_mutex() { m = SDL_CreateMutex(); } |
879 |
< |
~B2_mutex() { if (m) SDL_DestroyMutex(m); } |
880 |
< |
SDL_mutex *m; |
838 |
> |
mutex_t m; |
839 |
|
}; |
840 |
|
|
841 |
|
B2_mutex *B2_create_mutex(void) |
845 |
|
|
846 |
|
void B2_lock_mutex(B2_mutex *mutex) |
847 |
|
{ |
848 |
< |
if (mutex) |
891 |
< |
SDL_LockMutex(mutex->m); |
848 |
> |
mutex->m.lock(); |
849 |
|
} |
850 |
|
|
851 |
|
void B2_unlock_mutex(B2_mutex *mutex) |
852 |
|
{ |
853 |
< |
if (mutex) |
897 |
< |
SDL_UnlockMutex(mutex->m); |
853 |
> |
mutex->m.unlock(); |
854 |
|
} |
855 |
|
|
856 |
|
void B2_delete_mutex(B2_mutex *mutex) |
864 |
|
*/ |
865 |
|
|
866 |
|
volatile uint32 InterruptFlags = 0; |
867 |
+ |
static mutex_t intflags_mutex; |
868 |
|
|
869 |
|
void SetInterruptFlag(uint32 flag) |
870 |
|
{ |
871 |
< |
atomic_or((int *)&InterruptFlags, flag); |
871 |
> |
intflags_mutex.lock(); |
872 |
> |
InterruptFlags |= flag; |
873 |
> |
intflags_mutex.unlock(); |
874 |
|
} |
875 |
|
|
876 |
|
void ClearInterruptFlag(uint32 flag) |
877 |
|
{ |
878 |
< |
atomic_and((int *)&InterruptFlags, ~flag); |
878 |
> |
intflags_mutex.lock(); |
879 |
> |
InterruptFlags &= ~flag; |
880 |
> |
intflags_mutex.unlock(); |
881 |
|
} |
882 |
|
|
883 |
|
|