Mercurial > sdl-ios-xcode
comparison src/thread/beos/SDL_syssem.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | d910939febfa |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
26 #include <be/kernel/OS.h> | 26 #include <be/kernel/OS.h> |
27 | 27 |
28 #include "SDL_thread.h" | 28 #include "SDL_thread.h" |
29 | 29 |
30 | 30 |
31 struct SDL_semaphore { | 31 struct SDL_semaphore |
32 sem_id id; | 32 { |
33 sem_id id; | |
33 }; | 34 }; |
34 | 35 |
35 /* Create a counting semaphore */ | 36 /* Create a counting semaphore */ |
36 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | 37 SDL_sem * |
38 SDL_CreateSemaphore(Uint32 initial_value) | |
37 { | 39 { |
38 SDL_sem *sem; | 40 SDL_sem *sem; |
39 | 41 |
40 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); | 42 sem = (SDL_sem *) SDL_malloc(sizeof(*sem)); |
41 if ( sem ) { | 43 if (sem) { |
42 sem->id = create_sem(initial_value, "SDL semaphore"); | 44 sem->id = create_sem(initial_value, "SDL semaphore"); |
43 if ( sem->id < B_NO_ERROR ) { | 45 if (sem->id < B_NO_ERROR) { |
44 SDL_SetError("create_sem() failed"); | 46 SDL_SetError("create_sem() failed"); |
45 SDL_free(sem); | 47 SDL_free(sem); |
46 sem = NULL; | 48 sem = NULL; |
47 } | 49 } |
48 } else { | 50 } else { |
49 SDL_OutOfMemory(); | 51 SDL_OutOfMemory(); |
50 } | 52 } |
51 return(sem); | 53 return (sem); |
52 } | 54 } |
53 | 55 |
54 /* Free the semaphore */ | 56 /* Free the semaphore */ |
55 void SDL_DestroySemaphore(SDL_sem *sem) | 57 void |
58 SDL_DestroySemaphore(SDL_sem * sem) | |
56 { | 59 { |
57 if ( sem ) { | 60 if (sem) { |
58 if ( sem->id >= B_NO_ERROR ) { | 61 if (sem->id >= B_NO_ERROR) { |
59 delete_sem(sem->id); | 62 delete_sem(sem->id); |
60 } | 63 } |
61 SDL_free(sem); | 64 SDL_free(sem); |
62 } | 65 } |
63 } | 66 } |
64 | 67 |
65 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | 68 int |
69 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) | |
66 { | 70 { |
67 int32 val; | 71 int32 val; |
68 int retval; | 72 int retval; |
69 | 73 |
70 if ( ! sem ) { | 74 if (!sem) { |
71 SDL_SetError("Passed a NULL semaphore"); | 75 SDL_SetError("Passed a NULL semaphore"); |
72 return -1; | 76 return -1; |
73 } | 77 } |
74 | 78 |
75 tryagain: | 79 tryagain: |
76 if ( timeout == SDL_MUTEX_MAXWAIT ) { | 80 if (timeout == SDL_MUTEX_MAXWAIT) { |
77 val = acquire_sem(sem->id); | 81 val = acquire_sem(sem->id); |
78 } else { | 82 } else { |
79 timeout *= 1000; /* BeOS uses a timeout in microseconds */ | 83 timeout *= 1000; /* BeOS uses a timeout in microseconds */ |
80 val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout); | 84 val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout); |
81 } | 85 } |
82 switch (val) { | 86 switch (val) { |
83 case B_INTERRUPTED: | 87 case B_INTERRUPTED: |
84 goto tryagain; | 88 goto tryagain; |
85 case B_NO_ERROR: | 89 case B_NO_ERROR: |
86 retval = 0; | 90 retval = 0; |
87 break; | 91 break; |
88 case B_TIMED_OUT: | 92 case B_TIMED_OUT: |
89 retval = SDL_MUTEX_TIMEDOUT; | 93 retval = SDL_MUTEX_TIMEDOUT; |
90 break; | 94 break; |
91 case B_WOULD_BLOCK: | 95 case B_WOULD_BLOCK: |
92 retval = SDL_MUTEX_TIMEDOUT; | 96 retval = SDL_MUTEX_TIMEDOUT; |
93 break; | 97 break; |
94 default: | 98 default: |
95 SDL_SetError("acquire_sem() failed"); | 99 SDL_SetError("acquire_sem() failed"); |
96 retval = -1; | 100 retval = -1; |
97 break; | 101 break; |
98 } | 102 } |
99 | 103 |
100 return retval; | 104 return retval; |
101 } | 105 } |
102 | 106 |
103 int SDL_SemTryWait(SDL_sem *sem) | 107 int |
108 SDL_SemTryWait(SDL_sem * sem) | |
104 { | 109 { |
105 return SDL_SemWaitTimeout(sem, 0); | 110 return SDL_SemWaitTimeout(sem, 0); |
106 } | 111 } |
107 | 112 |
108 int SDL_SemWait(SDL_sem *sem) | 113 int |
114 SDL_SemWait(SDL_sem * sem) | |
109 { | 115 { |
110 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | 116 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); |
111 } | 117 } |
112 | 118 |
113 /* Returns the current count of the semaphore */ | 119 /* Returns the current count of the semaphore */ |
114 Uint32 SDL_SemValue(SDL_sem *sem) | 120 Uint32 |
121 SDL_SemValue(SDL_sem * sem) | |
115 { | 122 { |
116 int32 count; | 123 int32 count; |
117 Uint32 value; | 124 Uint32 value; |
118 | 125 |
119 value = 0; | 126 value = 0; |
120 if ( sem ) { | 127 if (sem) { |
121 get_sem_count(sem->id, &count); | 128 get_sem_count(sem->id, &count); |
122 if ( count > 0 ) { | 129 if (count > 0) { |
123 value = (Uint32)count; | 130 value = (Uint32) count; |
124 } | 131 } |
125 } | 132 } |
126 return value; | 133 return value; |
127 } | 134 } |
128 | 135 |
129 /* Atomically increases the semaphore's count (not blocking) */ | 136 /* Atomically increases the semaphore's count (not blocking) */ |
130 int SDL_SemPost(SDL_sem *sem) | 137 int |
138 SDL_SemPost(SDL_sem * sem) | |
131 { | 139 { |
132 if ( ! sem ) { | 140 if (!sem) { |
133 SDL_SetError("Passed a NULL semaphore"); | 141 SDL_SetError("Passed a NULL semaphore"); |
134 return -1; | 142 return -1; |
135 } | 143 } |
136 | 144 |
137 if ( release_sem(sem->id) != B_NO_ERROR ) { | 145 if (release_sem(sem->id) != B_NO_ERROR) { |
138 SDL_SetError("release_sem() failed"); | 146 SDL_SetError("release_sem() failed"); |
139 return -1; | 147 return -1; |
140 } | 148 } |
141 return 0; | 149 return 0; |
142 } | 150 } |
151 | |
152 /* vi: set ts=4 sw=4 expandtab: */ |