comparison src/thread/dc/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 536b0704b7d8
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 18
19 Sam Lantinga 19 Sam Lantinga
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22
23 #include <errno.h>
24
25 #include "SDL_config.h" 22 #include "SDL_config.h"
26 23
27 /* An implementation of semaphores using mutexes and condition variables */ 24 /* An implementation of semaphores using mutexes and condition variables */
28 25
29 #include "SDL_timer.h" 26 #include "SDL_timer.h"
31 #include "SDL_systhread_c.h" 28 #include "SDL_systhread_c.h"
32 29
33 30
34 #if SDL_THREADS_DISABLED 31 #if SDL_THREADS_DISABLED
35 32
36 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 33 SDL_sem *
34 SDL_CreateSemaphore(Uint32 initial_value)
37 { 35 {
38 SDL_SetError("SDL not configured with thread support"); 36 SDL_SetError("SDL not configured with thread support");
39 return (SDL_sem *)0; 37 return (SDL_sem *) 0;
40 } 38 }
41 39
42 void SDL_DestroySemaphore(SDL_sem *sem) 40 void
41 SDL_DestroySemaphore(SDL_sem * sem)
43 { 42 {
44 return; 43 return;
45 } 44 }
46 45
47 int SDL_SemTryWait(SDL_sem *sem) 46 int
47 SDL_SemTryWait(SDL_sem * sem)
48 { 48 {
49 SDL_SetError("SDL not configured with thread support"); 49 SDL_SetError("SDL not configured with thread support");
50 return -1; 50 return -1;
51 } 51 }
52 52
53 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 53 int
54 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
54 { 55 {
55 SDL_SetError("SDL not configured with thread support"); 56 SDL_SetError("SDL not configured with thread support");
56 return -1; 57 return -1;
57 } 58 }
58 59
59 int SDL_SemWait(SDL_sem *sem) 60 int
61 SDL_SemWait(SDL_sem * sem)
60 { 62 {
61 SDL_SetError("SDL not configured with thread support"); 63 SDL_SetError("SDL not configured with thread support");
62 return -1; 64 return -1;
63 } 65 }
64 66
65 Uint32 SDL_SemValue(SDL_sem *sem) 67 Uint32
68 SDL_SemValue(SDL_sem * sem)
66 { 69 {
67 return 0; 70 return 0;
68 } 71 }
69 72
70 int SDL_SemPost(SDL_sem *sem) 73 int
74 SDL_SemPost(SDL_sem * sem)
71 { 75 {
72 SDL_SetError("SDL not configured with thread support"); 76 SDL_SetError("SDL not configured with thread support");
73 return -1; 77 return -1;
74 } 78 }
75 79
76 #else 80 #else
77 81
78 #include <kos/sem.h> 82 #include <kos/sem.h>
79 83
80 struct SDL_semaphore 84 struct SDL_semaphore
81 { 85 {
82 semaphore_t sem; 86 semaphore_t sem;
83 }; 87 };
84 88
85 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 89 SDL_sem *
90 SDL_CreateSemaphore(Uint32 initial_value)
86 { 91 {
87 return (SDL_sem *)sem_create(initial_value); 92 return (SDL_sem *) sem_create(initial_value);
88 } 93 }
89 94
90 /* WARNING: 95 /* WARNING:
91 You cannot call this function when another thread is using the semaphore. 96 You cannot call this function when another thread is using the semaphore.
92 */ 97 */
93 void SDL_DestroySemaphore(SDL_sem *sem) 98 void
99 SDL_DestroySemaphore(SDL_sem * sem)
94 { 100 {
95 if ( ! sem ) { 101 if (!sem) {
96 SDL_SetError("Passed a NULL semaphore"); 102 SDL_SetError("Passed a NULL semaphore");
97 return; 103 return;
98 } 104 }
99 105
100 sem_destroy(&sem->sem); 106 sem_destroy(&sem->sem);
101 } 107 }
102 108
103 int SDL_SemTryWait(SDL_sem *sem) 109 int
110 SDL_SemTryWait(SDL_sem * sem)
104 { 111 {
105 int retval; 112 int retval;
106 113
107 if ( ! sem ) { 114 if (!sem) {
108 SDL_SetError("Passed a NULL semaphore"); 115 SDL_SetError("Passed a NULL semaphore");
109 return -1; 116 return -1;
110 } 117 }
111 118
112 retval = sem_trywait(&sem->sem); 119 retval = sem_trywait(&sem->sem);
113 if (retval==0) return 0; 120 if (retval == 0)
114 else return SDL_MUTEX_TIMEDOUT; 121 return 0;
122 else
123 return SDL_MUTEX_TIMEDOUT;
115 124
116 return retval; 125 return retval;
117 } 126 }
118 127
119 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 128 int
129 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
120 { 130 {
121 int retval; 131 int retval;
122 132
123 if ( ! sem ) { 133 if (!sem) {
124 SDL_SetError("Passed a NULL semaphore"); 134 SDL_SetError("Passed a NULL semaphore");
125 return -1; 135 return -1;
126 } 136 }
127 137
128 /* A timeout of 0 is an easy case */ 138 /* A timeout of 0 is an easy case */
129 if ( timeout == 0 ) { 139 if (timeout == 0) {
130 return SDL_SemTryWait(sem); 140 return SDL_SemTryWait(sem);
131 } 141 }
132 142
133 retval = sem_wait_timed(&sem->sem,timeout); 143 retval = sem_wait_timed(&sem->sem, timeout);
134 if (retval==-1) retval= SDL_MUTEX_TIMEDOUT; 144 if (retval == -1)
145 retval = SDL_MUTEX_TIMEDOUT;
135 146
136 return retval; 147 return retval;
137 } 148 }
138 149
139 int SDL_SemWait(SDL_sem *sem) 150 int
151 SDL_SemWait(SDL_sem * sem)
140 { 152 {
141 int retval; 153 if (!sem) {
154 SDL_SetError("Passed a NULL semaphore");
155 return -1;
156 }
142 157
143 if ( ! sem ) { 158 sem_wait(&sem->sem);
144 SDL_SetError("Passed a NULL semaphore"); 159 return 0;
145 return -1;
146 }
147
148 while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
149 return retval;
150 } 160 }
151 161
152 Uint32 SDL_SemValue(SDL_sem *sem) 162 Uint32
163 SDL_SemValue(SDL_sem * sem)
153 { 164 {
154 if ( ! sem ) { 165 if (!sem) {
155 SDL_SetError("Passed a NULL semaphore"); 166 SDL_SetError("Passed a NULL semaphore");
156 return -1; 167 return -1;
157 } 168 }
158 169
159 return sem_count(&sem->sem); 170 return sem_count(&sem->sem);
160 } 171 }
161 172
162 int SDL_SemPost(SDL_sem *sem) 173 int
174 SDL_SemPost(SDL_sem * sem)
163 { 175 {
164 if ( ! sem ) { 176 if (!sem) {
165 SDL_SetError("Passed a NULL semaphore"); 177 SDL_SetError("Passed a NULL semaphore");
166 return -1; 178 return -1;
167 } 179 }
168 180
169 sem_signal(&sem->sem); 181 sem_signal(&sem->sem);
170 return 0; 182 return 0;
171 } 183 }
172 184
173 #endif /* SDL_THREADS_DISABLED */ 185 #endif /* SDL_THREADS_DISABLED */
186 /* vi: set ts=4 sw=4 expandtab: */