Use atomic variables for thread communication
Fixes https://github.com/libsdl-org/SDL/issues/10711
(cherry picked from commit a0f36fb85b
)
This commit is contained in:
parent
37d3eea939
commit
a75227aaeb
2 changed files with 12 additions and 11 deletions
|
@ -20,7 +20,7 @@
|
||||||
#include "SDL_test.h"
|
#include "SDL_test.h"
|
||||||
|
|
||||||
static SDL_TLSID tls;
|
static SDL_TLSID tls;
|
||||||
static int alive = 0;
|
static SDL_atomic_t alive;
|
||||||
static int testprio = 0;
|
static int testprio = 0;
|
||||||
static SDLTest_CommonState *state;
|
static SDLTest_CommonState *state;
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ ThreadFunc(void *data)
|
||||||
SDL_TLSSet(tls, "baby thread", NULL);
|
SDL_TLSSet(tls, "baby thread", NULL);
|
||||||
SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
|
SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
|
||||||
(char *)data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
|
(char *)data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
|
||||||
while (alive) {
|
while (SDL_AtomicGet(&alive)) {
|
||||||
SDL_Log("Thread '%s' is alive!\n", (char *)data);
|
SDL_Log("Thread '%s' is alive!\n", (char *)data);
|
||||||
|
|
||||||
if (testprio) {
|
if (testprio) {
|
||||||
|
@ -79,7 +79,7 @@ killed(int sig)
|
||||||
{
|
{
|
||||||
SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
|
SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
|
||||||
SDL_Delay(5 * 1000);
|
SDL_Delay(5 * 1000);
|
||||||
alive = 0;
|
SDL_AtomicSet(&alive, 0);
|
||||||
quit(0);
|
quit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ int main(int argc, char *argv[])
|
||||||
SDL_TLSSet(tls, "main thread", NULL);
|
SDL_TLSSet(tls, "main thread", NULL);
|
||||||
SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
|
SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
|
||||||
|
|
||||||
alive = 1;
|
SDL_AtomicSet(&alive, 1);
|
||||||
thread = SDL_CreateThread(ThreadFunc, "One", "#1");
|
thread = SDL_CreateThread(ThreadFunc, "One", "#1");
|
||||||
if (!thread) {
|
if (!thread) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
|
||||||
|
@ -141,12 +141,12 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
SDL_Delay(5 * 1000);
|
SDL_Delay(5 * 1000);
|
||||||
SDL_Log("Waiting for thread #1\n");
|
SDL_Log("Waiting for thread #1\n");
|
||||||
alive = 0;
|
SDL_AtomicSet(&alive, 0);
|
||||||
SDL_WaitThread(thread, NULL);
|
SDL_WaitThread(thread, NULL);
|
||||||
|
|
||||||
SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
|
SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
|
||||||
|
|
||||||
alive = 1;
|
SDL_AtomicSet(&alive, 1);
|
||||||
(void)signal(SIGTERM, killed);
|
(void)signal(SIGTERM, killed);
|
||||||
thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
|
thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
|
||||||
if (!thread) {
|
if (!thread) {
|
||||||
|
|
|
@ -34,8 +34,9 @@ quit(int rc)
|
||||||
int SDLCALL
|
int SDLCALL
|
||||||
SubThreadFunc(void *data)
|
SubThreadFunc(void *data)
|
||||||
{
|
{
|
||||||
while (!*(int volatile *)data) {
|
SDL_atomic_t *flag = (SDL_atomic_t *)data;
|
||||||
; /* SDL_Delay(10); */ /* do nothing */
|
while (!SDL_AtomicGet(flag)) {
|
||||||
|
SDL_Delay(10);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +45,7 @@ int SDLCALL
|
||||||
ThreadFunc(void *data)
|
ThreadFunc(void *data)
|
||||||
{
|
{
|
||||||
SDL_Thread *sub_threads[NUMTHREADS];
|
SDL_Thread *sub_threads[NUMTHREADS];
|
||||||
int flags[NUMTHREADS];
|
SDL_atomic_t flags[NUMTHREADS];
|
||||||
int i;
|
int i;
|
||||||
int tid = (int)(uintptr_t)data;
|
int tid = (int)(uintptr_t)data;
|
||||||
|
|
||||||
|
@ -53,7 +54,7 @@ ThreadFunc(void *data)
|
||||||
for (i = 0; i < NUMTHREADS; i++) {
|
for (i = 0; i < NUMTHREADS; i++) {
|
||||||
char name[64];
|
char name[64];
|
||||||
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
|
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
|
||||||
flags[i] = 0;
|
SDL_AtomicSet(&flags[i], 0);
|
||||||
sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
|
sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ ThreadFunc(void *data)
|
||||||
|
|
||||||
SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
|
SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
|
||||||
for (i = 0; i < NUMTHREADS; i++) {
|
for (i = 0; i < NUMTHREADS; i++) {
|
||||||
flags[i] = 1;
|
SDL_AtomicSet(&flags[i], 1);
|
||||||
SDL_WaitThread(sub_threads[i], NULL);
|
SDL_WaitThread(sub_threads[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue