comparison test/testlock.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents 290b5baf2fca
children 4436464c4f51
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
17 17
18 /* 18 /*
19 * SDL_Quit() shouldn't be used with atexit() directly because 19 * SDL_Quit() shouldn't be used with atexit() directly because
20 * calling conventions may differ... 20 * calling conventions may differ...
21 */ 21 */
22 static void SDL_Quit_Wrapper(void) 22 static void
23 SDL_Quit_Wrapper(void)
23 { 24 {
24 SDL_Quit(); 25 SDL_Quit();
25 } 26 }
26 27
27 void printid(void) 28 void
29 printid(void)
28 { 30 {
29 printf("Process %u: exiting\n", SDL_ThreadID()); 31 printf("Process %u: exiting\n", SDL_ThreadID());
30 }
31
32 void terminate(int sig)
33 {
34 signal(SIGINT, terminate);
35 doterminate = 1;
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 }
47 int SDLCALL Run(void *data)
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);
66 if (SDL_ThreadID() == mainthread && doterminate) {
67 printf("Process %u: raising SIGTERM\n", SDL_ThreadID());
68 raise(SIGTERM);
69 }
70 }
71 return(0);
72 } 32 }
73 33
74 int main(int argc, char *argv[]) 34 void
35 terminate(int sig)
75 { 36 {
76 int i; 37 signal(SIGINT, terminate);
77 int maxproc = 6; 38 doterminate = 1;
39 }
78 40
79 /* Load the SDL library */ 41 void
80 if ( SDL_Init(0) < 0 ) { 42 closemutex(int sig)
81 fprintf(stderr, "%s\n", SDL_GetError()); 43 {
82 exit(1); 44 Uint32 id = SDL_ThreadID();
83 } 45 int i;
84 atexit(SDL_Quit_Wrapper); 46 printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id);
47 for (i = 0; i < 6; ++i)
48 SDL_KillThread(threads[i]);
49 SDL_DestroyMutex(mutex);
50 exit(sig);
51 }
85 52
86 if ( (mutex=SDL_CreateMutex()) == NULL ) { 53 int SDLCALL
87 fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError()); 54 Run(void *data)
88 exit(1); 55 {
89 } 56 if (SDL_ThreadID() == mainthread)
57 signal(SIGTERM, closemutex);
58 while (1) {
59 printf("Process %u ready to work\n", SDL_ThreadID());
60 if (SDL_mutexP(mutex) < 0) {
61 fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
62 exit(1);
63 }
64 printf("Process %u, working!\n", SDL_ThreadID());
65 SDL_Delay(1 * 1000);
66 printf("Process %u, done!\n", SDL_ThreadID());
67 if (SDL_mutexV(mutex) < 0) {
68 fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
69 exit(1);
70 }
71 /* If this sleep isn't done, then threads may starve */
72 SDL_Delay(10);
73 if (SDL_ThreadID() == mainthread && doterminate) {
74 printf("Process %u: raising SIGTERM\n", SDL_ThreadID());
75 raise(SIGTERM);
76 }
77 }
78 return (0);
79 }
90 80
91 mainthread = SDL_ThreadID(); 81 int
92 printf("Main thread: %u\n", mainthread); 82 main(int argc, char *argv[])
93 atexit(printid); 83 {
94 for ( i=0; i<maxproc; ++i ) { 84 int i;
95 if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL ) 85 int maxproc = 6;
96 fprintf(stderr, "Couldn't create thread!\n");
97 }
98 signal(SIGINT, terminate);
99 Run(NULL);
100 86
101 return(0); /* Never reached */ 87 /* Load the SDL library */
88 if (SDL_Init(0) < 0) {
89 fprintf(stderr, "%s\n", SDL_GetError());
90 exit(1);
91 }
92 atexit(SDL_Quit_Wrapper);
93
94 if ((mutex = SDL_CreateMutex()) == NULL) {
95 fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
96 exit(1);
97 }
98
99 mainthread = SDL_ThreadID();
100 printf("Main thread: %u\n", mainthread);
101 atexit(printid);
102 for (i = 0; i < maxproc; ++i) {
103 if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
104 fprintf(stderr, "Couldn't create thread!\n");
105 }
106 signal(SIGINT, terminate);
107 Run(NULL);
108
109 return (0); /* Never reached */
102 } 110 }