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