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

◆ closemutex()

void closemutex ( int  sig)

Definition at line 52 of file testlock.c.

53{
55 int i;
56 SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
58 for (i = 0; i < 6; ++i)
61 exit(sig);
62}
#define SDL_AtomicSet
#define SDL_ThreadID
#define SDL_WaitThread
#define SDL_DestroyMutex
#define SDL_Log
unsigned long SDL_threadID
Definition: SDL_thread.h:49
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
#define NULL
Definition: begin_code.h:167
static SDL_threadID mainthread
Definition: testlock.c:24
static SDL_mutex * mutex
Definition: testlock.c:23
static SDL_Thread * threads[6]
Definition: testlock.c:25
static SDL_atomic_t doterminate
Definition: testlock.c:26

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

Referenced by Run().

◆ main()

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

Definition at line 93 of file testlock.c.

94{
95 int i;
96 int maxproc = 6;
97
98 /* Enable standard application logging */
100
101 /* Load the SDL library */
102 if (SDL_Init(0) < 0) {
104 exit(1);
105 }
106 atexit(SDL_Quit_Wrapper);
107
109
110 if ((mutex = SDL_CreateMutex()) == NULL) {
111 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
112 exit(1);
113 }
114
116 SDL_Log("Main thread: %lu\n", mainthread);
117 atexit(printid);
118 for (i = 0; i < maxproc; ++i) {
119 char name[64];
120 SDL_snprintf(name, sizeof (name), "Worker%d", i);
121 if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
122 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
123 }
124 signal(SIGINT, terminate);
125 Run(NULL);
126
127 return (0); /* Never reached */
128}
#define SDL_GetError
#define SDL_CreateThread
#define SDL_CreateMutex
#define SDL_LogSetPriority
#define SDL_LogError
#define SDL_Init
#define SDL_snprintf
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLuint const GLchar * name
static void SDL_Quit_Wrapper(void)
Definition: testlock.c:33
int Run(void *data)
Definition: testlock.c:65
void terminate(int sig)
Definition: testlock.c:45
void printid(void)
Definition: testlock.c:39

References doterminate, i, mainthread, mutex, 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, terminate(), and threads.

◆ printid()

void printid ( void  )

Definition at line 39 of file testlock.c.

40{
41 SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
42}

References SDL_Log, and SDL_ThreadID.

Referenced by main().

◆ Run()

int Run ( void data)

Definition at line 65 of file testlock.c.

66{
67 if (SDL_ThreadID() == mainthread)
68 signal(SIGTERM, closemutex);
69 while (!SDL_AtomicGet(&doterminate)) {
70 SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
71 if (SDL_LockMutex(mutex) < 0) {
72 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
73 exit(1);
74 }
75 SDL_Log("Process %lu, working!\n", SDL_ThreadID());
76 SDL_Delay(1 * 1000);
77 SDL_Log("Process %lu, done!\n", SDL_ThreadID());
78 if (SDL_UnlockMutex(mutex) < 0) {
79 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
80 exit(1);
81 }
82 /* If this sleep isn't done, then threads may starve */
83 SDL_Delay(10);
84 }
86 SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
87 raise(SIGTERM);
88 }
89 return (0);
90}
#define SDL_LockMutex
#define SDL_Delay
#define SDL_AtomicGet
#define SDL_UnlockMutex
void closemutex(int sig)
Definition: testlock.c:52

References closemutex(), doterminate, mainthread, mutex, 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().

◆ SDL_Quit_Wrapper()

static void SDL_Quit_Wrapper ( void  )
static

Definition at line 33 of file testlock.c.

34{
35 SDL_Quit();
36}
#define SDL_Quit

References SDL_Quit.

Referenced by main().

◆ terminate()

void terminate ( int  sig)

Definition at line 45 of file testlock.c.

46{
47 signal(SIGINT, terminate);
49}

References doterminate, SDL_AtomicSet, and terminate().

Referenced by main(), and terminate().

Variable Documentation

◆ doterminate

SDL_atomic_t doterminate
static

Definition at line 26 of file testlock.c.

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

◆ mainthread

SDL_threadID mainthread
static

Definition at line 24 of file testlock.c.

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

◆ mutex

◆ threads

SDL_Thread* threads[6]
static

Definition at line 25 of file testlock.c.

Referenced by closemutex(), and main().