2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple test of the SDL semaphore code */
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 17:25:39 -06:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#define NUM_THREADS 10
|
2020-12-23 14:36:02 -07:00
|
|
|
/* This value should be smaller than the maximum count of the */
|
|
|
|
/* semaphore implementation: */
|
2022-11-30 13:51:59 -07:00
|
|
|
#define NUM_OVERHEAD_OPS 10000
|
2020-12-23 14:36:02 -07:00
|
|
|
#define NUM_OVERHEAD_OPS_MULT 10
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-04-28 08:31:12 -06:00
|
|
|
static SDL_Semaphore *sem;
|
2023-03-08 03:40:07 -07:00
|
|
|
static int alive;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
typedef struct Thread_State
|
|
|
|
{
|
|
|
|
SDL_Thread *thread;
|
2020-12-23 14:35:35 -07:00
|
|
|
int number;
|
2020-12-23 14:36:02 -07:00
|
|
|
SDL_bool flag;
|
2020-12-23 14:35:35 -07:00
|
|
|
int loop_count;
|
|
|
|
int content_count;
|
|
|
|
} Thread_State;
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
static void log_usage(char *progname, SDLTest_CommonState *state) {
|
|
|
|
static const char *options[] = { "init_value", NULL };
|
|
|
|
SDLTest_CommonLogUsage(state, progname, options);
|
|
|
|
}
|
|
|
|
|
2020-12-23 14:35:35 -07:00
|
|
|
static void
|
|
|
|
killed(int sig)
|
|
|
|
{
|
|
|
|
alive = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int SDLCALL
|
|
|
|
ThreadFuncRealWorld(void *data)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2022-11-30 13:51:59 -07:00
|
|
|
Thread_State *state = (Thread_State *)data;
|
2015-06-21 09:33:46 -06:00
|
|
|
while (alive) {
|
2023-04-27 21:49:54 -06:00
|
|
|
SDL_WaitSemaphore(sem);
|
2022-10-09 23:31:14 -06:00
|
|
|
SDL_Log("Thread number %d has got the semaphore (value = %" SDL_PRIu32 ")!\n",
|
2023-04-27 21:49:54 -06:00
|
|
|
state->number, SDL_GetSemaphoreValue(sem));
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Delay(200);
|
2023-04-27 21:49:54 -06:00
|
|
|
SDL_PostSemaphore(sem);
|
2022-10-09 23:31:14 -06:00
|
|
|
SDL_Log("Thread number %d has released the semaphore (value = %" SDL_PRIu32 ")!\n",
|
2023-04-27 21:49:54 -06:00
|
|
|
state->number, SDL_GetSemaphoreValue(sem));
|
2020-12-23 14:35:35 -07:00
|
|
|
++state->loop_count;
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Delay(1); /* For the scheduler */
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2020-12-23 14:35:35 -07:00
|
|
|
SDL_Log("Thread number %d exiting.\n", state->number);
|
2015-06-21 09:33:46 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-11-30 13:51:59 -07:00
|
|
|
TestRealWorld(int init_sem)
|
|
|
|
{
|
|
|
|
Thread_State thread_states[NUM_THREADS] = { { 0 } };
|
2020-12-23 14:35:35 -07:00
|
|
|
int i;
|
|
|
|
int loop_count;
|
|
|
|
|
|
|
|
sem = SDL_CreateSemaphore(init_sem);
|
|
|
|
|
|
|
|
SDL_Log("Running %d threads, semaphore value = %d\n", NUM_THREADS,
|
2022-11-30 13:51:59 -07:00
|
|
|
init_sem);
|
2020-12-23 14:35:35 -07:00
|
|
|
alive = 1;
|
|
|
|
/* Create all the threads */
|
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
|
|
|
char name[64];
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Thread%u", (unsigned int)i);
|
2020-12-23 14:35:35 -07:00
|
|
|
thread_states[i].number = i;
|
2022-11-30 13:51:59 -07:00
|
|
|
thread_states[i].thread = SDL_CreateThread(ThreadFuncRealWorld, name, (void *)&thread_states[i]);
|
2020-12-23 14:35:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait 10 seconds */
|
|
|
|
SDL_Delay(10 * 1000);
|
|
|
|
|
|
|
|
/* Wait for all threads to finish */
|
|
|
|
SDL_Log("Waiting for threads to finish\n");
|
2015-06-21 09:33:46 -06:00
|
|
|
alive = 0;
|
2020-12-23 14:35:35 -07:00
|
|
|
loop_count = 0;
|
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
|
|
|
SDL_WaitThread(thread_states[i].thread, NULL);
|
|
|
|
loop_count += thread_states[i].loop_count;
|
|
|
|
}
|
|
|
|
SDL_Log("Finished waiting for threads, ran %d loops in total\n\n", loop_count);
|
|
|
|
|
|
|
|
SDL_DestroySemaphore(sem);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TestWaitTimeout(void)
|
|
|
|
{
|
2022-12-02 02:17:17 -07:00
|
|
|
Uint64 start_ticks;
|
|
|
|
Uint64 end_ticks;
|
|
|
|
Uint64 duration;
|
2015-06-21 09:33:46 -06:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
sem = SDL_CreateSemaphore(0);
|
|
|
|
SDL_Log("Waiting 2 seconds on semaphore\n");
|
|
|
|
|
|
|
|
start_ticks = SDL_GetTicks();
|
2023-04-27 21:49:54 -06:00
|
|
|
retval = SDL_WaitSemaphoreTimeout(sem, 2000);
|
2015-06-21 09:33:46 -06:00
|
|
|
end_ticks = SDL_GetTicks();
|
|
|
|
|
|
|
|
duration = end_ticks - start_ticks;
|
|
|
|
|
|
|
|
/* Accept a little offset in the effective wait */
|
2020-12-23 14:35:35 -07:00
|
|
|
SDL_assert(duration > 1900 && duration < 2050);
|
2022-12-02 02:17:17 -07:00
|
|
|
SDL_Log("Wait took %" SDL_PRIu64 " milliseconds\n\n", duration);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* Check to make sure the return value indicates timed out */
|
2022-11-27 09:38:43 -07:00
|
|
|
if (retval != SDL_MUTEX_TIMEDOUT) {
|
2023-04-27 21:49:54 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_WaitSemaphoreTimeout returned: %d; expected: %d\n\n", retval, SDL_MUTEX_TIMEDOUT);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2020-12-23 14:35:35 -07:00
|
|
|
|
|
|
|
SDL_DestroySemaphore(sem);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2020-12-23 14:36:02 -07:00
|
|
|
static void
|
|
|
|
TestOverheadUncontended(void)
|
|
|
|
{
|
2022-12-02 02:17:17 -07:00
|
|
|
Uint64 start_ticks;
|
|
|
|
Uint64 end_ticks;
|
|
|
|
Uint64 duration;
|
2020-12-23 14:36:02 -07:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
sem = SDL_CreateSemaphore(0);
|
|
|
|
SDL_Log("Doing %d uncontended Post/Wait operations on semaphore\n", NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT);
|
|
|
|
|
|
|
|
start_ticks = SDL_GetTicks();
|
2022-11-27 09:38:43 -07:00
|
|
|
for (i = 0; i < NUM_OVERHEAD_OPS_MULT; i++) {
|
2020-12-23 14:36:02 -07:00
|
|
|
for (j = 0; j < NUM_OVERHEAD_OPS; j++) {
|
2023-04-27 21:49:54 -06:00
|
|
|
SDL_PostSemaphore(sem);
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
for (j = 0; j < NUM_OVERHEAD_OPS; j++) {
|
2023-04-27 21:49:54 -06:00
|
|
|
SDL_WaitSemaphore(sem);
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end_ticks = SDL_GetTicks();
|
|
|
|
|
|
|
|
duration = end_ticks - start_ticks;
|
2022-12-02 02:17:17 -07:00
|
|
|
SDL_Log("Took %" SDL_PRIu64 " milliseconds\n\n", duration);
|
2020-12-23 14:36:02 -07:00
|
|
|
|
|
|
|
SDL_DestroySemaphore(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int SDLCALL
|
|
|
|
ThreadFuncOverheadContended(void *data)
|
|
|
|
{
|
2022-11-30 13:51:59 -07:00
|
|
|
Thread_State *state = (Thread_State *)data;
|
2020-12-23 14:36:02 -07:00
|
|
|
|
|
|
|
if (state->flag) {
|
2022-11-27 09:38:43 -07:00
|
|
|
while (alive) {
|
2023-04-27 21:49:54 -06:00
|
|
|
if (SDL_TryWaitSemaphore(sem) == SDL_MUTEX_TIMEDOUT) {
|
2020-12-23 14:36:02 -07:00
|
|
|
++state->content_count;
|
|
|
|
}
|
|
|
|
++state->loop_count;
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-27 09:38:43 -07:00
|
|
|
while (alive) {
|
2020-12-23 14:36:02 -07:00
|
|
|
/* Timeout needed to allow check on alive flag */
|
2023-04-27 21:49:54 -06:00
|
|
|
if (SDL_WaitSemaphoreTimeout(sem, 50) == SDL_MUTEX_TIMEDOUT) {
|
2020-12-23 14:36:02 -07:00
|
|
|
++state->content_count;
|
|
|
|
}
|
|
|
|
++state->loop_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TestOverheadContended(SDL_bool try_wait)
|
|
|
|
{
|
2022-12-02 02:17:17 -07:00
|
|
|
Uint64 start_ticks;
|
|
|
|
Uint64 end_ticks;
|
|
|
|
Uint64 duration;
|
2022-11-30 13:51:59 -07:00
|
|
|
Thread_State thread_states[NUM_THREADS] = { { 0 } };
|
2020-12-23 14:36:02 -07:00
|
|
|
char textBuffer[1024];
|
|
|
|
int loop_count;
|
|
|
|
int content_count;
|
|
|
|
int i, j;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
sem = SDL_CreateSemaphore(0);
|
|
|
|
SDL_Log("Doing %d contended %s operations on semaphore using %d threads\n",
|
|
|
|
NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT, try_wait ? "Post/TryWait" : "Post/WaitTimeout", NUM_THREADS);
|
|
|
|
alive = 1;
|
|
|
|
/* Create multiple threads to starve the semaphore and cause contention */
|
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
|
|
|
char name[64];
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Thread%u", (unsigned int)i);
|
2020-12-23 14:36:02 -07:00
|
|
|
thread_states[i].flag = try_wait;
|
2022-11-30 13:51:59 -07:00
|
|
|
thread_states[i].thread = SDL_CreateThread(ThreadFuncOverheadContended, name, (void *)&thread_states[i]);
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
start_ticks = SDL_GetTicks();
|
|
|
|
for (i = 0; i < NUM_OVERHEAD_OPS_MULT; i++) {
|
|
|
|
for (j = 0; j < NUM_OVERHEAD_OPS; j++) {
|
2023-04-27 21:49:54 -06:00
|
|
|
SDL_PostSemaphore(sem);
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
/* Make sure threads consumed everything */
|
2023-04-27 21:49:54 -06:00
|
|
|
while (SDL_GetSemaphoreValue(sem)) {
|
2022-12-11 12:27:16 -07:00
|
|
|
/* Friendlier with cooperative threading models */
|
|
|
|
SDL_DelayNS(1);
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
end_ticks = SDL_GetTicks();
|
|
|
|
|
|
|
|
alive = 0;
|
|
|
|
loop_count = 0;
|
|
|
|
content_count = 0;
|
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
|
|
|
SDL_WaitThread(thread_states[i].thread, NULL);
|
|
|
|
loop_count += thread_states[i].loop_count;
|
|
|
|
content_count += thread_states[i].content_count;
|
|
|
|
}
|
|
|
|
SDL_assert_release((loop_count - content_count) == NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT);
|
|
|
|
|
|
|
|
duration = end_ticks - start_ticks;
|
2022-12-02 02:17:17 -07:00
|
|
|
SDL_Log("Took %" SDL_PRIu64 " milliseconds, threads %s %d out of %d times in total (%.2f%%)\n",
|
2020-12-23 14:36:02 -07:00
|
|
|
duration, try_wait ? "where contended" : "timed out", content_count,
|
2022-11-30 13:51:59 -07:00
|
|
|
loop_count, ((float)content_count * 100) / loop_count);
|
2020-12-23 14:36:02 -07:00
|
|
|
/* Print how many semaphores where consumed per thread */
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(textBuffer, sizeof(textBuffer), "{ ");
|
2020-12-23 14:36:02 -07:00
|
|
|
for (i = 0; i < NUM_THREADS; ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
len = SDL_strlen(textBuffer);
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", ");
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
len = SDL_strlen(textBuffer);
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", thread_states[i].loop_count - thread_states[i].content_count);
|
2020-12-23 14:36:02 -07:00
|
|
|
}
|
|
|
|
len = SDL_strlen(textBuffer);
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n");
|
2020-12-23 14:36:02 -07:00
|
|
|
SDL_Log("%s\n", textBuffer);
|
|
|
|
|
|
|
|
SDL_DestroySemaphore(sem);
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char **argv)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2023-03-16 17:25:39 -06:00
|
|
|
int arg_count = 0;
|
|
|
|
int i;
|
2023-04-12 03:07:28 -06:00
|
|
|
int init_sem = 0;
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!state) {
|
2023-03-16 17:25:39 -06:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2015-11-25 13:39:28 -07:00
|
|
|
/* Enable standard application logging */
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (arg_count == 0) {
|
|
|
|
char *endptr;
|
|
|
|
init_sem = SDL_strtol(argv[i], &endptr, 0);
|
|
|
|
if (endptr != argv[i] && *endptr == '\0') {
|
|
|
|
arg_count++;
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
|
|
|
log_usage(argv[0], state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_count != 1) {
|
|
|
|
log_usage(argv[0], state);
|
2022-11-27 09:38:43 -07:00
|
|
|
return 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the SDL library */
|
|
|
|
if (SDL_Init(0) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2022-11-27 09:38:43 -07:00
|
|
|
return 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-12-01 14:07:03 -07:00
|
|
|
(void)signal(SIGTERM, killed);
|
|
|
|
(void)signal(SIGINT, killed);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2020-12-23 14:35:35 -07:00
|
|
|
if (init_sem > 0) {
|
|
|
|
TestRealWorld(init_sem);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
TestWaitTimeout();
|
|
|
|
|
2020-12-23 14:36:02 -07:00
|
|
|
TestOverheadUncontended();
|
|
|
|
|
|
|
|
TestOverheadContended(SDL_FALSE);
|
|
|
|
|
|
|
|
TestOverheadContended(SDL_TRUE);
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Quit();
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
|
2022-11-27 09:38:43 -07:00
|
|
|
return 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|