Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* GNU pth threads interface |
| 12 | http://www.gnu.org/software/pth |
| 13 | 2000-05-03 Andy Dustman <andy@dustman.net> |
| 14 | |
| 15 | Adapted from Posix threads interface |
| 16 | 12 May 1997 -- david arnold <davida@pobox.com> |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <pth.h> |
| 22 | |
| 23 | /* A pth mutex isn't sufficient to model the Python lock type |
| 24 | * because pth mutexes can be acquired multiple times by the |
| 25 | * same thread. |
| 26 | * |
| 27 | * The pth_lock struct implements a Python lock as a "locked?" bit |
| 28 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 29 | * instantly, it is, else the pair is used to block the thread until the |
| 30 | * bit is cleared. |
| 31 | */ |
| 32 | |
| 33 | typedef struct { |
| 34 | char locked; /* 0=unlocked, 1=locked */ |
| 35 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 36 | pth_cond_t lock_released; |
| 37 | pth_mutex_t mut; |
| 38 | } pth_lock; |
| 39 | |
| 40 | #define CHECK_STATUS(name) if (status == -1) { printf("%d ", status); perror(name); error = 1; } |
| 41 | |
| 42 | /* |
| 43 | * Initialization. |
| 44 | */ |
| 45 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 46 | static void PyThread__init_thread(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 47 | { |
| 48 | pth_init(); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Thread support. |
| 53 | */ |
| 54 | |
| 55 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 56 | int PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 57 | { |
| 58 | pth_t th; |
| 59 | int success; |
| 60 | dprintf(("PyThread_start_new_thread called\n")); |
| 61 | if (!initialized) |
| 62 | PyThread_init_thread(); |
| 63 | |
| 64 | th = pth_spawn(PTH_ATTR_DEFAULT, |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 65 | (void* (*)(void *))func, |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 66 | (void *)arg |
| 67 | ); |
| 68 | |
| 69 | return th == NULL ? 0 : 1; |
| 70 | } |
| 71 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 72 | long PyThread_get_thread_ident(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 73 | { |
| 74 | volatile pth_t threadid; |
| 75 | if (!initialized) |
| 76 | PyThread_init_thread(); |
| 77 | /* Jump through some hoops for Alpha OSF/1 */ |
| 78 | threadid = pth_self(); |
| 79 | return (long) *(long *) &threadid; |
| 80 | } |
| 81 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 82 | static void do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 83 | { |
| 84 | dprintf(("PyThread_exit_thread called\n")); |
| 85 | if (!initialized) { |
| 86 | if (no_cleanup) |
| 87 | _exit(0); |
| 88 | else |
| 89 | exit(0); |
| 90 | } |
| 91 | } |
| 92 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 93 | void PyThread_exit_thread(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 94 | { |
| 95 | do_PyThread_exit_thread(0); |
| 96 | } |
| 97 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 98 | void PyThread__exit_thread(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 99 | { |
| 100 | do_PyThread_exit_thread(1); |
| 101 | } |
| 102 | |
| 103 | #ifndef NO_EXIT_PROG |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 104 | static void do_PyThread_exit_prog(int status, int no_cleanup) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 105 | { |
| 106 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
| 107 | if (!initialized) |
| 108 | if (no_cleanup) |
| 109 | _exit(status); |
| 110 | else |
| 111 | exit(status); |
| 112 | } |
| 113 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 114 | void PyThread_exit_prog(int status) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 115 | { |
| 116 | do_PyThread_exit_prog(status, 0); |
| 117 | } |
| 118 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 119 | void PyThread__exit_prog(int status) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 120 | { |
| 121 | do_PyThread_exit_prog(status, 1); |
| 122 | } |
| 123 | #endif /* NO_EXIT_PROG */ |
| 124 | |
| 125 | /* |
| 126 | * Lock support. |
| 127 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 128 | PyThread_type_lock PyThread_allocate_lock(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 129 | { |
| 130 | pth_lock *lock; |
| 131 | int status, error = 0; |
| 132 | |
| 133 | dprintf(("PyThread_allocate_lock called\n")); |
| 134 | if (!initialized) |
| 135 | PyThread_init_thread(); |
| 136 | |
| 137 | lock = (pth_lock *) malloc(sizeof(pth_lock)); |
| 138 | memset((void *)lock, '\0', sizeof(pth_lock)); |
| 139 | if (lock) { |
| 140 | lock->locked = 0; |
| 141 | status = pth_mutex_init(&lock->mut); |
| 142 | CHECK_STATUS("pth_mutex_init"); |
| 143 | status = pth_cond_init(&lock->lock_released); |
| 144 | CHECK_STATUS("pth_cond_init"); |
| 145 | if (error) { |
| 146 | free((void *)lock); |
| 147 | lock = NULL; |
| 148 | } |
| 149 | } |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 150 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 151 | return (PyThread_type_lock) lock; |
| 152 | } |
| 153 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 154 | void PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 155 | { |
| 156 | pth_lock *thelock = (pth_lock *)lock; |
| 157 | int status, error = 0; |
| 158 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 159 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 160 | |
| 161 | free((void *)thelock); |
| 162 | } |
| 163 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 164 | int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 165 | { |
| 166 | int success; |
| 167 | pth_lock *thelock = (pth_lock *)lock; |
| 168 | int status, error = 0; |
| 169 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 170 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 171 | |
| 172 | status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL); |
| 173 | CHECK_STATUS("pth_mutex_acquire[1]"); |
| 174 | success = thelock->locked == 0; |
| 175 | if (success) thelock->locked = 1; |
| 176 | status = pth_mutex_release( &thelock->mut ); |
| 177 | CHECK_STATUS("pth_mutex_release[1]"); |
| 178 | |
| 179 | if ( !success && waitflag ) { |
| 180 | /* continue trying until we get the lock */ |
| 181 | |
| 182 | /* mut must be locked by me -- part of the condition |
| 183 | * protocol */ |
| 184 | status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL ); |
| 185 | CHECK_STATUS("pth_mutex_acquire[2]"); |
| 186 | while ( thelock->locked ) { |
| 187 | status = pth_cond_await(&thelock->lock_released, |
| 188 | &thelock->mut, NULL); |
| 189 | CHECK_STATUS("pth_cond_await"); |
| 190 | } |
| 191 | thelock->locked = 1; |
| 192 | status = pth_mutex_release( &thelock->mut ); |
| 193 | CHECK_STATUS("pth_mutex_release[2]"); |
| 194 | success = 1; |
| 195 | } |
| 196 | if (error) success = 0; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 197 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 198 | return success; |
| 199 | } |
| 200 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 201 | void PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 202 | { |
| 203 | pth_lock *thelock = (pth_lock *)lock; |
| 204 | int status, error = 0; |
| 205 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 206 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 207 | |
| 208 | status = pth_mutex_acquire( &thelock->mut, 0, NULL ); |
| 209 | CHECK_STATUS("pth_mutex_acquire[3]"); |
| 210 | |
| 211 | thelock->locked = 0; |
| 212 | |
| 213 | status = pth_mutex_release( &thelock->mut ); |
| 214 | CHECK_STATUS("pth_mutex_release[3]"); |
| 215 | |
| 216 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 217 | status = pth_cond_notify( &thelock->lock_released, 0 ); |
| 218 | CHECK_STATUS("pth_cond_notify"); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Semaphore support. |
| 223 | */ |
| 224 | |
| 225 | struct semaphore { |
| 226 | pth_mutex_t mutex; |
| 227 | pth_cond_t cond; |
| 228 | int value; |
| 229 | }; |
| 230 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 231 | PyThread_type_sema PyThread_allocate_sema(int value) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 232 | { |
| 233 | struct semaphore *sema; |
| 234 | int status, error = 0; |
| 235 | |
| 236 | dprintf(("PyThread_allocate_sema called\n")); |
| 237 | if (!initialized) |
| 238 | PyThread_init_thread(); |
| 239 | |
| 240 | sema = (struct semaphore *) malloc(sizeof(struct semaphore)); |
| 241 | if (sema != NULL) { |
| 242 | sema->value = value; |
| 243 | status = pth_mutex_init(&sema->mutex); |
| 244 | CHECK_STATUS("pth_mutex_init"); |
| 245 | status = pth_cond_init(&sema->cond); |
| 246 | CHECK_STATUS("pth_mutex_init"); |
| 247 | if (error) { |
| 248 | free((void *) sema); |
| 249 | sema = NULL; |
| 250 | } |
| 251 | } |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 252 | dprintf(("PyThread_allocate_sema() -> %p\n", sema)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 253 | return (PyThread_type_sema) sema; |
| 254 | } |
| 255 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 256 | void PyThread_free_sema(PyThread_type_sema sema) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 257 | { |
| 258 | int status, error = 0; |
| 259 | struct semaphore *thesema = (struct semaphore *) sema; |
| 260 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 261 | dprintf(("PyThread_free_sema(%p) called\n", sema)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 262 | free((void *) thesema); |
| 263 | } |
| 264 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 265 | int PyThread_down_sema(PyThread_type_sema sema, int waitflag) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 266 | { |
| 267 | int status, error = 0, success; |
| 268 | struct semaphore *thesema = (struct semaphore *) sema; |
| 269 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 270 | dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 271 | status = pth_mutex_acquire(&thesema->mutex, !waitflag, NULL); |
| 272 | CHECK_STATUS("pth_mutex_acquire"); |
| 273 | if (waitflag) { |
| 274 | while (!error && thesema->value <= 0) { |
| 275 | status = pth_cond_await(&thesema->cond, |
| 276 | &thesema->mutex, NULL); |
| 277 | CHECK_STATUS("pth_cond_await"); |
| 278 | } |
| 279 | } |
| 280 | if (error) |
| 281 | success = 0; |
| 282 | else if (thesema->value > 0) { |
| 283 | thesema->value--; |
| 284 | success = 1; |
| 285 | } |
| 286 | else |
| 287 | success = 0; |
| 288 | status = pth_mutex_release(&thesema->mutex); |
| 289 | CHECK_STATUS("pth_mutex_release"); |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 290 | dprintf(("PyThread_down_sema(%p) return\n", sema)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 291 | return success; |
| 292 | } |
| 293 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 294 | void PyThread_up_sema(PyThread_type_sema sema) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 295 | { |
| 296 | int status, error = 0; |
| 297 | struct semaphore *thesema = (struct semaphore *) sema; |
| 298 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 299 | dprintf(("PyThread_up_sema(%p)\n", sema)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 300 | status = pth_mutex_acquire(&thesema->mutex, 0, NULL); |
| 301 | CHECK_STATUS("pth_mutex_acquire"); |
| 302 | thesema->value++; |
| 303 | status = pth_cond_notify(&thesema->cond, 1); |
| 304 | CHECK_STATUS("pth_cond_notify"); |
| 305 | status = pth_mutex_release(&thesema->mutex); |
| 306 | CHECK_STATUS("pth_mutex_release"); |
| 307 | } |