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