Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 32 | /* Posix threads interface */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
| 35 | #include <pthread.h> |
| 36 | |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 38 | /* try to determine what version of the Pthread Standard is installed. |
| 39 | * this is important, since all sorts of parameter types changed from |
| 40 | * draft to draft and there are several (incompatible) drafts in |
| 41 | * common use. these macros are a start, at least. |
| 42 | * 12 May 1997 -- david arnold <davida@pobox.com> |
| 43 | */ |
| 44 | |
| 45 | #if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_) |
| 46 | /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */ |
| 47 | # define PY_PTHREAD_D4 |
| 48 | |
| 49 | #elif defined(__osf__) && defined (__alpha) |
| 50 | /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */ |
| 51 | # if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4) |
| 52 | # define PY_PTHREAD_D4 |
| 53 | # else |
| 54 | # define PY_PTHREAD_STD |
| 55 | # endif |
| 56 | |
| 57 | #elif defined(_AIX) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 58 | /* SCHED_BG_NP is defined if using AIX DCE pthreads |
| 59 | * but it is unsupported by AIX 4 pthreads. Default |
| 60 | * attributes for AIX 4 pthreads equal to NULL. For |
| 61 | * AIX DCE pthreads they should be left unchanged. |
| 62 | */ |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 63 | # if !defined(SCHED_BG_NP) |
| 64 | # define PY_PTHREAD_STD |
| 65 | # else |
| 66 | # define PY_PTHREAD_D7 |
| 67 | # endif |
| 68 | |
| 69 | #elif defined(__unix) && defined(__sparc) |
| 70 | # define PY_PTHREAD_STD |
Guido van Rossum | c8fba8e | 1997-05-15 12:24:53 +0000 | [diff] [blame] | 71 | #elif defined(__linux) |
| 72 | # define PY_PTHREAD_STD |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame^] | 73 | #elif defined(__DGUX) |
| 74 | # define PY_PTHREAD_D6 |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 77 | |
| 78 | /* set default attribute object for different versions */ |
| 79 | |
| 80 | #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7) |
| 81 | # define pthread_attr_default pthread_attr_default |
| 82 | # define pthread_mutexattr_default pthread_mutexattr_default |
| 83 | # define pthread_condattr_default pthread_condattr_default |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame^] | 84 | #elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 85 | # define pthread_attr_default ((pthread_attr_t *)NULL) |
| 86 | # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL) |
| 87 | # define pthread_condattr_default ((pthread_condattr_t *)NULL) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 88 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 91 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 92 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 93 | * following are undefined: |
| 94 | * -> a thread tries to lock a mutex it already has locked |
| 95 | * -> a thread tries to unlock a mutex locked by a different thread |
| 96 | * pthread mutexes are designed for serializing threads over short pieces |
| 97 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 98 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 99 | * |
| 100 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 101 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 102 | * instantly, it is, else the pair is used to block the thread until the |
| 103 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 104 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 106 | typedef struct { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 107 | char locked; /* 0=unlocked, 1=locked */ |
| 108 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 109 | pthread_cond_t lock_released; |
| 110 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 111 | } pthread_lock; |
| 112 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 113 | #define CHECK_STATUS(name) if (status < 0) { perror(name); error=1; } |
| 114 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 115 | /* |
| 116 | * Initialization. |
| 117 | */ |
| 118 | static void _init_thread _P0() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Thread support. |
| 124 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 125 | |
| 126 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 127 | int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
| 128 | { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 129 | pthread_t th; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 130 | int success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 131 | dprintf(("start_new_thread called\n")); |
| 132 | if (!initialized) |
| 133 | init_thread(); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 134 | |
| 135 | success = pthread_create(&th, |
| 136 | #if defined(PY_PTHREAD_D4) |
| 137 | pthread_attr_default, |
| 138 | (pthread_startroutine_t)func, |
| 139 | (pthread_addr_t)arg |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame^] | 140 | #elif defined(PY_PTHREAD_D6) |
| 141 | pthread_attr_default, |
| 142 | (void* (*)_P((void *)))func, |
| 143 | arg |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 144 | #elif defined(PY_PTHREAD_D7) |
| 145 | pthread_attr_default, |
| 146 | func, |
| 147 | arg |
| 148 | #elif defined(PY_PTHREAD_STD) |
| 149 | (pthread_attr_t*)NULL, |
| 150 | (void* (*)_P((void *)))func, |
| 151 | (void *)arg |
| 152 | #endif |
| 153 | ); |
| 154 | |
| 155 | if (success >= 0) { |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame^] | 156 | #if defined(PY_THREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 157 | pthread_detach(&th); |
| 158 | #elif defined(PY_PTHREAD_STD) |
Guido van Rossum | f4806c2 | 1997-04-30 19:59:22 +0000 | [diff] [blame] | 159 | pthread_detach(th); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 160 | #endif |
| 161 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 162 | return success < 0 ? 0 : 1; |
| 163 | } |
| 164 | |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 165 | long get_thread_ident _P0() |
| 166 | { |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 167 | pthread_t threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 168 | if (!initialized) |
| 169 | init_thread(); |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 170 | /* Jump through some hoops for Alpha OSF/1 */ |
| 171 | threadid = pthread_self(); |
| 172 | return (long) *(long *) &threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 175 | static void do_exit_thread _P1(no_cleanup, int no_cleanup) |
| 176 | { |
| 177 | dprintf(("exit_thread called\n")); |
| 178 | if (!initialized) |
| 179 | if (no_cleanup) |
| 180 | _exit(0); |
| 181 | else |
| 182 | exit(0); |
| 183 | } |
| 184 | |
| 185 | void exit_thread _P0() |
| 186 | { |
| 187 | do_exit_thread(0); |
| 188 | } |
| 189 | |
| 190 | void _exit_thread _P0() |
| 191 | { |
| 192 | do_exit_thread(1); |
| 193 | } |
| 194 | |
| 195 | #ifndef NO_EXIT_PROG |
| 196 | static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) |
| 197 | { |
| 198 | dprintf(("exit_prog(%d) called\n", status)); |
| 199 | if (!initialized) |
| 200 | if (no_cleanup) |
| 201 | _exit(status); |
| 202 | else |
| 203 | exit(status); |
| 204 | } |
| 205 | |
| 206 | void exit_prog _P1(status, int status) |
| 207 | { |
| 208 | do_exit_prog(status, 0); |
| 209 | } |
| 210 | |
| 211 | void _exit_prog _P1(status, int status) |
| 212 | { |
| 213 | do_exit_prog(status, 1); |
| 214 | } |
| 215 | #endif /* NO_EXIT_PROG */ |
| 216 | |
| 217 | /* |
| 218 | * Lock support. |
| 219 | */ |
| 220 | type_lock allocate_lock _P0() |
| 221 | { |
| 222 | pthread_lock *lock; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 223 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 224 | |
| 225 | dprintf(("allocate_lock called\n")); |
| 226 | if (!initialized) |
| 227 | init_thread(); |
| 228 | |
| 229 | lock = (pthread_lock *) malloc(sizeof(pthread_lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 230 | if (lock) { |
| 231 | lock->locked = 0; |
| 232 | |
| 233 | status = pthread_mutex_init(&lock->mut, |
| 234 | pthread_mutexattr_default); |
| 235 | CHECK_STATUS("pthread_mutex_init"); |
| 236 | |
| 237 | status = pthread_cond_init(&lock->lock_released, |
| 238 | pthread_condattr_default); |
| 239 | CHECK_STATUS("pthread_cond_init"); |
| 240 | |
| 241 | if (error) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 242 | free((void *)lock); |
| 243 | lock = 0; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | dprintf(("allocate_lock() -> %lx\n", (long)lock)); |
| 248 | return (type_lock) lock; |
| 249 | } |
| 250 | |
| 251 | void free_lock _P1(lock, type_lock lock) |
| 252 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 253 | pthread_lock *thelock = (pthread_lock *)lock; |
| 254 | int status, error = 0; |
| 255 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 256 | dprintf(("free_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 257 | |
| 258 | status = pthread_mutex_destroy( &thelock->mut ); |
| 259 | CHECK_STATUS("pthread_mutex_destroy"); |
| 260 | |
| 261 | status = pthread_cond_destroy( &thelock->lock_released ); |
| 262 | CHECK_STATUS("pthread_cond_destroy"); |
| 263 | |
| 264 | free((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag) |
| 268 | { |
| 269 | int success; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 270 | pthread_lock *thelock = (pthread_lock *)lock; |
| 271 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 272 | |
| 273 | dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 275 | status = pthread_mutex_lock( &thelock->mut ); |
| 276 | CHECK_STATUS("pthread_mutex_lock[1]"); |
| 277 | success = thelock->locked == 0; |
| 278 | if (success) thelock->locked = 1; |
| 279 | status = pthread_mutex_unlock( &thelock->mut ); |
| 280 | CHECK_STATUS("pthread_mutex_unlock[1]"); |
| 281 | |
| 282 | if ( !success && waitflag ) { |
| 283 | /* continue trying until we get the lock */ |
| 284 | |
| 285 | /* mut must be locked by me -- part of the condition |
| 286 | * protocol */ |
| 287 | status = pthread_mutex_lock( &thelock->mut ); |
| 288 | CHECK_STATUS("pthread_mutex_lock[2]"); |
| 289 | while ( thelock->locked ) { |
| 290 | status = pthread_cond_wait(&thelock->lock_released, |
| 291 | &thelock->mut); |
| 292 | CHECK_STATUS("pthread_cond_wait"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 293 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 294 | thelock->locked = 1; |
| 295 | status = pthread_mutex_unlock( &thelock->mut ); |
| 296 | CHECK_STATUS("pthread_mutex_unlock[2]"); |
| 297 | success = 1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 298 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 299 | if (error) success = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 300 | dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success)); |
| 301 | return success; |
| 302 | } |
| 303 | |
| 304 | void release_lock _P1(lock, type_lock lock) |
| 305 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 306 | pthread_lock *thelock = (pthread_lock *)lock; |
| 307 | int status, error = 0; |
| 308 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 309 | dprintf(("release_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 311 | status = pthread_mutex_lock( &thelock->mut ); |
| 312 | CHECK_STATUS("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 314 | thelock->locked = 0; |
| 315 | |
| 316 | status = pthread_mutex_unlock( &thelock->mut ); |
| 317 | CHECK_STATUS("pthread_mutex_unlock[3]"); |
| 318 | |
| 319 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 320 | status = pthread_cond_signal( &thelock->lock_released ); |
| 321 | CHECK_STATUS("pthread_cond_signal"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Semaphore support. |
| 326 | */ |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 327 | |
| 328 | struct semaphore { |
| 329 | pthread_mutex_t mutex; |
| 330 | pthread_cond_t cond; |
| 331 | int value; |
| 332 | }; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 334 | type_sema allocate_sema _P1(value, int value) |
| 335 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 336 | struct semaphore *sema; |
| 337 | int status, error = 0; |
| 338 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 339 | dprintf(("allocate_sema called\n")); |
| 340 | if (!initialized) |
| 341 | init_thread(); |
| 342 | |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 343 | sema = (struct semaphore *) malloc(sizeof(struct semaphore)); |
| 344 | if (sema != NULL) { |
| 345 | sema->value = value; |
| 346 | status = pthread_mutex_init(&sema->mutex, |
| 347 | pthread_mutexattr_default); |
| 348 | CHECK_STATUS("pthread_mutex_init"); |
| 349 | status = pthread_cond_init(&sema->cond, |
| 350 | pthread_condattr_default); |
| 351 | CHECK_STATUS("pthread_cond_init"); |
| 352 | if (error) { |
| 353 | free((void *) sema); |
| 354 | sema = NULL; |
| 355 | } |
| 356 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 357 | dprintf(("allocate_sema() -> %lx\n", (long) sema)); |
| 358 | return (type_sema) sema; |
| 359 | } |
| 360 | |
| 361 | void free_sema _P1(sema, type_sema sema) |
| 362 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 363 | int status, error = 0; |
| 364 | struct semaphore *thesema = (struct semaphore *) sema; |
| 365 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 366 | dprintf(("free_sema(%lx) called\n", (long) sema)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 367 | status = pthread_cond_destroy(&thesema->cond); |
| 368 | CHECK_STATUS("pthread_cond_destroy"); |
| 369 | status = pthread_mutex_destroy(&thesema->mutex); |
| 370 | CHECK_STATUS("pthread_mutex_destroy"); |
| 371 | free((void *) thesema); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 374 | int down_sema _P2(sema, type_sema sema, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 375 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 376 | int status, error = 0, success; |
| 377 | struct semaphore *thesema = (struct semaphore *) sema; |
| 378 | |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 379 | dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 380 | status = pthread_mutex_lock(&thesema->mutex); |
| 381 | CHECK_STATUS("pthread_mutex_lock"); |
| 382 | if (waitflag) { |
| 383 | while (!error && thesema->value <= 0) { |
| 384 | status = pthread_cond_wait(&thesema->cond, |
| 385 | &thesema->mutex); |
| 386 | CHECK_STATUS("pthread_cond_wait"); |
| 387 | } |
| 388 | } |
| 389 | if (error) |
| 390 | success = 0; |
| 391 | else if (thesema->value > 0) { |
| 392 | thesema->value--; |
| 393 | success = 1; |
| 394 | } |
| 395 | else |
| 396 | success = 0; |
| 397 | status = pthread_mutex_unlock(&thesema->mutex); |
| 398 | CHECK_STATUS("pthread_mutex_unlock"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 399 | dprintf(("down_sema(%lx) return\n", (long) sema)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 400 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void up_sema _P1(sema, type_sema sema) |
| 404 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 405 | int status, error = 0; |
| 406 | struct semaphore *thesema = (struct semaphore *) sema; |
| 407 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 408 | dprintf(("up_sema(%lx)\n", (long) sema)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 409 | status = pthread_mutex_lock(&thesema->mutex); |
| 410 | CHECK_STATUS("pthread_mutex_lock"); |
| 411 | thesema->value++; |
| 412 | status = pthread_cond_signal(&thesema->cond); |
| 413 | CHECK_STATUS("pthread_cond_signal"); |
| 414 | status = pthread_mutex_unlock(&thesema->mutex); |
| 415 | CHECK_STATUS("pthread_mutex_unlock"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 416 | } |