comparison src/thread/irix/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 ad887c988713
children e27bdcc80744 204be4fc2726
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
34 34
35 #include "SDL_error.h" 35 #include "SDL_error.h"
36 #include "SDL_thread.h" 36 #include "SDL_thread.h"
37 37
38 38
39 struct SDL_semaphore { 39 struct SDL_semaphore
40 int id; 40 {
41 int id;
41 }; 42 };
42 43
43 /* Not defined by many operating systems, use configure to detect */ 44 /* Not defined by many operating systems, use configure to detect */
44 /* 45 /*
45 #if !defined(HAVE_SEMUN) 46 #if !defined(HAVE_SEMUN)
50 }; 51 };
51 #endif 52 #endif
52 */ 53 */
53 54
54 static struct sembuf op_trywait[2] = { 55 static struct sembuf op_trywait[2] = {
55 { 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */ 56 {0, -1, (IPC_NOWAIT | SEM_UNDO)} /* Decrement semaphore, no block */
56 }; 57 };
57 static struct sembuf op_wait[2] = { 58 static struct sembuf op_wait[2] = {
58 { 0, -1, SEM_UNDO } /* Decrement semaphore */ 59 {0, -1, SEM_UNDO} /* Decrement semaphore */
59 }; 60 };
60 static struct sembuf op_post[1] = { 61 static struct sembuf op_post[1] = {
61 { 0, 1, (IPC_NOWAIT|SEM_UNDO) } /* Increment semaphore */ 62 {0, 1, (IPC_NOWAIT | SEM_UNDO)} /* Increment semaphore */
62 }; 63 };
63 64
64 /* Create a blockable semaphore */ 65 /* Create a blockable semaphore */
65 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 66 SDL_sem *
66 { 67 SDL_CreateSemaphore(Uint32 initial_value)
67 extern int _creating_thread_lock; /* SDL_threads.c */ 68 {
68 SDL_sem *sem; 69 extern int _creating_thread_lock; /* SDL_threads.c */
69 union semun init; 70 SDL_sem *sem;
70 71 union semun init;
71 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); 72
72 if ( sem == NULL ) { 73 sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
73 SDL_OutOfMemory(); 74 if (sem == NULL) {
74 return(NULL); 75 SDL_OutOfMemory();
75 } 76 return (NULL);
76 sem->id = semget(IPC_PRIVATE, 1, (0600|IPC_CREAT)); 77 }
77 if ( sem->id < 0 ) { 78 sem->id = semget(IPC_PRIVATE, 1, (0600 | IPC_CREAT));
78 SDL_SetError("Couldn't create semaphore"); 79 if (sem->id < 0) {
79 SDL_free(sem); 80 SDL_SetError("Couldn't create semaphore");
80 return(NULL); 81 SDL_free(sem);
81 } 82 return (NULL);
82 init.val = initial_value; /* Initialize semaphore */ 83 }
83 semctl(sem->id, 0, SETVAL, init); 84 init.val = initial_value; /* Initialize semaphore */
84 return(sem); 85 semctl(sem->id, 0, SETVAL, init);
85 } 86 return (sem);
86 87 }
87 void SDL_DestroySemaphore(SDL_sem *sem) 88
88 { 89 void
89 if ( sem ) { 90 SDL_DestroySemaphore(SDL_sem * sem)
91 {
92 if (sem) {
90 #ifdef __IRIX__ 93 #ifdef __IRIX__
91 semctl(sem->id, 0, IPC_RMID); 94 semctl(sem->id, 0, IPC_RMID);
92 #else 95 #else
93 union semun dummy; 96 union semun dummy;
94 dummy.val = 0; 97 dummy.val = 0;
95 semctl(sem->id, 0, IPC_RMID, dummy); 98 semctl(sem->id, 0, IPC_RMID, dummy);
96 #endif 99 #endif
97 SDL_free(sem); 100 SDL_free(sem);
98 } 101 }
99 } 102 }
100 103
101 int SDL_SemTryWait(SDL_sem *sem) 104 int
102 { 105 SDL_SemTryWait(SDL_sem * sem)
103 int retval; 106 {
104 107 int retval;
105 if ( ! sem ) { 108
106 SDL_SetError("Passed a NULL semaphore"); 109 if (!sem) {
107 return -1; 110 SDL_SetError("Passed a NULL semaphore");
108 } 111 return -1;
109 112 }
110 retval = 0; 113
114 retval = 0;
111 tryagain: 115 tryagain:
112 if ( semop(sem->id, op_trywait, 1) < 0 ) { 116 if (semop(sem->id, op_trywait, 1) < 0) {
113 if ( errno == EINTR ) { 117 if (errno == EINTR) {
114 goto tryagain; 118 goto tryagain;
115 } 119 }
116 retval = SDL_MUTEX_TIMEDOUT; 120 retval = SDL_MUTEX_TIMEDOUT;
117 } 121 }
118 return retval; 122 return retval;
119 } 123 }
120 124
121 int SDL_SemWait(SDL_sem *sem) 125 int
122 { 126 SDL_SemWait(SDL_sem * sem)
123 int retval; 127 {
124 128 int retval;
125 if ( ! sem ) { 129
126 SDL_SetError("Passed a NULL semaphore"); 130 if (!sem) {
127 return -1; 131 SDL_SetError("Passed a NULL semaphore");
128 } 132 return -1;
129 133 }
130 retval = 0; 134
135 retval = 0;
131 tryagain: 136 tryagain:
132 if ( semop(sem->id, op_wait, 1) < 0 ) { 137 if (semop(sem->id, op_wait, 1) < 0) {
133 if ( errno == EINTR ) { 138 if (errno == EINTR) {
134 goto tryagain; 139 goto tryagain;
135 } 140 }
136 SDL_SetError("Semaphore operation error"); 141 SDL_SetError("Semaphore operation error");
137 retval = -1; 142 retval = -1;
138 } 143 }
139 return retval; 144 return retval;
140 } 145 }
141 146
142 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 147 int
143 { 148 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
144 int retval; 149 {
145 150 int retval;
146 if ( ! sem ) { 151
147 SDL_SetError("Passed a NULL semaphore"); 152 if (!sem) {
148 return -1; 153 SDL_SetError("Passed a NULL semaphore");
149 } 154 return -1;
150 155 }
151 /* Try the easy cases first */ 156
152 if ( timeout == 0 ) { 157 /* Try the easy cases first */
153 return SDL_SemTryWait(sem); 158 if (timeout == 0) {
154 } 159 return SDL_SemTryWait(sem);
155 if ( timeout == SDL_MUTEX_MAXWAIT ) { 160 }
156 return SDL_SemWait(sem); 161 if (timeout == SDL_MUTEX_MAXWAIT) {
157 } 162 return SDL_SemWait(sem);
158 163 }
159 /* Ack! We have to busy wait... */ 164
160 timeout += SDL_GetTicks(); 165 /* Ack! We have to busy wait... */
161 do { 166 timeout += SDL_GetTicks();
162 retval = SDL_SemTryWait(sem); 167 do {
163 if ( retval == 0 ) { 168 retval = SDL_SemTryWait(sem);
164 break; 169 if (retval == 0) {
165 } 170 break;
166 SDL_Delay(1); 171 }
167 } while ( SDL_GetTicks() < timeout ); 172 SDL_Delay(1);
168 173 }
169 return retval; 174 while (SDL_GetTicks() < timeout);
170 } 175
171 176 return retval;
172 Uint32 SDL_SemValue(SDL_sem *sem) 177 }
173 { 178
174 int semval; 179 Uint32
175 Uint32 value; 180 SDL_SemValue(SDL_sem * sem)
176 181 {
177 value = 0; 182 int semval;
178 if ( sem ) { 183 Uint32 value;
179 tryagain: 184
185 value = 0;
186 if (sem) {
187 tryagain:
180 #ifdef __IRIX__ 188 #ifdef __IRIX__
181 semval = semctl(sem->id, 0, GETVAL); 189 semval = semctl(sem->id, 0, GETVAL);
182 #else 190 #else
183 { 191 {
184 union semun arg; 192 union semun arg;
185 arg.val = 0; 193 arg.val = 0;
186 semval = semctl(sem->id, 0, GETVAL, arg); 194 semval = semctl(sem->id, 0, GETVAL, arg);
187 } 195 }
188 #endif 196 #endif
189 if ( semval < 0 ) { 197 if (semval < 0) {
190 if ( errno == EINTR ) { 198 if (errno == EINTR) {
191 goto tryagain; 199 goto tryagain;
192 } 200 }
193 } else { 201 } else {
194 value = (Uint32)semval; 202 value = (Uint32) semval;
195 } 203 }
196 } 204 }
197 return value; 205 return value;
198 } 206 }
199 207
200 int SDL_SemPost(SDL_sem *sem) 208 int
201 { 209 SDL_SemPost(SDL_sem * sem)
202 int retval; 210 {
203 211 int retval;
204 if ( ! sem ) { 212
205 SDL_SetError("Passed a NULL semaphore"); 213 if (!sem) {
206 return -1; 214 SDL_SetError("Passed a NULL semaphore");
207 } 215 return -1;
208 216 }
209 retval = 0; 217
218 retval = 0;
210 tryagain: 219 tryagain:
211 if ( semop(sem->id, op_post, 1) < 0 ) { 220 if (semop(sem->id, op_post, 1) < 0) {
212 if ( errno == EINTR ) { 221 if (errno == EINTR) {
213 goto tryagain; 222 goto tryagain;
214 } 223 }
215 SDL_SetError("Semaphore operation error"); 224 SDL_SetError("Semaphore operation error");
216 retval = -1; 225 retval = -1;
217 } 226 }
218 return retval; 227 return retval;
219 } 228 }
229
230 /* vi: set ts=4 sw=4 expandtab: */