Mercurial > sdl-ios-xcode
comparison test/testlock.c @ 3578:0d1b16ee0bca
Fixed bug #741
The thread ID is an unsigned long so it can hold pthread_t so people can do naughty things with it.
I'm going to be adding additional useful thread API functions, but this should prevent crashes in people's existing code on 64-bit architectures.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 16 Dec 2009 04:48:11 +0000 |
parents | 4436464c4f51 |
children |
comparison
equal
deleted
inserted
replaced
3577:72024425b437 | 3578:0d1b16ee0bca |
---|---|
9 #include "SDL.h" | 9 #include "SDL.h" |
10 #include "SDL_mutex.h" | 10 #include "SDL_mutex.h" |
11 #include "SDL_thread.h" | 11 #include "SDL_thread.h" |
12 | 12 |
13 static SDL_mutex *mutex = NULL; | 13 static SDL_mutex *mutex = NULL; |
14 static Uint32 mainthread; | 14 static SDL_threadID mainthread; |
15 static SDL_Thread *threads[6]; | 15 static SDL_Thread *threads[6]; |
16 static volatile int doterminate = 0; | 16 static volatile int doterminate = 0; |
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 |
26 } | 26 } |
27 | 27 |
28 void | 28 void |
29 printid(void) | 29 printid(void) |
30 { | 30 { |
31 printf("Process %u: exiting\n", SDL_ThreadID()); | 31 printf("Process %lu: exiting\n", SDL_ThreadID()); |
32 } | 32 } |
33 | 33 |
34 void | 34 void |
35 terminate(int sig) | 35 terminate(int sig) |
36 { | 36 { |
39 } | 39 } |
40 | 40 |
41 void | 41 void |
42 closemutex(int sig) | 42 closemutex(int sig) |
43 { | 43 { |
44 Uint32 id = SDL_ThreadID(); | 44 SDL_threadID id = SDL_ThreadID(); |
45 int i; | 45 int i; |
46 printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id); | 46 printf("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id); |
47 doterminate = 1; | 47 doterminate = 1; |
48 for (i = 0; i < 6; ++i) | 48 for (i = 0; i < 6; ++i) |
49 SDL_WaitThread(threads[i], NULL); | 49 SDL_WaitThread(threads[i], NULL); |
50 SDL_DestroyMutex(mutex); | 50 SDL_DestroyMutex(mutex); |
51 exit(sig); | 51 exit(sig); |
55 Run(void *data) | 55 Run(void *data) |
56 { | 56 { |
57 if (SDL_ThreadID() == mainthread) | 57 if (SDL_ThreadID() == mainthread) |
58 signal(SIGTERM, closemutex); | 58 signal(SIGTERM, closemutex); |
59 while (!doterminate) { | 59 while (!doterminate) { |
60 printf("Process %u ready to work\n", SDL_ThreadID()); | 60 printf("Process %lu ready to work\n", SDL_ThreadID()); |
61 if (SDL_mutexP(mutex) < 0) { | 61 if (SDL_mutexP(mutex) < 0) { |
62 fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError()); | 62 fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError()); |
63 exit(1); | 63 exit(1); |
64 } | 64 } |
65 printf("Process %u, working!\n", SDL_ThreadID()); | 65 printf("Process %lu, working!\n", SDL_ThreadID()); |
66 SDL_Delay(1 * 1000); | 66 SDL_Delay(1 * 1000); |
67 printf("Process %u, done!\n", SDL_ThreadID()); | 67 printf("Process %lu, done!\n", SDL_ThreadID()); |
68 if (SDL_mutexV(mutex) < 0) { | 68 if (SDL_mutexV(mutex) < 0) { |
69 fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError()); | 69 fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError()); |
70 exit(1); | 70 exit(1); |
71 } | 71 } |
72 /* If this sleep isn't done, then threads may starve */ | 72 /* If this sleep isn't done, then threads may starve */ |
73 SDL_Delay(10); | 73 SDL_Delay(10); |
74 } | 74 } |
75 if (SDL_ThreadID() == mainthread && doterminate) { | 75 if (SDL_ThreadID() == mainthread && doterminate) { |
76 printf("Process %u: raising SIGTERM\n", SDL_ThreadID()); | 76 printf("Process %lu: raising SIGTERM\n", SDL_ThreadID()); |
77 raise(SIGTERM); | 77 raise(SIGTERM); |
78 } | 78 } |
79 return (0); | 79 return (0); |
80 } | 80 } |
81 | 81 |
96 fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError()); | 96 fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError()); |
97 exit(1); | 97 exit(1); |
98 } | 98 } |
99 | 99 |
100 mainthread = SDL_ThreadID(); | 100 mainthread = SDL_ThreadID(); |
101 printf("Main thread: %u\n", mainthread); | 101 printf("Main thread: %lu\n", mainthread); |
102 atexit(printid); | 102 atexit(printid); |
103 for (i = 0; i < maxproc; ++i) { | 103 for (i = 0; i < maxproc; ++i) { |
104 if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL) | 104 if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL) |
105 fprintf(stderr, "Couldn't create thread!\n"); | 105 fprintf(stderr, "Couldn't create thread!\n"); |
106 } | 106 } |