SDL  2.0
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_mutexmutex = NULL
static SDL_threadID mainthread
static SDL_Threadthreads [6]
static SDL_atomic_t doterminate

Function Documentation

void closemutex ( int  sig)

Definition at line 52 of file testlock.c.

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

Referenced by Run().

{
int i;
SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
for (i = 0; i < 6; ++i)
exit(sig);
}
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 */
/* Load the SDL library */
if (SDL_Init(0) < 0) {
exit(1);
}
if ((mutex = SDL_CreateMutex()) == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
exit(1);
}
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);
return (0); /* Never reached */
}
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());
}
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(), and SDL_WinRTInitNonXAMLApp().

{
signal(SIGTERM, closemutex);
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);
}
SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
raise(SIGTERM);
}
return (0);
}
static void SDL_Quit_Wrapper ( void  )
static

Definition at line 33 of file testlock.c.

References SDL_Quit.

Referenced by main().

{
}
void terminate ( int  sig)

Definition at line 45 of file testlock.c.

References SDL_AtomicSet.

Referenced by main().

{
signal(SIGINT, terminate);
}

Variable Documentation

SDL_atomic_t doterminate
static

Definition at line 26 of file testlock.c.

SDL_threadID mainthread
static

Definition at line 24 of file testlock.c.

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

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().

SDL_Thread* threads[6]
static

Definition at line 25 of file testlock.c.

Referenced by main().