18
#include <SDL3/SDL_main.h>
19
#include <SDL3/SDL_test.h>
21
static SDL_RWLock *rwlock = NULL;
22
static SDL_ThreadID mainthread;
23
static SDL_AtomicInt doterminate;
24
static int nb_threads = 6;
25
static SDL_Thread **threads;
26
static int worktime = 1000;
27
static int writerworktime = 100;
28
static int timeout = 10000;
29
static SDLTest_CommonState *state;
31
static void DoWork(const int workticks)
33
const SDL_ThreadID tid = SDL_GetCurrentThreadID();
34
const SDL_bool is_reader = tid != mainthread;
35
const char *typestr = is_reader ? "Reader" : "Writer";
37
SDL_Log("%s Thread %" SDL_PRIu64 ": ready to work\n", typestr, tid);
39
SDL_LockRWLockForReading(rwlock);
41
SDL_LockRWLockForWriting(rwlock);
44
SDL_Log("%s Thread %" SDL_PRIu64 ": start work!\n", typestr, tid);
46
SDL_Log("%s Thread %" SDL_PRIu64 ": work done!\n", typestr, tid);
47
SDL_UnlockRWLock(rwlock);
56
SDL_Log("Reader Thread %" SDL_PRIu64 ": starting up", SDL_GetCurrentThreadID());
57
while (!SDL_AtomicGet(&doterminate)) {
60
SDL_Log("Reader Thread %" SDL_PRIu64 ": exiting!\n", SDL_GetCurrentThreadID());
64
int main(int argc, char *argv[])
69
state = SDLTest_CommonCreateState(argv, 0);
74
SDL_AtomicSet(&doterminate, 0);
77
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
79
for (i = 1; i < argc;) {
82
consumed = SDLTest_CommonArg(state, i);
84
if (SDL_strcmp(argv[i], "--nbthreads") == 0) {
87
nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
88
if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
92
} else if (SDL_strcmp(argv[i], "--worktime") == 0) {
95
worktime = SDL_strtol(argv[i + 1], &endptr, 0);
96
if (endptr != argv[i + 1] && *endptr == '\0' && worktime > 0) {
100
} else if (SDL_strcmp(argv[i], "--writerworktime") == 0) {
103
writerworktime = SDL_strtol(argv[i + 1], &endptr, 0);
104
if (endptr != argv[i + 1] && *endptr == '\0' && writerworktime > 0) {
108
} else if (SDL_strcmp(argv[i], "--timeout") == 0) {
111
timeout = (Uint64) SDL_strtol(argv[i + 1], &endptr, 0);
112
if (endptr != argv[i + 1] && *endptr == '\0' && timeout > 0) {
119
static const char *options[] = {
122
"[--writerworktime ms]",
126
SDLTest_CommonLogUsage(state, argv[0], options);
133
threads = SDL_malloc(nb_threads * sizeof(SDL_Thread*));
136
if (SDL_Init(0) < 0) {
137
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
141
SDL_AtomicSet(&doterminate, 0);
143
rwlock = SDL_CreateRWLock();
145
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create rwlock: %s\n", SDL_GetError());
147
SDLTest_CommonDestroyState(state);
151
mainthread = SDL_GetCurrentThreadID();
152
SDL_Log("Writer thread: %" SDL_PRIu64 "\n", mainthread);
153
for (i = 0; i < nb_threads; ++i) {
155
(void)SDL_snprintf(name, sizeof(name), "Reader%d", i);
156
threads[i] = SDL_CreateThread(ReaderRun, name, NULL);
157
if (threads[i] == NULL) {
158
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create reader thread! %s\n", SDL_GetError());
162
while (!SDL_AtomicGet(&doterminate) && (SDL_GetTicks() < ((Uint64) timeout))) {
163
DoWork(writerworktime);
166
SDL_AtomicSet(&doterminate, 1);
167
SDL_Log("Waiting on reader threads to terminate...");
168
for (i = 0; i < nb_threads; ++i) {
169
SDL_WaitThread(threads[i], NULL);
172
SDL_Log("Reader threads have terminated, quitting!");
173
SDL_DestroyRWLock(rwlock);
175
SDLTest_CommonDestroyState(state);