Mercurial > sdl-ios-xcode
annotate test/testlock.c @ 3983:540466e900db SDL-1.2
Removed textrels from hermes code.
Partially fixes Bugzilla #418.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 27 Jun 2007 10:12:49 +0000 |
parents | 290b5baf2fca |
children | 782fd950bd46 c121d94672cb |
rev | line source |
---|---|
0 | 1 |
2 /* Test the thread and mutex locking functions | |
3 Also exercises the system's signal/thread interaction | |
4 */ | |
5 | |
6 #include <signal.h> | |
7 #include <stdio.h> | |
8 | |
9 #include "SDL.h" | |
10 #include "SDL_mutex.h" | |
11 #include "SDL_thread.h" | |
12 | |
13 static SDL_mutex *mutex = NULL; | |
14 static Uint32 mainthread; | |
15 static SDL_Thread *threads[6]; | |
1769 | 16 static volatile int doterminate = 0; |
0 | 17 |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
18 /* |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
19 * SDL_Quit() shouldn't be used with atexit() directly because |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
20 * calling conventions may differ... |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
21 */ |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
22 static void SDL_Quit_Wrapper(void) |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
23 { |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
24 SDL_Quit(); |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
25 } |
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
26 |
0 | 27 void printid(void) |
28 { | |
29 printf("Process %u: exiting\n", SDL_ThreadID()); | |
30 } | |
31 | |
32 void terminate(int sig) | |
33 { | |
1769 | 34 signal(SIGINT, terminate); |
35 doterminate = 1; | |
0 | 36 } |
37 void closemutex(int sig) | |
38 { | |
39 Uint32 id = SDL_ThreadID(); | |
40 int i; | |
41 printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id); | |
42 for ( i=0; i<6; ++i ) | |
43 SDL_KillThread(threads[i]); | |
44 SDL_DestroyMutex(mutex); | |
45 exit(sig); | |
46 } | |
1769 | 47 int SDLCALL Run(void *data) |
0 | 48 { |
49 if ( SDL_ThreadID() == mainthread ) | |
50 signal(SIGTERM, closemutex); | |
51 while ( 1 ) { | |
52 printf("Process %u ready to work\n", SDL_ThreadID()); | |
53 if ( SDL_mutexP(mutex) < 0 ) { | |
54 fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError()); | |
55 exit(1); | |
56 } | |
57 printf("Process %u, working!\n", SDL_ThreadID()); | |
58 SDL_Delay(1*1000); | |
59 printf("Process %u, done!\n", SDL_ThreadID()); | |
60 if ( SDL_mutexV(mutex) < 0 ) { | |
61 fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError()); | |
62 exit(1); | |
63 } | |
64 /* If this sleep isn't done, then threads may starve */ | |
65 SDL_Delay(10); | |
1769 | 66 if (SDL_ThreadID() == mainthread && doterminate) { |
67 printf("Process %u: raising SIGTERM\n", SDL_ThreadID()); | |
68 raise(SIGTERM); | |
69 } | |
0 | 70 } |
71 return(0); | |
72 } | |
73 | |
74 int main(int argc, char *argv[]) | |
75 { | |
76 int i; | |
77 int maxproc = 6; | |
78 | |
79 /* Load the SDL library */ | |
80 if ( SDL_Init(0) < 0 ) { | |
81 fprintf(stderr, "%s\n", SDL_GetError()); | |
82 exit(1); | |
83 } | |
1151
be9c9c8f6d53
Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
84 atexit(SDL_Quit_Wrapper); |
0 | 85 |
86 if ( (mutex=SDL_CreateMutex()) == NULL ) { | |
87 fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError()); | |
88 exit(1); | |
89 } | |
90 | |
91 mainthread = SDL_ThreadID(); | |
92 printf("Main thread: %u\n", mainthread); | |
93 atexit(printid); | |
94 for ( i=0; i<maxproc; ++i ) { | |
95 if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL ) | |
96 fprintf(stderr, "Couldn't create thread!\n"); | |
97 } | |
98 signal(SIGINT, terminate); | |
99 Run(NULL); | |
100 | |
101 return(0); /* Never reached */ | |
102 } |