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 }

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 }

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 }

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 }

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, and SDL_AtomicSet.

Referenced by main().

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

terminate
void terminate(int sig)
Definition: testlock.c:45
SDL_CreateThread
#define SDL_CreateThread(fn, name, data)
Definition: SDL_thread.h:132
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
NULL
#define NULL
Definition: begin_code.h:167
mutex
static SDL_mutex * mutex
Definition: testlock.c:23
SDL_CreateMutex
#define SDL_CreateMutex
Definition: SDL_dynapi_overrides.h:259
threads
static SDL_Thread * threads[6]
Definition: testlock.c:25
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
doterminate
static SDL_atomic_t doterminate
Definition: testlock.c:26
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:663
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_Quit_Wrapper
static void SDL_Quit_Wrapper(void)
Definition: testlock.c:33
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_threadID
unsigned long SDL_threadID
Definition: SDL_thread.h:49
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_DestroyMutex
#define SDL_DestroyMutex
Definition: SDL_dynapi_overrides.h:263
mainthread
static SDL_threadID mainthread
Definition: testlock.c:24
Run
int Run(void *data)
Definition: testlock.c:65
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
SDL_AtomicGet
#define SDL_AtomicGet
Definition: SDL_dynapi_overrides.h:68
closemutex
void closemutex(int sig)
Definition: testlock.c:52
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
printid
void printid(void)
Definition: testlock.c:39
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_WaitThread
#define SDL_WaitThread
Definition: SDL_dynapi_overrides.h:478
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