comparison test/testlock.c @ 1668:4da1ee79c9af SDL-1.3

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