mirror of
https://github.com/encounter/SDL.git
synced 2025-12-08 13:15:10 +00:00
Remove almost all instances of "volatile" keyword.
As Tiffany pointed out in Bugzilla, volatile is not useful for thread safety: https://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/ Some of these volatiles didn't need to be, some were otherwise protected by spinlocks or mutexes, and some got moved over to SDL_atomic_t data, etc. Fixes Bugzilla #3220.
This commit is contained in:
@@ -284,7 +284,7 @@ typedef struct
|
||||
char cache_pad4[SDL_CACHELINE_SIZE-sizeof(SDL_SpinLock)-2*sizeof(SDL_atomic_t)];
|
||||
#endif
|
||||
|
||||
volatile SDL_bool active;
|
||||
SDL_atomic_t active;
|
||||
|
||||
/* Only needed for the mutex test */
|
||||
SDL_mutex *mutex;
|
||||
@@ -305,7 +305,7 @@ static void InitEventQueue(SDL_EventQueue *queue)
|
||||
SDL_AtomicSet(&queue->rwcount, 0);
|
||||
SDL_AtomicSet(&queue->watcher, 0);
|
||||
#endif
|
||||
queue->active = SDL_TRUE;
|
||||
SDL_AtomicSet(&queue->active, 1);
|
||||
}
|
||||
|
||||
static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *event)
|
||||
@@ -538,7 +538,7 @@ static int FIFO_Reader(void* _data)
|
||||
if (DequeueEvent_LockFree(queue, &event)) {
|
||||
WriterData *writer = (WriterData*)event.user.data1;
|
||||
++data->counters[writer->index];
|
||||
} else if (queue->active) {
|
||||
} else if (SDL_AtomicGet(&queue->active)) {
|
||||
++data->waits;
|
||||
SDL_Delay(0);
|
||||
} else {
|
||||
@@ -551,7 +551,7 @@ static int FIFO_Reader(void* _data)
|
||||
if (DequeueEvent_Mutex(queue, &event)) {
|
||||
WriterData *writer = (WriterData*)event.user.data1;
|
||||
++data->counters[writer->index];
|
||||
} else if (queue->active) {
|
||||
} else if (SDL_AtomicGet(&queue->active)) {
|
||||
++data->waits;
|
||||
SDL_Delay(0);
|
||||
} else {
|
||||
@@ -571,7 +571,7 @@ static int FIFO_Watcher(void* _data)
|
||||
{
|
||||
SDL_EventQueue *queue = (SDL_EventQueue *)_data;
|
||||
|
||||
while (queue->active) {
|
||||
while (SDL_AtomicGet(&queue->active)) {
|
||||
SDL_AtomicLock(&queue->lock);
|
||||
SDL_AtomicIncRef(&queue->watcher);
|
||||
while (SDL_AtomicGet(&queue->rwcount) > 0) {
|
||||
@@ -652,7 +652,7 @@ static void RunFIFOTest(SDL_bool lock_free)
|
||||
}
|
||||
|
||||
/* Shut down the queue so readers exit */
|
||||
queue.active = SDL_FALSE;
|
||||
SDL_AtomicSet(&queue.active, 0);
|
||||
|
||||
/* Wait for the readers */
|
||||
while (SDL_AtomicGet(&readersRunning) > 0) {
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
static SDL_mutex *mutex = NULL;
|
||||
static SDL_threadID mainthread;
|
||||
static SDL_Thread *threads[6];
|
||||
static volatile int doterminate = 0;
|
||||
static SDL_atomic_t doterminate;
|
||||
|
||||
/*
|
||||
* SDL_Quit() shouldn't be used with atexit() directly because
|
||||
@@ -45,7 +45,7 @@ void
|
||||
terminate(int sig)
|
||||
{
|
||||
signal(SIGINT, terminate);
|
||||
doterminate = 1;
|
||||
SDL_AtomicSet(&doterminate, 1);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -54,7 +54,7 @@ closemutex(int sig)
|
||||
SDL_threadID id = SDL_ThreadID();
|
||||
int i;
|
||||
SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
|
||||
doterminate = 1;
|
||||
SDL_AtomicSet(&doterminate, 1);
|
||||
for (i = 0; i < 6; ++i)
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
SDL_DestroyMutex(mutex);
|
||||
@@ -66,7 +66,7 @@ Run(void *data)
|
||||
{
|
||||
if (SDL_ThreadID() == mainthread)
|
||||
signal(SIGTERM, closemutex);
|
||||
while (!doterminate) {
|
||||
while (!SDL_AtomicGet(&doterminate)) {
|
||||
SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
|
||||
if (SDL_LockMutex(mutex) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
|
||||
@@ -82,7 +82,7 @@ Run(void *data)
|
||||
/* If this sleep isn't done, then threads may starve */
|
||||
SDL_Delay(10);
|
||||
}
|
||||
if (SDL_ThreadID() == mainthread && doterminate) {
|
||||
if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
|
||||
SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
|
||||
raise(SIGTERM);
|
||||
}
|
||||
@@ -105,6 +105,8 @@ main(int argc, char *argv[])
|
||||
}
|
||||
atexit(SDL_Quit_Wrapper);
|
||||
|
||||
SDL_AtomicSet(&doterminate, 0);
|
||||
|
||||
if ((mutex = SDL_CreateMutex()) == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
|
||||
@@ -25,7 +25,7 @@ typedef struct
|
||||
{
|
||||
SDL_AudioDeviceID dev;
|
||||
int soundpos;
|
||||
volatile int done;
|
||||
SDL_atomic_t done;
|
||||
} callback_data;
|
||||
|
||||
callback_data cbd[64];
|
||||
@@ -46,14 +46,14 @@ play_through_once(void *arg, Uint8 * stream, int len)
|
||||
if (len > 0) {
|
||||
stream += cpy;
|
||||
SDL_memset(stream, spec.silence, len);
|
||||
cbd->done++;
|
||||
SDL_AtomicSet(&cbd->done, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
loop()
|
||||
{
|
||||
if(cbd[0].done) {
|
||||
if (SDL_AtomicGet(&cbd[0].done)) {
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_cancel_main_loop();
|
||||
#endif
|
||||
@@ -100,8 +100,7 @@ test_multi_audio(int devcount)
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_set_main_loop(loop, 0, 1);
|
||||
#else
|
||||
while (!cbd[0].done)
|
||||
{
|
||||
while (!SDL_AtomicGet(&cbd[0].done)) {
|
||||
#ifdef __ANDROID__
|
||||
/* Empty queue, some application events would prevent pause. */
|
||||
while (SDL_PollEvent(&event)){}
|
||||
@@ -136,7 +135,7 @@ test_multi_audio(int devcount)
|
||||
while (keep_going) {
|
||||
keep_going = 0;
|
||||
for (i = 0; i < devcount; i++) {
|
||||
if ((cbd[i].dev) && (!cbd[i].done)) {
|
||||
if ((cbd[i].dev) && (!SDL_AtomicGet(&cbd[i].done))) {
|
||||
keep_going = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#define NUMTHREADS 10
|
||||
|
||||
static char volatile time_for_threads_to_die[NUMTHREADS];
|
||||
static SDL_atomic_t time_for_threads_to_die[NUMTHREADS];
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
static void
|
||||
@@ -58,7 +58,7 @@ ThreadFunc(void *data)
|
||||
}
|
||||
|
||||
SDL_Log("Thread '%d' waiting for signal\n", tid);
|
||||
while (time_for_threads_to_die[tid] != 1) {
|
||||
while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
|
||||
; /* do nothing */
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ main(int argc, char *argv[])
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
char name[64];
|
||||
SDL_snprintf(name, sizeof (name), "Parent%d", i);
|
||||
time_for_threads_to_die[i] = 0;
|
||||
SDL_AtomicSet(&time_for_threads_to_die[i], 0);
|
||||
threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
|
||||
|
||||
if (threads[i] == NULL) {
|
||||
@@ -102,7 +102,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
time_for_threads_to_die[i] = 1;
|
||||
SDL_AtomicSet(&time_for_threads_to_die[i], 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
|
||||
Reference in New Issue
Block a user