| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Simple test of the SDL semaphore code */ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <signal.h> |
| 18 |
|
| 19 |
#include "SDL.h" |
| 20 |
|
| 21 |
#define NUM_THREADS 10 |
| 22 |
/* This value should be smaller than the maximum count of the */ |
| 23 |
/* semaphore implementation: */ |
| 24 |
#define NUM_OVERHEAD_OPS 10000 |
| 25 |
#define NUM_OVERHEAD_OPS_MULT 10 |
| 26 |
|
| 27 |
static SDL_sem *sem; |
| 28 |
int alive; |
| 29 |
|
| 30 |
typedef struct Thread_State |
| 31 |
{ |
| 32 |
SDL_Thread *thread; |
| 33 |
int number; |
| 34 |
SDL_bool flag; |
| 35 |
int loop_count; |
| 36 |
int content_count; |
| 37 |
} Thread_State; |
| 38 |
|
| 39 |
static void |
| 40 |
killed(int sig) |
| 41 |
{ |
| 42 |
alive = 0; |
| 43 |
} |
| 44 |
|
| 45 |
static int SDLCALL |
| 46 |
ThreadFuncRealWorld(void *data) |
| 47 |
{ |
| 48 |
Thread_State *state = (Thread_State *)data; |
| 49 |
while (alive) { |
| 50 |
SDL_SemWait(sem); |
| 51 |
SDL_Log("Thread number %d has got the semaphore (value = %" SDL_PRIu32 ")!\n", |
| 52 |
state->number, SDL_SemValue(sem)); |
| 53 |
SDL_Delay(200); |
| 54 |
SDL_SemPost(sem); |
| 55 |
SDL_Log("Thread number %d has released the semaphore (value = %" SDL_PRIu32 ")!\n", |
| 56 |
state->number, SDL_SemValue(sem)); |
| 57 |
++state->loop_count; |
| 58 |
SDL_Delay(1); /* For the scheduler */ |
| 59 |
} |
| 60 |
SDL_Log("Thread number %d exiting.\n", state->number); |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
TestRealWorld(int init_sem) |
| 66 |
{ |
| 67 |
Thread_State thread_states[NUM_THREADS] = { { 0 } }; |
| 68 |
int i; |
| 69 |
int loop_count; |
| 70 |
|
| 71 |
sem = SDL_CreateSemaphore(init_sem); |
| 72 |
|
| 73 |
SDL_Log("Running %d threads, semaphore value = %d\n", NUM_THREADS, |
| 74 |
init_sem); |
| 75 |
alive = 1; |
| 76 |
/* Create all the threads */ |
| 77 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 78 |
char name[64]; |
| 79 |
(void)SDL_snprintf(name, sizeof(name), "Thread%u", (unsigned int)i); |
| 80 |
thread_states[i].number = i; |
| 81 |
thread_states[i].thread = SDL_CreateThread(ThreadFuncRealWorld, name, (void *)&thread_states[i]); |
| 82 |
} |
| 83 |
|
| 84 |
/* Wait 10 seconds */ |
| 85 |
SDL_Delay(10 * 1000); |
| 86 |
|
| 87 |
/* Wait for all threads to finish */ |
| 88 |
SDL_Log("Waiting for threads to finish\n"); |
| 89 |
alive = 0; |
| 90 |
loop_count = 0; |
| 91 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 92 |
SDL_WaitThread(thread_states[i].thread, NULL); |
| 93 |
loop_count += thread_states[i].loop_count; |
| 94 |
} |
| 95 |
SDL_Log("Finished waiting for threads, ran %d loops in total\n\n", loop_count); |
| 96 |
|
| 97 |
SDL_DestroySemaphore(sem); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
TestWaitTimeout(void) |
| 102 |
{ |
| 103 |
Uint32 start_ticks; |
| 104 |
Uint32 end_ticks; |
| 105 |
Uint32 duration; |
| 106 |
int retval; |
| 107 |
|
| 108 |
sem = SDL_CreateSemaphore(0); |
| 109 |
SDL_Log("Waiting 2 seconds on semaphore\n"); |
| 110 |
|
| 111 |
start_ticks = SDL_GetTicks(); |
| 112 |
retval = SDL_SemWaitTimeout(sem, 2000); |
| 113 |
end_ticks = SDL_GetTicks(); |
| 114 |
|
| 115 |
duration = end_ticks - start_ticks; |
| 116 |
|
| 117 |
/* Accept a little offset in the effective wait */ |
| 118 |
SDL_assert(duration > 1900 && duration < 2050); |
| 119 |
SDL_Log("Wait took %" SDL_PRIu32 " milliseconds\n\n", duration); |
| 120 |
|
| 121 |
/* Check to make sure the return value indicates timed out */ |
| 122 |
if (retval != SDL_MUTEX_TIMEDOUT) { |
| 123 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SemWaitTimeout returned: %d; expected: %d\n\n", retval, SDL_MUTEX_TIMEDOUT); |
| 124 |
} |
| 125 |
|
| 126 |
SDL_DestroySemaphore(sem); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
TestOverheadUncontended(void) |
| 131 |
{ |
| 132 |
Uint32 start_ticks; |
| 133 |
Uint32 end_ticks; |
| 134 |
Uint32 duration; |
| 135 |
int i, j; |
| 136 |
|
| 137 |
sem = SDL_CreateSemaphore(0); |
| 138 |
SDL_Log("Doing %d uncontended Post/Wait operations on semaphore\n", NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT); |
| 139 |
|
| 140 |
start_ticks = SDL_GetTicks(); |
| 141 |
for (i = 0; i < NUM_OVERHEAD_OPS_MULT; i++) { |
| 142 |
for (j = 0; j < NUM_OVERHEAD_OPS; j++) { |
| 143 |
SDL_SemPost(sem); |
| 144 |
} |
| 145 |
for (j = 0; j < NUM_OVERHEAD_OPS; j++) { |
| 146 |
SDL_SemWait(sem); |
| 147 |
} |
| 148 |
} |
| 149 |
end_ticks = SDL_GetTicks(); |
| 150 |
|
| 151 |
duration = end_ticks - start_ticks; |
| 152 |
SDL_Log("Took %" SDL_PRIu32 " milliseconds\n\n", duration); |
| 153 |
|
| 154 |
SDL_DestroySemaphore(sem); |
| 155 |
} |
| 156 |
|
| 157 |
static int SDLCALL |
| 158 |
ThreadFuncOverheadContended(void *data) |
| 159 |
{ |
| 160 |
Thread_State *state = (Thread_State *)data; |
| 161 |
|
| 162 |
if (state->flag) { |
| 163 |
while (alive) { |
| 164 |
if (SDL_SemTryWait(sem) == SDL_MUTEX_TIMEDOUT) { |
| 165 |
++state->content_count; |
| 166 |
} |
| 167 |
++state->loop_count; |
| 168 |
} |
| 169 |
} else { |
| 170 |
while (alive) { |
| 171 |
/* Timeout needed to allow check on alive flag */ |
| 172 |
if (SDL_SemWaitTimeout(sem, 50) == SDL_MUTEX_TIMEDOUT) { |
| 173 |
++state->content_count; |
| 174 |
} |
| 175 |
++state->loop_count; |
| 176 |
} |
| 177 |
} |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
TestOverheadContended(SDL_bool try_wait) |
| 183 |
{ |
| 184 |
Uint32 start_ticks; |
| 185 |
Uint32 end_ticks; |
| 186 |
Uint32 duration; |
| 187 |
Thread_State thread_states[NUM_THREADS] = { { 0 } }; |
| 188 |
char textBuffer[1024]; |
| 189 |
int loop_count; |
| 190 |
int content_count; |
| 191 |
int i, j; |
| 192 |
size_t len; |
| 193 |
|
| 194 |
sem = SDL_CreateSemaphore(0); |
| 195 |
SDL_Log("Doing %d contended %s operations on semaphore using %d threads\n", |
| 196 |
NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT, try_wait ? "Post/TryWait" : "Post/WaitTimeout", NUM_THREADS); |
| 197 |
alive = 1; |
| 198 |
/* Create multiple threads to starve the semaphore and cause contention */ |
| 199 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 200 |
char name[64]; |
| 201 |
(void)SDL_snprintf(name, sizeof(name), "Thread%u", (unsigned int)i); |
| 202 |
thread_states[i].flag = try_wait; |
| 203 |
thread_states[i].thread = SDL_CreateThread(ThreadFuncOverheadContended, name, (void *)&thread_states[i]); |
| 204 |
} |
| 205 |
|
| 206 |
start_ticks = SDL_GetTicks(); |
| 207 |
for (i = 0; i < NUM_OVERHEAD_OPS_MULT; i++) { |
| 208 |
for (j = 0; j < NUM_OVERHEAD_OPS; j++) { |
| 209 |
SDL_SemPost(sem); |
| 210 |
} |
| 211 |
/* Make sure threads consumed everything */ |
| 212 |
while (SDL_SemValue(sem)) { |
| 213 |
/* Friendlier with cooperative threading models */ |
| 214 |
SDL_Delay(1); |
| 215 |
} |
| 216 |
} |
| 217 |
end_ticks = SDL_GetTicks(); |
| 218 |
|
| 219 |
alive = 0; |
| 220 |
loop_count = 0; |
| 221 |
content_count = 0; |
| 222 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 223 |
SDL_WaitThread(thread_states[i].thread, NULL); |
| 224 |
loop_count += thread_states[i].loop_count; |
| 225 |
content_count += thread_states[i].content_count; |
| 226 |
} |
| 227 |
SDL_assert_release((loop_count - content_count) == NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT); |
| 228 |
|
| 229 |
duration = end_ticks - start_ticks; |
| 230 |
SDL_Log("Took %" SDL_PRIu32 " milliseconds, threads %s %d out of %d times in total (%.2f%%)\n", |
| 231 |
duration, try_wait ? "where contended" : "timed out", content_count, |
| 232 |
loop_count, ((float)content_count * 100) / loop_count); |
| 233 |
/* Print how many semaphores where consumed per thread */ |
| 234 |
(void)SDL_snprintf(textBuffer, sizeof(textBuffer), "{ "); |
| 235 |
for (i = 0; i < NUM_THREADS; ++i) { |
| 236 |
if (i > 0) { |
| 237 |
len = SDL_strlen(textBuffer); |
| 238 |
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", "); |
| 239 |
} |
| 240 |
len = SDL_strlen(textBuffer); |
| 241 |
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", thread_states[i].loop_count - thread_states[i].content_count); |
| 242 |
} |
| 243 |
len = SDL_strlen(textBuffer); |
| 244 |
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n"); |
| 245 |
SDL_Log("%s\n", textBuffer); |
| 246 |
|
| 247 |
SDL_DestroySemaphore(sem); |
| 248 |
} |
| 249 |
|
| 250 |
int main(int argc, char **argv) |
| 251 |
{ |
| 252 |
int init_sem; |
| 253 |
|
| 254 |
/* Enable standard application logging */ |
| 255 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 256 |
|
| 257 |
if (argc < 2) { |
| 258 |
SDL_Log("Usage: %s init_value\n", argv[0]); |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
/* Load the SDL library */ |
| 263 |
if (SDL_Init(0) < 0) { |
| 264 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 265 |
return 1; |
| 266 |
} |
| 267 |
(void)signal(SIGTERM, killed); |
| 268 |
(void)signal(SIGINT, killed); |
| 269 |
|
| 270 |
init_sem = SDL_atoi(argv[1]); |
| 271 |
if (init_sem > 0) { |
| 272 |
TestRealWorld(init_sem); |
| 273 |
} |
| 274 |
|
| 275 |
TestWaitTimeout(); |
| 276 |
|
| 277 |
TestOverheadUncontended(); |
| 278 |
|
| 279 |
TestOverheadContended(SDL_FALSE); |
| 280 |
|
| 281 |
TestOverheadContended(SDL_TRUE); |
| 282 |
|
| 283 |
SDL_Quit(); |
| 284 |
return 0; |
| 285 |
} |