Mercurial > sdl-ios-xcode
annotate test/testsem.c @ 1316:0781906086fa
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 08:24:32 +0000 |
parents | be9c9c8f6d53 |
children | 4d3bb026cd16 |
rev | line source |
---|---|
0 | 1 |
2 /* Simple test of the SDL semaphore code */ | |
3 | |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <signal.h> | |
7 | |
8 #include "SDL.h" | |
9 #include "SDL_thread.h" | |
10 | |
11 #define NUM_THREADS 10 | |
12 | |
13 static SDL_sem *sem; | |
14 int alive = 1; | |
15 | |
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
|
16 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
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
|
17 static void quit(int rc) |
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(); |
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 exit(rc); |
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 |
0 | 23 int ThreadFunc(void *data) |
24 { | |
25 while ( alive ) { | |
26 SDL_SemWait(sem); | |
27 fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", (int)data, SDL_SemValue(sem)); | |
28 SDL_Delay(200); | |
29 SDL_SemPost(sem); | |
30 fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", (int)data, SDL_SemValue(sem)); | |
31 SDL_Delay(1); /* For the scheduler */ | |
32 } | |
33 printf("Thread number %d exiting.\n", (int)data); | |
34 return 0; | |
35 } | |
36 | |
37 static void killed(int sig) | |
38 { | |
39 alive = 0; | |
40 } | |
41 | |
42 int main(int argc, char **argv) | |
43 { | |
44 SDL_Thread *threads[NUM_THREADS]; | |
45 int i, init_sem; | |
46 | |
47 if(argc < 2) { | |
48 fprintf(stderr,"Usage: %s init_value\n", argv[0]); | |
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
|
49 return(1); |
0 | 50 } |
51 | |
52 /* Load the SDL library */ | |
53 if ( SDL_Init(0) < 0 ) { | |
54 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | |
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
|
55 return(1); |
0 | 56 } |
57 signal(SIGTERM, killed); | |
58 signal(SIGINT, killed); | |
59 | |
60 init_sem = atoi(argv[1]); | |
61 sem = SDL_CreateSemaphore(init_sem); | |
62 | |
63 printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem); | |
64 /* Create all the threads */ | |
65 for( i = 0; i < NUM_THREADS; ++i ) { | |
66 threads[i] = SDL_CreateThread(ThreadFunc, (void*)i); | |
67 } | |
68 | |
69 /* Wait 10 seconds */ | |
70 SDL_Delay(10 * 1000); | |
71 | |
72 /* Wait for all threads to finish */ | |
73 printf("Waiting for threads to finish\n"); | |
74 alive = 0; | |
75 for( i = 0; i < NUM_THREADS; ++i ) { | |
76 SDL_WaitThread(threads[i], NULL); | |
77 } | |
78 printf("Finished waiting for threads\n"); | |
79 | |
80 SDL_DestroySemaphore(sem); | |
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
|
81 SDL_Quit(); |
0 | 82 return(0); |
83 } |