Skip to content

Latest commit

 

History

History
128 lines (110 loc) · 3.35 KB

testlock.c

File metadata and controls

128 lines (110 loc) · 3.35 KB
 
Jan 2, 2017
Jan 2, 2017
2
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Test the thread and mutex locking functions
Also exercises the system's signal/thread interaction
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h> /* for atexit() */
#include "SDL.h"
static SDL_mutex *mutex = NULL;
static SDL_threadID mainthread;
static SDL_Thread *threads[6];
Jan 3, 2016
Jan 3, 2016
26
static SDL_atomic_t doterminate;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* SDL_Quit() shouldn't be used with atexit() directly because
* calling conventions may differ...
*/
static void
SDL_Quit_Wrapper(void)
{
SDL_Quit();
}
void
printid(void)
{
SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
}
void
terminate(int sig)
{
signal(SIGINT, terminate);
Jan 3, 2016
Jan 3, 2016
48
SDL_AtomicSet(&doterminate, 1);
49
50
51
52
53
54
55
56
}
void
closemutex(int sig)
{
SDL_threadID id = SDL_ThreadID();
int i;
SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
Jan 3, 2016
Jan 3, 2016
57
SDL_AtomicSet(&doterminate, 1);
58
59
60
61
62
63
64
65
66
67
68
for (i = 0; i < 6; ++i)
SDL_WaitThread(threads[i], NULL);
SDL_DestroyMutex(mutex);
exit(sig);
}
int SDLCALL
Run(void *data)
{
if (SDL_ThreadID() == mainthread)
signal(SIGTERM, closemutex);
Jan 3, 2016
Jan 3, 2016
69
while (!SDL_AtomicGet(&doterminate)) {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
}
Jan 3, 2016
Jan 3, 2016
85
if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
86
87
88
89
90
91
92
93
94
95
96
97
SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
raise(SIGTERM);
}
return (0);
}
int
main(int argc, char *argv[])
{
int i;
int maxproc = 6;
Nov 25, 2015
Nov 25, 2015
98
/* Enable standard application logging */
99
100
101
102
103
104
105
106
107
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);
Jan 3, 2016
Jan 3, 2016
108
109
SDL_AtomicSet(&doterminate, 0);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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 */
}