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