/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testlock.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testlock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 401 - (show annotations) (download)
Fri Feb 25 17:31:09 2011 UTC (9 years, 9 months ago) by william
File MIME type: text/plain
File size: 2581 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4358 local: v0.9.7.313-latest) in ./trunk
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 SDL_threadID mainthread;
15 static SDL_Thread *threads[6];
16 static volatile int doterminate = 0;
17
18 /*
19 * SDL_Quit() shouldn't be used with atexit() directly because
20 * calling conventions may differ...
21 */
22 static void
23 SDL_Quit_Wrapper(void)
24 {
25 SDL_Quit();
26 }
27
28 void
29 printid(void)
30 {
31 printf("Process %lu: exiting\n", SDL_ThreadID());
32 }
33
34 void
35 terminate(int sig)
36 {
37 signal(SIGINT, terminate);
38 doterminate = 1;
39 }
40
41 void
42 closemutex(int sig)
43 {
44 SDL_threadID id = SDL_ThreadID();
45 int i;
46 printf("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
47 doterminate = 1;
48 for (i = 0; i < 6; ++i)
49 SDL_WaitThread(threads[i], NULL);
50 SDL_DestroyMutex(mutex);
51 exit(sig);
52 }
53
54 int SDLCALL
55 Run(void *data)
56 {
57 if (SDL_ThreadID() == mainthread)
58 signal(SIGTERM, closemutex);
59 while (!doterminate) {
60 printf("Process %lu ready to work\n", SDL_ThreadID());
61 if (SDL_mutexP(mutex) < 0) {
62 fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
63 exit(1);
64 }
65 printf("Process %lu, working!\n", SDL_ThreadID());
66 SDL_Delay(1 * 1000);
67 printf("Process %lu, done!\n", SDL_ThreadID());
68 if (SDL_mutexV(mutex) < 0) {
69 fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
70 exit(1);
71 }
72 /* If this sleep isn't done, then threads may starve */
73 SDL_Delay(10);
74 }
75 if (SDL_ThreadID() == mainthread && doterminate) {
76 printf("Process %lu: raising SIGTERM\n", SDL_ThreadID());
77 raise(SIGTERM);
78 }
79 return (0);
80 }
81
82 int
83 main(int argc, char *argv[])
84 {
85 int i;
86 int maxproc = 6;
87
88 /* Load the SDL library */
89 if (SDL_Init(0) < 0) {
90 fprintf(stderr, "%s\n", SDL_GetError());
91 exit(1);
92 }
93 atexit(SDL_Quit_Wrapper);
94
95 if ((mutex = SDL_CreateMutex()) == NULL) {
96 fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
97 exit(1);
98 }
99
100 mainthread = SDL_ThreadID();
101 printf("Main thread: %lu\n", mainthread);
102 atexit(printid);
103 for (i = 0; i < maxproc; ++i) {
104 if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
105 fprintf(stderr, "Couldn't create thread!\n");
106 }
107 signal(SIGINT, terminate);
108 Run(NULL);
109
110 return (0); /* Never reached */
111 }

  ViewVC Help
Powered by ViewVC 1.1.22