Mercurial > sdl-ios-xcode
annotate test/testsem.c @ 5247:78ce7bfd0faf
Better window parameter checking
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 10 Feb 2011 10:37:35 -0800 |
parents | d547877e355e |
children |
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 | |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
16 int SDLCALL |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
17 ThreadFunc(void *data) |
0 | 18 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
19 int threadnum = (int) (uintptr_t) data; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
20 while (alive) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
21 SDL_SemWait(sem); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
22 fprintf(stderr, |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
23 "Thread number %d has got the semaphore (value = %d)!\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
24 threadnum, SDL_SemValue(sem)); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
25 SDL_Delay(200); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
26 SDL_SemPost(sem); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
27 fprintf(stderr, |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
28 "Thread number %d has released the semaphore (value = %d)!\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
29 threadnum, SDL_SemValue(sem)); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
30 SDL_Delay(1); /* For the scheduler */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
31 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
32 printf("Thread number %d exiting.\n", threadnum); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
33 return 0; |
0 | 34 } |
35 | |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
36 static void |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
37 killed(int sig) |
0 | 38 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
39 alive = 0; |
0 | 40 } |
41 | |
5108
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
42 static void |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
43 TestWaitTimeout(void) |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
44 { |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
45 Uint32 start_ticks; |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
46 Uint32 end_ticks; |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
47 Uint32 duration; |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
48 |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
49 sem = SDL_CreateSemaphore(0); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
50 printf("Waiting 2 seconds on semaphore\n"); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
51 |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
52 start_ticks = SDL_GetTicks(); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
53 SDL_SemWaitTimeout(sem, 2000); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
54 end_ticks = SDL_GetTicks(); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
55 |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
56 duration = end_ticks - start_ticks; |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
57 |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
58 /* Accept a little offset in the effective wait */ |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
59 if (duration > 1900 && duration < 2050) |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
60 printf("Wait done.\n"); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
61 else |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
62 fprintf(stderr, "Wait took %d milliseconds\n", duration); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
63 } |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
64 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
65 int |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
66 main(int argc, char **argv) |
0 | 67 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
68 SDL_Thread *threads[NUM_THREADS]; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
69 uintptr_t i; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
70 int init_sem; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
71 |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
72 if (argc < 2) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
73 fprintf(stderr, "Usage: %s init_value\n", argv[0]); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
74 return (1); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
75 } |
0 | 76 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
77 /* Load the SDL library */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
78 if (SDL_Init(0) < 0) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
79 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
80 return (1); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
81 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
82 signal(SIGTERM, killed); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
83 signal(SIGINT, killed); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
84 |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
85 init_sem = atoi(argv[1]); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
86 sem = SDL_CreateSemaphore(init_sem); |
0 | 87 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
88 printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
89 init_sem); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
90 /* Create all the threads */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
91 for (i = 0; i < NUM_THREADS; ++i) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
92 threads[i] = SDL_CreateThread(ThreadFunc, (void *) i); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
93 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
94 |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
95 /* Wait 10 seconds */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
96 SDL_Delay(10 * 1000); |
0 | 97 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
98 /* Wait for all threads to finish */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
99 printf("Waiting for threads to finish\n"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
100 alive = 0; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
101 for (i = 0; i < NUM_THREADS; ++i) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
102 SDL_WaitThread(threads[i], NULL); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
103 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
104 printf("Finished waiting for threads\n"); |
0 | 105 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
106 SDL_DestroySemaphore(sem); |
5108
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
107 |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
108 TestWaitTimeout(); |
d547877e355e
Colin Leroy 2011-01-26 04:24:20 PST
Sam Lantinga <slouken@libsdl.org>
parents:
1895
diff
changeset
|
109 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
110 SDL_Quit(); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1769
diff
changeset
|
111 return (0); |
0 | 112 } |