Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +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 | 2c8cb9f | 1994-05-09 15:12:46 +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 | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 11 | /* Posix threads interface */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 14 | #include <string.h> |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 15 | #include <pthread.h> |
| 16 | |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 18 | /* try to determine what version of the Pthread Standard is installed. |
| 19 | * this is important, since all sorts of parameter types changed from |
| 20 | * draft to draft and there are several (incompatible) drafts in |
| 21 | * common use. these macros are a start, at least. |
| 22 | * 12 May 1997 -- david arnold <davida@pobox.com> |
| 23 | */ |
| 24 | |
| 25 | #if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_) |
| 26 | /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */ |
| 27 | # define PY_PTHREAD_D4 |
| 28 | |
| 29 | #elif defined(__osf__) && defined (__alpha) |
| 30 | /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */ |
| 31 | # if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4) |
| 32 | # define PY_PTHREAD_D4 |
| 33 | # else |
| 34 | # define PY_PTHREAD_STD |
| 35 | # endif |
| 36 | |
| 37 | #elif defined(_AIX) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 38 | /* SCHED_BG_NP is defined if using AIX DCE pthreads |
| 39 | * but it is unsupported by AIX 4 pthreads. Default |
| 40 | * attributes for AIX 4 pthreads equal to NULL. For |
| 41 | * AIX DCE pthreads they should be left unchanged. |
| 42 | */ |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 43 | # if !defined(SCHED_BG_NP) |
| 44 | # define PY_PTHREAD_STD |
| 45 | # else |
| 46 | # define PY_PTHREAD_D7 |
| 47 | # endif |
| 48 | |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame] | 49 | #elif defined(__DGUX) |
| 50 | # define PY_PTHREAD_D6 |
Guido van Rossum | 46ff190 | 1997-06-02 22:25:45 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 532246e | 1998-05-14 21:01:27 +0000 | [diff] [blame] | 52 | #elif defined(__hpux) && defined(_DECTHREADS_) |
Guido van Rossum | 89df70b | 1998-05-07 13:28:23 +0000 | [diff] [blame] | 53 | # define PY_PTHREAD_D4 |
| 54 | |
Guido van Rossum | 46ff190 | 1997-06-02 22:25:45 +0000 | [diff] [blame] | 55 | #else /* Default case */ |
| 56 | # define PY_PTHREAD_STD |
| 57 | |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 60 | |
| 61 | /* set default attribute object for different versions */ |
| 62 | |
| 63 | #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7) |
| 64 | # define pthread_attr_default pthread_attr_default |
| 65 | # define pthread_mutexattr_default pthread_mutexattr_default |
| 66 | # define pthread_condattr_default pthread_condattr_default |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame] | 67 | #elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 68 | # define pthread_attr_default ((pthread_attr_t *)NULL) |
| 69 | # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL) |
| 70 | # define pthread_condattr_default ((pthread_condattr_t *)NULL) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 71 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 74 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 75 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 76 | * following are undefined: |
| 77 | * -> a thread tries to lock a mutex it already has locked |
| 78 | * -> a thread tries to unlock a mutex locked by a different thread |
| 79 | * pthread mutexes are designed for serializing threads over short pieces |
| 80 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 81 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 82 | * |
| 83 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 84 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 85 | * instantly, it is, else the pair is used to block the thread until the |
| 86 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 87 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 89 | typedef struct { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 90 | char locked; /* 0=unlocked, 1=locked */ |
| 91 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 92 | pthread_cond_t lock_released; |
| 93 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 94 | } pthread_lock; |
| 95 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 96 | #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 98 | /* |
| 99 | * Initialization. |
| 100 | */ |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 101 | |
| 102 | #ifdef _HAVE_BSDI |
| 103 | static void _noop() |
| 104 | { |
| 105 | } |
| 106 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 107 | static void PyThread__init_thread _P0() |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 108 | { |
| 109 | /* DO AN INIT BY STARTING THE THREAD */ |
| 110 | static int dummy = 0; |
| 111 | pthread_t thread1; |
| 112 | pthread_create(&thread1, NULL, (void *) _noop, &dummy); |
| 113 | pthread_join(thread1, NULL); |
| 114 | } |
| 115 | |
| 116 | #else /* !_HAVE_BSDI */ |
| 117 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 118 | static void PyThread__init_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 120 | #if defined(_AIX) && defined(__GNUC__) |
| 121 | pthread_init(); |
| 122 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 125 | #endif /* !_HAVE_BSDI */ |
| 126 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 127 | /* |
| 128 | * Thread support. |
| 129 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 130 | |
| 131 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 132 | int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 133 | { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 134 | pthread_t th; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 135 | int success; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 136 | dprintf(("PyThread_start_new_thread called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 137 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 138 | PyThread_init_thread(); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 139 | |
| 140 | success = pthread_create(&th, |
| 141 | #if defined(PY_PTHREAD_D4) |
| 142 | pthread_attr_default, |
| 143 | (pthread_startroutine_t)func, |
| 144 | (pthread_addr_t)arg |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame] | 145 | #elif defined(PY_PTHREAD_D6) |
| 146 | pthread_attr_default, |
| 147 | (void* (*)_P((void *)))func, |
| 148 | arg |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 149 | #elif defined(PY_PTHREAD_D7) |
| 150 | pthread_attr_default, |
| 151 | func, |
| 152 | arg |
| 153 | #elif defined(PY_PTHREAD_STD) |
| 154 | (pthread_attr_t*)NULL, |
| 155 | (void* (*)_P((void *)))func, |
| 156 | (void *)arg |
| 157 | #endif |
| 158 | ); |
| 159 | |
Guido van Rossum | 701f25e | 1999-03-15 20:27:53 +0000 | [diff] [blame] | 160 | if (success == 0) { |
Guido van Rossum | a74d0e4 | 1998-09-04 13:38:32 +0000 | [diff] [blame] | 161 | #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 162 | pthread_detach(&th); |
| 163 | #elif defined(PY_PTHREAD_STD) |
Guido van Rossum | f4806c2 | 1997-04-30 19:59:22 +0000 | [diff] [blame] | 164 | pthread_detach(th); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 165 | #endif |
| 166 | } |
Guido van Rossum | 701f25e | 1999-03-15 20:27:53 +0000 | [diff] [blame] | 167 | return success != 0 ? 0 : 1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 170 | long PyThread_get_thread_ident _P0() |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 171 | { |
Guido van Rossum | 44ee479 | 1998-08-27 19:21:53 +0000 | [diff] [blame] | 172 | volatile pthread_t threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 173 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 174 | PyThread_init_thread(); |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 175 | /* Jump through some hoops for Alpha OSF/1 */ |
| 176 | threadid = pthread_self(); |
| 177 | return (long) *(long *) &threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 180 | static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 181 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 182 | dprintf(("PyThread_exit_thread called\n")); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 183 | if (!initialized) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 184 | if (no_cleanup) |
| 185 | _exit(0); |
| 186 | else |
| 187 | exit(0); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 188 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 191 | void PyThread_exit_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 193 | do_PyThread_exit_thread(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 196 | void PyThread__exit_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 197 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 198 | do_PyThread_exit_thread(1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | #ifndef NO_EXIT_PROG |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 202 | static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 203 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 204 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 205 | if (!initialized) |
| 206 | if (no_cleanup) |
| 207 | _exit(status); |
| 208 | else |
| 209 | exit(status); |
| 210 | } |
| 211 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 212 | void PyThread_exit_prog _P1(status, int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 213 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 214 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 217 | void PyThread__exit_prog _P1(status, int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 218 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 219 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 220 | } |
| 221 | #endif /* NO_EXIT_PROG */ |
| 222 | |
| 223 | /* |
| 224 | * Lock support. |
| 225 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 226 | PyThread_type_lock PyThread_allocate_lock _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 227 | { |
| 228 | pthread_lock *lock; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 229 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 231 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 232 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 233 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 234 | |
| 235 | lock = (pthread_lock *) malloc(sizeof(pthread_lock)); |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 236 | memset((void *)lock, '\0', sizeof(pthread_lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 237 | if (lock) { |
| 238 | lock->locked = 0; |
| 239 | |
| 240 | status = pthread_mutex_init(&lock->mut, |
| 241 | pthread_mutexattr_default); |
| 242 | CHECK_STATUS("pthread_mutex_init"); |
| 243 | |
| 244 | status = pthread_cond_init(&lock->lock_released, |
| 245 | pthread_condattr_default); |
| 246 | CHECK_STATUS("pthread_cond_init"); |
| 247 | |
| 248 | if (error) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 249 | free((void *)lock); |
| 250 | lock = 0; |
| 251 | } |
| 252 | } |
| 253 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 254 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 255 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 258 | void PyThread_free_lock _P1(lock, PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 259 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 260 | pthread_lock *thelock = (pthread_lock *)lock; |
| 261 | int status, error = 0; |
| 262 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 263 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 264 | |
| 265 | status = pthread_mutex_destroy( &thelock->mut ); |
| 266 | CHECK_STATUS("pthread_mutex_destroy"); |
| 267 | |
| 268 | status = pthread_cond_destroy( &thelock->lock_released ); |
| 269 | CHECK_STATUS("pthread_cond_destroy"); |
| 270 | |
| 271 | free((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 274 | int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 275 | { |
| 276 | int success; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 277 | pthread_lock *thelock = (pthread_lock *)lock; |
| 278 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 279 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 280 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 282 | status = pthread_mutex_lock( &thelock->mut ); |
| 283 | CHECK_STATUS("pthread_mutex_lock[1]"); |
| 284 | success = thelock->locked == 0; |
| 285 | if (success) thelock->locked = 1; |
| 286 | status = pthread_mutex_unlock( &thelock->mut ); |
| 287 | CHECK_STATUS("pthread_mutex_unlock[1]"); |
| 288 | |
| 289 | if ( !success && waitflag ) { |
| 290 | /* continue trying until we get the lock */ |
| 291 | |
| 292 | /* mut must be locked by me -- part of the condition |
| 293 | * protocol */ |
| 294 | status = pthread_mutex_lock( &thelock->mut ); |
| 295 | CHECK_STATUS("pthread_mutex_lock[2]"); |
| 296 | while ( thelock->locked ) { |
| 297 | status = pthread_cond_wait(&thelock->lock_released, |
| 298 | &thelock->mut); |
| 299 | CHECK_STATUS("pthread_cond_wait"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 300 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 301 | thelock->locked = 1; |
| 302 | status = pthread_mutex_unlock( &thelock->mut ); |
| 303 | CHECK_STATUS("pthread_mutex_unlock[2]"); |
| 304 | success = 1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 305 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 306 | if (error) success = 0; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 307 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 308 | return success; |
| 309 | } |
| 310 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 311 | void PyThread_release_lock _P1(lock, PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 312 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 313 | pthread_lock *thelock = (pthread_lock *)lock; |
| 314 | int status, error = 0; |
| 315 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 316 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 318 | status = pthread_mutex_lock( &thelock->mut ); |
| 319 | CHECK_STATUS("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 321 | thelock->locked = 0; |
| 322 | |
| 323 | status = pthread_mutex_unlock( &thelock->mut ); |
| 324 | CHECK_STATUS("pthread_mutex_unlock[3]"); |
| 325 | |
| 326 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 327 | status = pthread_cond_signal( &thelock->lock_released ); |
| 328 | CHECK_STATUS("pthread_cond_signal"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Semaphore support. |
| 333 | */ |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 334 | |
| 335 | struct semaphore { |
| 336 | pthread_mutex_t mutex; |
| 337 | pthread_cond_t cond; |
| 338 | int value; |
| 339 | }; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 341 | PyThread_type_sema PyThread_allocate_sema _P1(value, int value) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 342 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 343 | struct semaphore *sema; |
| 344 | int status, error = 0; |
| 345 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 346 | dprintf(("PyThread_allocate_sema called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 347 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 348 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 350 | sema = (struct semaphore *) malloc(sizeof(struct semaphore)); |
| 351 | if (sema != NULL) { |
| 352 | sema->value = value; |
| 353 | status = pthread_mutex_init(&sema->mutex, |
| 354 | pthread_mutexattr_default); |
| 355 | CHECK_STATUS("pthread_mutex_init"); |
| 356 | status = pthread_cond_init(&sema->cond, |
| 357 | pthread_condattr_default); |
| 358 | CHECK_STATUS("pthread_cond_init"); |
| 359 | if (error) { |
| 360 | free((void *) sema); |
| 361 | sema = NULL; |
| 362 | } |
| 363 | } |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 364 | dprintf(("PyThread_allocate_sema() -> %p\n", sema)); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 365 | return (PyThread_type_sema) sema; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 368 | void PyThread_free_sema _P1(sema, PyThread_type_sema sema) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 369 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 370 | int status, error = 0; |
| 371 | struct semaphore *thesema = (struct semaphore *) sema; |
| 372 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 373 | dprintf(("PyThread_free_sema(%p) called\n", sema)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 374 | status = pthread_cond_destroy(&thesema->cond); |
| 375 | CHECK_STATUS("pthread_cond_destroy"); |
| 376 | status = pthread_mutex_destroy(&thesema->mutex); |
| 377 | CHECK_STATUS("pthread_mutex_destroy"); |
| 378 | free((void *) thesema); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 381 | int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 382 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 383 | int status, error = 0, success; |
| 384 | struct semaphore *thesema = (struct semaphore *) sema; |
| 385 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 386 | dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 387 | status = pthread_mutex_lock(&thesema->mutex); |
| 388 | CHECK_STATUS("pthread_mutex_lock"); |
| 389 | if (waitflag) { |
| 390 | while (!error && thesema->value <= 0) { |
| 391 | status = pthread_cond_wait(&thesema->cond, |
| 392 | &thesema->mutex); |
| 393 | CHECK_STATUS("pthread_cond_wait"); |
| 394 | } |
| 395 | } |
| 396 | if (error) |
| 397 | success = 0; |
| 398 | else if (thesema->value > 0) { |
| 399 | thesema->value--; |
| 400 | success = 1; |
| 401 | } |
| 402 | else |
| 403 | success = 0; |
| 404 | status = pthread_mutex_unlock(&thesema->mutex); |
| 405 | CHECK_STATUS("pthread_mutex_unlock"); |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 406 | dprintf(("PyThread_down_sema(%p) return\n", sema)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 407 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 410 | void PyThread_up_sema _P1(sema, PyThread_type_sema sema) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 411 | { |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 412 | int status, error = 0; |
| 413 | struct semaphore *thesema = (struct semaphore *) sema; |
| 414 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 415 | dprintf(("PyThread_up_sema(%p)\n", sema)); |
Guido van Rossum | 7af8130 | 1997-01-17 21:06:41 +0000 | [diff] [blame] | 416 | status = pthread_mutex_lock(&thesema->mutex); |
| 417 | CHECK_STATUS("pthread_mutex_lock"); |
| 418 | thesema->value++; |
| 419 | status = pthread_cond_signal(&thesema->cond); |
| 420 | CHECK_STATUS("pthread_cond_signal"); |
| 421 | status = pthread_mutex_unlock(&thesema->mutex); |
| 422 | CHECK_STATUS("pthread_mutex_unlock"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 423 | } |