Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
110 lines (96 loc) · 2.48 KB

testlock.c

File metadata and controls

110 lines (96 loc) · 2.48 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Test the thread and mutex locking functions
Also exercises the system's signal/thread interaction
*/
#include <signal.h>
#include <stdio.h>
#include "SDL.h"
#include "SDL_mutex.h"
#include "SDL_thread.h"
static SDL_mutex *mutex = NULL;
static Uint32 mainthread;
static SDL_Thread *threads[6];
May 17, 2006
May 17, 2006
16
static volatile int doterminate = 0;
Apr 26, 2001
Apr 26, 2001
17
Sep 28, 2005
Sep 28, 2005
18
19
20
21
/*
* SDL_Quit() shouldn't be used with atexit() directly because
* calling conventions may differ...
*/
May 28, 2006
May 28, 2006
22
static void
May 29, 2006
May 29, 2006
23
SDL_Quit_Wrapper(void)
Sep 28, 2005
Sep 28, 2005
24
{
May 29, 2006
May 29, 2006
25
SDL_Quit();
Sep 28, 2005
Sep 28, 2005
26
27
}
May 28, 2006
May 28, 2006
28
void
May 29, 2006
May 29, 2006
29
printid(void)
Apr 26, 2001
Apr 26, 2001
30
{
May 29, 2006
May 29, 2006
31
printf("Process %u: exiting\n", SDL_ThreadID());
Apr 26, 2001
Apr 26, 2001
32
}
May 28, 2006
May 28, 2006
33
34
void
May 29, 2006
May 29, 2006
35
terminate(int sig)
Apr 26, 2001
Apr 26, 2001
36
{
May 29, 2006
May 29, 2006
37
signal(SIGINT, terminate);
May 28, 2006
May 28, 2006
38
doterminate = 1;
Apr 26, 2001
Apr 26, 2001
39
}
May 28, 2006
May 28, 2006
40
41
void
May 29, 2006
May 29, 2006
42
closemutex(int sig)
Apr 26, 2001
Apr 26, 2001
43
{
May 29, 2006
May 29, 2006
44
Uint32 id = SDL_ThreadID();
May 28, 2006
May 28, 2006
45
int i;
May 29, 2006
May 29, 2006
46
printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id);
May 28, 2006
May 28, 2006
47
for (i = 0; i < 6; ++i)
May 29, 2006
May 29, 2006
48
49
50
SDL_KillThread(threads[i]);
SDL_DestroyMutex(mutex);
exit(sig);
Apr 26, 2001
Apr 26, 2001
51
}
May 28, 2006
May 28, 2006
52
53
int SDLCALL
May 29, 2006
May 29, 2006
54
Run(void *data)
Apr 26, 2001
Apr 26, 2001
55
{
May 29, 2006
May 29, 2006
56
57
if (SDL_ThreadID() == mainthread)
signal(SIGTERM, closemutex);
May 28, 2006
May 28, 2006
58
while (1) {
May 29, 2006
May 29, 2006
59
60
61
62
printf("Process %u ready to work\n", SDL_ThreadID());
if (SDL_mutexP(mutex) < 0) {
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
exit(1);
May 28, 2006
May 28, 2006
63
}
May 29, 2006
May 29, 2006
64
65
66
67
68
69
printf("Process %u, working!\n", SDL_ThreadID());
SDL_Delay(1 * 1000);
printf("Process %u, done!\n", SDL_ThreadID());
if (SDL_mutexV(mutex) < 0) {
fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
exit(1);
May 28, 2006
May 28, 2006
70
71
}
/* If this sleep isn't done, then threads may starve */
May 29, 2006
May 29, 2006
72
73
74
75
SDL_Delay(10);
if (SDL_ThreadID() == mainthread && doterminate) {
printf("Process %u: raising SIGTERM\n", SDL_ThreadID());
raise(SIGTERM);
May 28, 2006
May 28, 2006
76
77
78
}
}
return (0);
Apr 26, 2001
Apr 26, 2001
79
80
}
May 28, 2006
May 28, 2006
81
int
May 29, 2006
May 29, 2006
82
main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
83
{
May 28, 2006
May 28, 2006
84
85
int i;
int maxproc = 6;
Apr 26, 2001
Apr 26, 2001
86
May 28, 2006
May 28, 2006
87
/* Load the SDL library */
May 29, 2006
May 29, 2006
88
89
90
if (SDL_Init(0) < 0) {
fprintf(stderr, "%s\n", SDL_GetError());
exit(1);
May 28, 2006
May 28, 2006
91
}
May 29, 2006
May 29, 2006
92
atexit(SDL_Quit_Wrapper);
Apr 26, 2001
Apr 26, 2001
93
May 29, 2006
May 29, 2006
94
95
96
if ((mutex = SDL_CreateMutex()) == NULL) {
fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
exit(1);
May 28, 2006
May 28, 2006
97
}
Apr 26, 2001
Apr 26, 2001
98
May 29, 2006
May 29, 2006
99
100
101
mainthread = SDL_ThreadID();
printf("Main thread: %u\n", mainthread);
atexit(printid);
May 28, 2006
May 28, 2006
102
for (i = 0; i < maxproc; ++i) {
May 29, 2006
May 29, 2006
103
104
if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
fprintf(stderr, "Couldn't create thread!\n");
May 28, 2006
May 28, 2006
105
}
May 29, 2006
May 29, 2006
106
107
signal(SIGINT, terminate);
Run(NULL);
Apr 26, 2001
Apr 26, 2001
108
May 28, 2006
May 28, 2006
109
return (0); /* Never reached */
Apr 26, 2001
Apr 26, 2001
110
}