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> |
Martin v. Löwis | a7a76d3 | 2002-10-04 07:21:24 +0000 | [diff] [blame] | 6 | #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR) |
Jack Jansen | 7668957 | 2002-01-15 20:36:14 +0000 | [diff] [blame] | 7 | #define destructor xxdestructor |
| 8 | #endif |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 9 | #include <pthread.h> |
Martin v. Löwis | a7a76d3 | 2002-10-04 07:21:24 +0000 | [diff] [blame] | 10 | #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR) |
Jack Jansen | 7668957 | 2002-01-15 20:36:14 +0000 | [diff] [blame] | 11 | #undef destructor |
| 12 | #endif |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 13 | #include <signal.h> |
Martin v. Löwis | 42ab61e | 2002-03-17 17:19:00 +0000 | [diff] [blame] | 14 | |
| 15 | /* The POSIX spec says that implementations supporting the sem_* |
| 16 | family of functions must indicate this by defining |
| 17 | _POSIX_SEMAPHORES. */ |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 18 | #ifdef _POSIX_SEMAPHORES |
| 19 | #include <semaphore.h> |
| 20 | #include <errno.h> |
| 21 | #endif |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 22 | |
Martin v. Löwis | b023381 | 2002-12-11 13:12:30 +0000 | [diff] [blame] | 23 | #if !defined(pthread_attr_default) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 24 | # define pthread_attr_default ((pthread_attr_t *)NULL) |
Martin v. Löwis | b023381 | 2002-12-11 13:12:30 +0000 | [diff] [blame] | 25 | #endif |
| 26 | #if !defined(pthread_mutexattr_default) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 27 | # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL) |
Martin v. Löwis | b023381 | 2002-12-11 13:12:30 +0000 | [diff] [blame] | 28 | #endif |
| 29 | #if !defined(pthread_condattr_default) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 30 | # define pthread_condattr_default ((pthread_condattr_t *)NULL) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 31 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 33 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 34 | /* Whether or not to use semaphores directly rather than emulating them with |
| 35 | * mutexes and condition variables: |
| 36 | */ |
Martin v. Löwis | dfc33fd | 2003-01-21 10:14:41 +0000 | [diff] [blame] | 37 | #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 38 | # define USE_SEMAPHORES |
| 39 | #else |
| 40 | # undef USE_SEMAPHORES |
| 41 | #endif |
| 42 | |
| 43 | |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 44 | /* On platforms that don't use standard POSIX threads pthread_sigmask() |
| 45 | * isn't present. DEC threads uses sigprocmask() instead as do most |
| 46 | * other UNIX International compliant systems that don't have the full |
| 47 | * pthread implementation. |
| 48 | */ |
Jason Tishler | fac083d | 2003-07-22 15:20:49 +0000 | [diff] [blame] | 49 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 50 | # define SET_THREAD_SIGMASK pthread_sigmask |
| 51 | #else |
| 52 | # define SET_THREAD_SIGMASK sigprocmask |
| 53 | #endif |
| 54 | |
| 55 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 56 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 57 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 58 | * following are undefined: |
| 59 | * -> a thread tries to lock a mutex it already has locked |
| 60 | * -> a thread tries to unlock a mutex locked by a different thread |
| 61 | * pthread mutexes are designed for serializing threads over short pieces |
| 62 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 63 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 64 | * |
| 65 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 66 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 67 | * instantly, it is, else the pair is used to block the thread until the |
| 68 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 69 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 71 | typedef struct { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 72 | char locked; /* 0=unlocked, 1=locked */ |
| 73 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 74 | pthread_cond_t lock_released; |
| 75 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 76 | } pthread_lock; |
| 77 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 78 | #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 80 | /* |
| 81 | * Initialization. |
| 82 | */ |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 83 | |
| 84 | #ifdef _HAVE_BSDI |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 85 | static |
| 86 | void _noop(void) |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 90 | static void |
| 91 | PyThread__init_thread(void) |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 92 | { |
| 93 | /* DO AN INIT BY STARTING THE THREAD */ |
| 94 | static int dummy = 0; |
| 95 | pthread_t thread1; |
| 96 | pthread_create(&thread1, NULL, (void *) _noop, &dummy); |
| 97 | pthread_join(thread1, NULL); |
| 98 | } |
| 99 | |
| 100 | #else /* !_HAVE_BSDI */ |
| 101 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 102 | static void |
| 103 | PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 104 | { |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 105 | #if defined(_AIX) && defined(__GNUC__) |
| 106 | pthread_init(); |
| 107 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 110 | #endif /* !_HAVE_BSDI */ |
| 111 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 112 | /* |
| 113 | * Thread support. |
| 114 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 115 | |
| 116 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 117 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 118 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 120 | pthread_t th; |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 121 | int status; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 122 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 123 | pthread_attr_t attrs; |
| 124 | #endif |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 125 | dprintf(("PyThread_start_new_thread called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 126 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 127 | PyThread_init_thread(); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 129 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 130 | pthread_attr_init(&attrs); |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 131 | #endif |
| 132 | #ifdef THREAD_STACK_SIZE |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 133 | pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE); |
| 134 | #endif |
Hye-Shik Chang | 30e97db | 2004-03-04 06:35:57 +0000 | [diff] [blame] | 135 | #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) && !defined(__FreeBSD__) |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 136 | pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); |
| 137 | #endif |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 138 | |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 139 | status = pthread_create(&th, |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 140 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 141 | &attrs, |
| 142 | #else |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 143 | (pthread_attr_t*)NULL, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 144 | #endif |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 145 | (void* (*)(void *))func, |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 146 | (void *)arg |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 147 | ); |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 148 | |
Fred Drake | 03459a5 | 2001-11-09 16:00:41 +0000 | [diff] [blame] | 149 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 150 | pthread_attr_destroy(&attrs); |
| 151 | #endif |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 152 | if (status != 0) |
| 153 | return -1; |
| 154 | |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 155 | pthread_detach(th); |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 157 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG |
| 158 | return (long) th; |
| 159 | #else |
| 160 | return (long) *(long *) &th; |
| 161 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 164 | /* XXX This implementation is considered (to quote Tim Peters) "inherently |
| 165 | hosed" because: |
Skip Montanaro | 6babcc2 | 2004-03-03 08:42:23 +0000 | [diff] [blame] | 166 | - It does not guarantee the promise that a non-zero integer is returned. |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 167 | - The cast to long is inherently unsafe. |
| 168 | - It is not clear that the 'volatile' (for AIX?) and ugly casting in the |
| 169 | latter return statement (for Alpha OSF/1) are any longer necessary. |
| 170 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 171 | long |
| 172 | PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 173 | { |
Guido van Rossum | 44ee479 | 1998-08-27 19:21:53 +0000 | [diff] [blame] | 174 | volatile pthread_t threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 175 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 176 | PyThread_init_thread(); |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 177 | /* Jump through some hoops for Alpha OSF/1 */ |
| 178 | threadid = pthread_self(); |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 179 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG |
| 180 | return (long) threadid; |
| 181 | #else |
Guido van Rossum | 2565bff | 1995-01-09 17:50:47 +0000 | [diff] [blame] | 182 | return (long) *(long *) &threadid; |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 183 | #endif |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 186 | static void |
| 187 | do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 188 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 189 | dprintf(("PyThread_exit_thread called\n")); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 190 | if (!initialized) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 191 | if (no_cleanup) |
| 192 | _exit(0); |
| 193 | else |
| 194 | exit(0); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 195 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 198 | void |
| 199 | PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 200 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 201 | do_PyThread_exit_thread(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 204 | void |
| 205 | PyThread__exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 206 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 207 | do_PyThread_exit_thread(1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | #ifndef NO_EXIT_PROG |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 211 | static void |
| 212 | do_PyThread_exit_prog(int status, int no_cleanup) |
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 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 215 | if (!initialized) |
| 216 | if (no_cleanup) |
| 217 | _exit(status); |
| 218 | else |
| 219 | exit(status); |
| 220 | } |
| 221 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 222 | void |
| 223 | PyThread_exit_prog(int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 224 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 225 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 228 | void |
| 229 | PyThread__exit_prog(int status) |
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 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 232 | } |
| 233 | #endif /* NO_EXIT_PROG */ |
| 234 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 235 | #ifdef USE_SEMAPHORES |
| 236 | |
| 237 | /* |
| 238 | * Lock support. |
| 239 | */ |
| 240 | |
| 241 | PyThread_type_lock |
| 242 | PyThread_allocate_lock(void) |
| 243 | { |
| 244 | sem_t *lock; |
| 245 | int status, error = 0; |
| 246 | |
| 247 | dprintf(("PyThread_allocate_lock called\n")); |
| 248 | if (!initialized) |
| 249 | PyThread_init_thread(); |
| 250 | |
| 251 | lock = (sem_t *)malloc(sizeof(sem_t)); |
| 252 | |
| 253 | if (lock) { |
| 254 | status = sem_init(lock,0,1); |
| 255 | CHECK_STATUS("sem_init"); |
| 256 | |
| 257 | if (error) { |
| 258 | free((void *)lock); |
| 259 | lock = NULL; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 264 | return (PyThread_type_lock)lock; |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | PyThread_free_lock(PyThread_type_lock lock) |
| 269 | { |
| 270 | sem_t *thelock = (sem_t *)lock; |
| 271 | int status, error = 0; |
| 272 | |
| 273 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
| 274 | |
| 275 | if (!thelock) |
| 276 | return; |
| 277 | |
| 278 | status = sem_destroy(thelock); |
| 279 | CHECK_STATUS("sem_destroy"); |
| 280 | |
| 281 | free((void *)thelock); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * As of February 2002, Cygwin thread implementations mistakenly report error |
| 286 | * codes in the return value of the sem_ calls (like the pthread_ functions). |
| 287 | * Correct implementations return -1 and put the code in errno. This supports |
| 288 | * either. |
| 289 | */ |
| 290 | static int |
| 291 | fix_status(int status) |
| 292 | { |
| 293 | return (status == -1) ? errno : status; |
| 294 | } |
| 295 | |
| 296 | int |
| 297 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
| 298 | { |
| 299 | int success; |
| 300 | sem_t *thelock = (sem_t *)lock; |
| 301 | int status, error = 0; |
| 302 | |
| 303 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
| 304 | |
| 305 | do { |
| 306 | if (waitflag) |
| 307 | status = fix_status(sem_wait(thelock)); |
| 308 | else |
| 309 | status = fix_status(sem_trywait(thelock)); |
| 310 | } while (status == EINTR); /* Retry if interrupted by a signal */ |
| 311 | |
| 312 | if (waitflag) { |
| 313 | CHECK_STATUS("sem_wait"); |
| 314 | } else if (status != EAGAIN) { |
| 315 | CHECK_STATUS("sem_trywait"); |
| 316 | } |
| 317 | |
| 318 | success = (status == 0) ? 1 : 0; |
| 319 | |
| 320 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 321 | return success; |
| 322 | } |
| 323 | |
| 324 | void |
| 325 | PyThread_release_lock(PyThread_type_lock lock) |
| 326 | { |
| 327 | sem_t *thelock = (sem_t *)lock; |
| 328 | int status, error = 0; |
| 329 | |
| 330 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
| 331 | |
| 332 | status = sem_post(thelock); |
| 333 | CHECK_STATUS("sem_post"); |
| 334 | } |
| 335 | |
| 336 | #else /* USE_SEMAPHORES */ |
| 337 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 338 | /* |
| 339 | * Lock support. |
| 340 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 341 | PyThread_type_lock |
| 342 | PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 343 | { |
| 344 | pthread_lock *lock; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 345 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 347 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 348 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 349 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 350 | |
| 351 | lock = (pthread_lock *) malloc(sizeof(pthread_lock)); |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 352 | memset((void *)lock, '\0', sizeof(pthread_lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 353 | if (lock) { |
| 354 | lock->locked = 0; |
| 355 | |
| 356 | status = pthread_mutex_init(&lock->mut, |
| 357 | pthread_mutexattr_default); |
| 358 | CHECK_STATUS("pthread_mutex_init"); |
| 359 | |
| 360 | status = pthread_cond_init(&lock->lock_released, |
| 361 | pthread_condattr_default); |
| 362 | CHECK_STATUS("pthread_cond_init"); |
| 363 | |
| 364 | if (error) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 365 | free((void *)lock); |
| 366 | lock = 0; |
| 367 | } |
| 368 | } |
| 369 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 370 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 371 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 374 | void |
| 375 | PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 376 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 377 | pthread_lock *thelock = (pthread_lock *)lock; |
| 378 | int status, error = 0; |
| 379 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 380 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 381 | |
| 382 | status = pthread_mutex_destroy( &thelock->mut ); |
| 383 | CHECK_STATUS("pthread_mutex_destroy"); |
| 384 | |
| 385 | status = pthread_cond_destroy( &thelock->lock_released ); |
| 386 | CHECK_STATUS("pthread_cond_destroy"); |
| 387 | |
| 388 | free((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 391 | int |
| 392 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 393 | { |
| 394 | int success; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 395 | pthread_lock *thelock = (pthread_lock *)lock; |
| 396 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 397 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 398 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 400 | status = pthread_mutex_lock( &thelock->mut ); |
| 401 | CHECK_STATUS("pthread_mutex_lock[1]"); |
| 402 | success = thelock->locked == 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 403 | |
| 404 | if ( !success && waitflag ) { |
| 405 | /* continue trying until we get the lock */ |
| 406 | |
| 407 | /* mut must be locked by me -- part of the condition |
| 408 | * protocol */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 409 | while ( thelock->locked ) { |
| 410 | status = pthread_cond_wait(&thelock->lock_released, |
| 411 | &thelock->mut); |
| 412 | CHECK_STATUS("pthread_cond_wait"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 413 | } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 414 | success = 1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 415 | } |
Martin v. Löwis | 1509a15 | 2003-04-18 11:11:09 +0000 | [diff] [blame] | 416 | if (success) thelock->locked = 1; |
| 417 | status = pthread_mutex_unlock( &thelock->mut ); |
| 418 | CHECK_STATUS("pthread_mutex_unlock[1]"); |
| 419 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 420 | if (error) success = 0; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 421 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 422 | return success; |
| 423 | } |
| 424 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 425 | void |
| 426 | PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 427 | { |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 428 | pthread_lock *thelock = (pthread_lock *)lock; |
| 429 | int status, error = 0; |
| 430 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 431 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 433 | status = pthread_mutex_lock( &thelock->mut ); |
| 434 | CHECK_STATUS("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 436 | thelock->locked = 0; |
| 437 | |
| 438 | status = pthread_mutex_unlock( &thelock->mut ); |
| 439 | CHECK_STATUS("pthread_mutex_unlock[3]"); |
| 440 | |
| 441 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 442 | status = pthread_cond_signal( &thelock->lock_released ); |
| 443 | CHECK_STATUS("pthread_cond_signal"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 444 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 445 | |
| 446 | #endif /* USE_SEMAPHORES */ |