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.

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

Referenced by Run().

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_ThreadID
static SDL_mutex * mutex
Definition: testlock.c:23
#define SDL_Log
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:164
static SDL_Thread * threads[6]
Definition: testlock.c:25
#define SDL_DestroyMutex
#define SDL_AtomicSet
static SDL_atomic_t doterminate
Definition: testlock.c:26
unsigned long SDL_threadID
Definition: SDL_thread.h:49
#define SDL_WaitThread
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().

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_ThreadID
#define SDL_GetError
#define SDL_CreateMutex
void printid(void)
Definition: testlock.c:39
static SDL_mutex * mutex
Definition: testlock.c:23
GLuint const GLchar * name
#define SDL_LogError
#define SDL_Log
int Run(void *data)
Definition: testlock.c:65
#define SDL_CreateThread
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 SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
static SDL_Thread * threads[6]
Definition: testlock.c:25
#define SDL_AtomicSet
void terminate(int sig)
Definition: testlock.c:45
#define SDL_snprintf
#define SDL_Init
static void SDL_Quit_Wrapper(void)
Definition: testlock.c:33
static SDL_atomic_t doterminate
Definition: testlock.c:26
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().

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

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

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_ThreadID
#define SDL_LockMutex
#define SDL_GetError
static SDL_mutex * mutex
Definition: testlock.c:23
#define SDL_LogError
#define SDL_Log
void closemutex(int sig)
Definition: testlock.c:52
#define SDL_Delay
#define SDL_AtomicGet
#define SDL_UnlockMutex
static SDL_atomic_t doterminate
Definition: testlock.c:26
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().

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

◆ terminate()

void terminate ( int  sig)

Definition at line 45 of file testlock.c.

References SDL_AtomicSet.

Referenced by main().

46 {
47  signal(SIGINT, terminate);
49 }
#define SDL_AtomicSet
void terminate(int sig)
Definition: testlock.c:45
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().