testlock.c File Reference
`#include <signal.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include "SDL.h"`
Include dependency graph for testlock.c:

Go to the source code of this file.

## Functions

static void | SDL_Quit_Wrapper (void)
void | printid (void)
void | terminate (int sig)
void | closemutex (int sig)
int | Run (void *data)
int | main (int argc, char *argv[])

## Variables

static SDL_mutex * | mutex = NULL
static SDL_threadID | mainthread
static SDL_Thread * | threads [6]
static SDL_atomic_t | doterminate

## Function Documentation

## closemutex()

void closemutex | ( | int | sig | ) |

Definition at line 52 of file testlock.c.

References i, mainthread, NULL, SDL_AtomicSet, SDL_DestroyMutex, SDL_Log, SDL_ThreadID, SDL_WaitThread, and SDLCALL.

Referenced by Run().

{
SDL_threadID id = SDL_ThreadID();
int i;
SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
SDL_AtomicSet(&doterminate, 1);
for (i = 0; i < 6; ++i)
SDL_WaitThread(threads[i], NULL);
SDL_DestroyMutex(mutex);
exit(sig);
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
mutex
static SDL_mutex * mutex
Definition: testlock.c:23
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
NULL
#define NULL
Definition: begin_code.h:164
threads
static SDL_Thread * threads[6]
Definition: testlock.c:25
SDL_DestroyMutex
#define SDL_DestroyMutex
Definition: SDL_dynapi_overrides.h:263
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
doterminate
static SDL_atomic_t doterminate
Definition: testlock.c:26
SDL_threadID
unsigned long SDL_threadID
Definition: SDL_thread.h:49
SDL_WaitThread
#define SDL_WaitThread
Definition: SDL_dynapi_overrides.h:478
mainthread
static SDL_threadID mainthread
Definition: testlock.c:24

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Definition at line 93 of file testlock.c.

References i, mainthread, NULL, printid(), Run(), SDL_AtomicSet, SDL_CreateMutex, SDL_CreateThread, SDL_GetError, SDL_Init, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_Quit_Wrapper(), SDL_snprintf, SDL_ThreadID, and terminate().

{
int i;
int maxproc = 6;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
/* Load the SDL library */
if (SDL_Init(0) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
exit(1);
}
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);
}
mainthread = SDL_ThreadID();
SDL_Log("Main thread: %lu\n", mainthread);
atexit(printid);
for (i = 0; i < maxproc; ++i) {
char name[64];
SDL_snprintf(name, sizeof (name), "Worker%d", i);
if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
}
signal(SIGINT, terminate);
Run(NULL);
return (0); /* Never reached */
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_CreateMutex
#define SDL_CreateMutex
Definition: SDL_dynapi_overrides.h:259
printid
void printid(void)
Definition: testlock.c:39
mutex
static SDL_mutex * mutex
Definition: testlock.c:23
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
Run
int Run(void *data)
Definition: testlock.c:65
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_CreateThread
#define SDL_CreateThread
Definition: SDL_dynapi_overrides.h:41
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
NULL
#define NULL
Definition: begin_code.h:164
threads
static SDL_Thread * threads[6]
Definition: testlock.c:25
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
terminate
void terminate(int sig)
Definition: testlock.c:45
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_Quit_Wrapper
static void SDL_Quit_Wrapper(void)
Definition: testlock.c:33
doterminate
static SDL_atomic_t doterminate
Definition: testlock.c:26
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
mainthread
static SDL_threadID mainthread
Definition: testlock.c:24

## printid()

void printid | ( | void |  | ) |

Definition at line 39 of file testlock.c.

References SDL_Log, and SDL_ThreadID.

Referenced by main().

{
SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31

## Run()

int Run | ( | void * | data | ) |

Definition at line 65 of file testlock.c.

References closemutex(), mainthread, SDL_AtomicGet, SDL_Delay, SDL_GetError, SDL_LockMutex, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_ThreadID, and SDL_UnlockMutex.

Referenced by main(), SDL_WinRTInitNonXAMLApp(), and WINRT_ProcessWindowSizeChange().

{
if (SDL_ThreadID() == mainthread)
signal(SIGTERM, closemutex);
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());
exit(1);
}
SDL_Log("Process %lu, working!\n", SDL_ThreadID());
SDL_Delay(1 * 1000);
SDL_Log("Process %lu, done!\n", SDL_ThreadID());
if (SDL_UnlockMutex(mutex) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
exit(1);
}
/* If this sleep isn't done, then threads may starve */
SDL_Delay(10);
}
if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
raise(SIGTERM);
}
return (0);
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
mutex
static SDL_mutex * mutex
Definition: testlock.c:23
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
closemutex
void closemutex(int sig)
Definition: testlock.c:52
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_AtomicGet
#define SDL_AtomicGet
Definition: SDL_dynapi_overrides.h:68
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
doterminate
static SDL_atomic_t doterminate
Definition: testlock.c:26
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
mainthread
static SDL_threadID mainthread
Definition: testlock.c:24

## SDL_Quit_Wrapper()

static void SDL_Quit_Wrapper | ( | void |  | ) |

static

Definition at line 33 of file testlock.c.

References SDL_Quit.

Referenced by main().

{
SDL_Quit();
}
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58

## terminate()

void terminate | ( | int | sig | ) |

Definition at line 45 of file testlock.c.

References SDL_AtomicSet.

Referenced by main().

{
signal(SIGINT, terminate);
SDL_AtomicSet(&doterminate, 1);
}
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
terminate
void terminate(int sig)
Definition: testlock.c:45
doterminate
static SDL_atomic_t doterminate
Definition: testlock.c:26

## Variable Documentation

## doterminate

SDL_atomic_t doterminate

static

Definition at line 26 of file testlock.c.

## mainthread

SDL_threadID mainthread

static

Definition at line 24 of file testlock.c.

Referenced by closemutex(), main(), and Run().

## mutex

SDL_mutex* mutex = NULL

static

Definition at line 23 of file testlock.c.

Referenced by SDL_CreateMutex(), SDL_DestroyMutex(), SDL_Generic_GetTLSData(), and SDL_ThreadID().

## threads

SDL_Thread* threads[6]

static

Definition at line 25 of file testlock.c.

Referenced by main().