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 | #ifdef _AIX |
| 38 | |
| 39 | #ifndef SCHED_BG_NP |
| 40 | /* SCHED_BG_NP is defined if using AIX DCE pthreads |
| 41 | * but it is unsupported by AIX 4 pthreads. Default |
| 42 | * attributes for AIX 4 pthreads equal to NULL. For |
| 43 | * AIX DCE pthreads they should be left unchanged. |
| 44 | */ |
| 45 | #define pthread_attr_default NULL |
| 46 | #define pthread_mutexattr_default NULL |
| 47 | #define pthread_condattr_default NULL |
| 48 | #endif |
| 49 | |
| 50 | #else |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 51 | #define pthread_attr_default ((pthread_attr_t *)0) |
| 52 | #define pthread_mutexattr_default ((pthread_mutexattr_t *)0) |
| 53 | #define pthread_condattr_default ((pthread_condattr_t *)0) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 54 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 56 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 57 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 58 | * following are undefined: |
| 59 | * -> a thread tries to lock a mutex it already has locked |
| 60 | * -> a thread tries to unlock a mutex locked by a different thread |
| 61 | * pthread mutexes are designed for serializing threads over short pieces |
| 62 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 63 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 64 | * |
| 65 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 66 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 67 | * instantly, it is, else the pair is used to block the thread until the |
| 68 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 69 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 71 | typedef struct { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 72 | char locked; /* 0=unlocked, 1=locked */ |
| 73 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 74 | pthread_cond_t lock_released; |
| 75 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 76 | } pthread_lock; |
| 77 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 78 | #define CHECK_STATUS(name) if (status < 0) { perror(name); error=1; } |
| 79 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 80 | /* |
| 81 | * Initialization. |
| 82 | */ |
| 83 | static void _init_thread _P0() |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Thread support. |
| 89 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 90 | |
| 91 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 92 | int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
| 93 | { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 94 | pthread_t th; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 95 | int success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 96 | dprintf(("start_new_thread called\n")); |
| 97 | if (!initialized) |
| 98 | init_thread(); |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 99 | success = pthread_create(&th, pthread_attr_default, |
| 100 | (void* (*) _P((void *)))func, arg); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 101 | return success < 0 ? 0 : 1; |
| 102 | } |
| 103 | |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 104 | long get_thread_ident _P0() |
| 105 | { |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 106 | pthread_t threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 107 | if (!initialized) |
| 108 | init_thread(); |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 109 | /* Jump through some hoops for Alpha OSF/1 */ |
| 110 | threadid = pthread_self(); |
| 111 | return (long) *(long *) &threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 114 | static void do_exit_thread _P1(no_cleanup, int no_cleanup) |
| 115 | { |
| 116 | dprintf(("exit_thread called\n")); |
| 117 | if (!initialized) |
| 118 | if (no_cleanup) |
| 119 | _exit(0); |
| 120 | else |
| 121 | exit(0); |
| 122 | } |
| 123 | |
| 124 | void exit_thread _P0() |
| 125 | { |
| 126 | do_exit_thread(0); |
| 127 | } |
| 128 | |
| 129 | void _exit_thread _P0() |
| 130 | { |
| 131 | do_exit_thread(1); |
| 132 | } |
| 133 | |
| 134 | #ifndef NO_EXIT_PROG |
| 135 | static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) |
| 136 | { |
| 137 | dprintf(("exit_prog(%d) called\n", status)); |
| 138 | if (!initialized) |
| 139 | if (no_cleanup) |
| 140 | _exit(status); |
| 141 | else |
| 142 | exit(status); |
| 143 | } |
| 144 | |
| 145 | void exit_prog _P1(status, int status) |
| 146 | { |
| 147 | do_exit_prog(status, 0); |
| 148 | } |
| 149 | |
| 150 | void _exit_prog _P1(status, int status) |
| 151 | { |
| 152 | do_exit_prog(status, 1); |
| 153 | } |
| 154 | #endif /* NO_EXIT_PROG */ |
| 155 | |
| 156 | /* |
| 157 | * Lock support. |
| 158 | */ |
| 159 | type_lock allocate_lock _P0() |
| 160 | { |
| 161 | pthread_lock *lock; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 162 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 163 | |
| 164 | dprintf(("allocate_lock called\n")); |
| 165 | if (!initialized) |
| 166 | init_thread(); |
| 167 | |
| 168 | lock = (pthread_lock *) malloc(sizeof(pthread_lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 169 | if (lock) { |
| 170 | lock->locked = 0; |
| 171 | |
| 172 | status = pthread_mutex_init(&lock->mut, |
| 173 | pthread_mutexattr_default); |
| 174 | CHECK_STATUS("pthread_mutex_init"); |
| 175 | |
| 176 | status = pthread_cond_init(&lock->lock_released, |
| 177 | pthread_condattr_default); |
| 178 | CHECK_STATUS("pthread_cond_init"); |
| 179 | |
| 180 | if (error) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 181 | free((void *)lock); |
| 182 | lock = 0; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | dprintf(("allocate_lock() -> %lx\n", (long)lock)); |
| 187 | return (type_lock) lock; |
| 188 | } |
| 189 | |
| 190 | void free_lock _P1(lock, type_lock lock) |
| 191 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 192 | pthread_lock *thelock = (pthread_lock *)lock; |
| 193 | int status, error = 0; |
| 194 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 195 | dprintf(("free_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 196 | |
| 197 | status = pthread_mutex_destroy( &thelock->mut ); |
| 198 | CHECK_STATUS("pthread_mutex_destroy"); |
| 199 | |
| 200 | status = pthread_cond_destroy( &thelock->lock_released ); |
| 201 | CHECK_STATUS("pthread_cond_destroy"); |
| 202 | |
| 203 | free((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag) |
| 207 | { |
| 208 | int success; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 209 | pthread_lock *thelock = (pthread_lock *)lock; |
| 210 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 211 | |
| 212 | dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 214 | status = pthread_mutex_lock( &thelock->mut ); |
| 215 | CHECK_STATUS("pthread_mutex_lock[1]"); |
| 216 | success = thelock->locked == 0; |
| 217 | if (success) thelock->locked = 1; |
| 218 | status = pthread_mutex_unlock( &thelock->mut ); |
| 219 | CHECK_STATUS("pthread_mutex_unlock[1]"); |
| 220 | |
| 221 | if ( !success && waitflag ) { |
| 222 | /* continue trying until we get the lock */ |
| 223 | |
| 224 | /* mut must be locked by me -- part of the condition |
| 225 | * protocol */ |
| 226 | status = pthread_mutex_lock( &thelock->mut ); |
| 227 | CHECK_STATUS("pthread_mutex_lock[2]"); |
| 228 | while ( thelock->locked ) { |
| 229 | status = pthread_cond_wait(&thelock->lock_released, |
| 230 | &thelock->mut); |
| 231 | CHECK_STATUS("pthread_cond_wait"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 232 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 233 | thelock->locked = 1; |
| 234 | status = pthread_mutex_unlock( &thelock->mut ); |
| 235 | CHECK_STATUS("pthread_mutex_unlock[2]"); |
| 236 | success = 1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 237 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 238 | if (error) success = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 239 | dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success)); |
| 240 | return success; |
| 241 | } |
| 242 | |
| 243 | void release_lock _P1(lock, type_lock lock) |
| 244 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 245 | pthread_lock *thelock = (pthread_lock *)lock; |
| 246 | int status, error = 0; |
| 247 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 248 | dprintf(("release_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 250 | status = pthread_mutex_lock( &thelock->mut ); |
| 251 | CHECK_STATUS("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 253 | thelock->locked = 0; |
| 254 | |
| 255 | status = pthread_mutex_unlock( &thelock->mut ); |
| 256 | CHECK_STATUS("pthread_mutex_unlock[3]"); |
| 257 | |
| 258 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 259 | status = pthread_cond_signal( &thelock->lock_released ); |
| 260 | CHECK_STATUS("pthread_cond_signal"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Semaphore support. |
| 265 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 266 | /* NOTE: 100% non-functional at this time - tim */ |
| 267 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 268 | type_sema allocate_sema _P1(value, int value) |
| 269 | { |
| 270 | char *sema = 0; |
| 271 | dprintf(("allocate_sema called\n")); |
| 272 | if (!initialized) |
| 273 | init_thread(); |
| 274 | |
| 275 | dprintf(("allocate_sema() -> %lx\n", (long) sema)); |
| 276 | return (type_sema) sema; |
| 277 | } |
| 278 | |
| 279 | void free_sema _P1(sema, type_sema sema) |
| 280 | { |
| 281 | dprintf(("free_sema(%lx) called\n", (long) sema)); |
| 282 | } |
| 283 | |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 284 | int down_sema _P2(sema, type_sema sema, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 285 | { |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 286 | dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 287 | dprintf(("down_sema(%lx) return\n", (long) sema)); |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 288 | return -1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void up_sema _P1(sema, type_sema sema) |
| 292 | { |
| 293 | dprintf(("up_sema(%lx)\n", (long) sema)); |
| 294 | } |