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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 15 | /* The POSIX spec requires that use of pthread_attr_setstacksize |
| 16 | be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */ |
| 17 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 18 | #ifndef THREAD_STACK_SIZE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | #define THREAD_STACK_SIZE 0 /* use default stack size */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | #endif |
| 21 | /* for safety, ensure a viable minimum stacksize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | #define THREAD_STACK_MIN 0x8000 /* 32kB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 23 | #else /* !_POSIX_THREAD_ATTR_STACKSIZE */ |
| 24 | #ifdef THREAD_STACK_SIZE |
| 25 | #error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined" |
| 26 | #endif |
| 27 | #endif |
| 28 | |
Martin v. Löwis | 42ab61e | 2002-03-17 17:19:00 +0000 | [diff] [blame] | 29 | /* The POSIX spec says that implementations supporting the sem_* |
| 30 | family of functions must indicate this by defining |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | _POSIX_SEMAPHORES. */ |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 32 | #ifdef _POSIX_SEMAPHORES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so |
Martin v. Löwis | 8b8fb3d | 2005-03-28 12:34:20 +0000 | [diff] [blame] | 34 | we need to add 0 to make it work there as well. */ |
| 35 | #if (_POSIX_SEMAPHORES+0) == -1 |
Anthony Baxter | 19b2369 | 2005-03-16 04:15:07 +0000 | [diff] [blame] | 36 | #define HAVE_BROKEN_POSIX_SEMAPHORES |
| 37 | #else |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 38 | #include <semaphore.h> |
| 39 | #include <errno.h> |
| 40 | #endif |
Anthony Baxter | 19b2369 | 2005-03-16 04:15:07 +0000 | [diff] [blame] | 41 | #endif |
Guido van Rossum | 6602099 | 1996-06-11 18:32:18 +0000 | [diff] [blame] | 42 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | /* Before FreeBSD 5.4, system scope threads was very limited resource |
| 44 | in default setting. So the process scope is preferred to get |
| 45 | enough number of threads to work. */ |
| 46 | #ifdef __FreeBSD__ |
| 47 | #include <osreldate.h> |
| 48 | #if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101 |
| 49 | #undef PTHREAD_SYSTEM_SCHED_SUPPORTED |
| 50 | #endif |
| 51 | #endif |
| 52 | |
Martin v. Löwis | b023381 | 2002-12-11 13:12:30 +0000 | [diff] [blame] | 53 | #if !defined(pthread_attr_default) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 54 | # define pthread_attr_default ((pthread_attr_t *)NULL) |
Martin v. Löwis | b023381 | 2002-12-11 13:12:30 +0000 | [diff] [blame] | 55 | #endif |
| 56 | #if !defined(pthread_mutexattr_default) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 57 | # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL) |
Martin v. Löwis | b023381 | 2002-12-11 13:12:30 +0000 | [diff] [blame] | 58 | #endif |
| 59 | #if !defined(pthread_condattr_default) |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 60 | # define pthread_condattr_default ((pthread_condattr_t *)NULL) |
Guido van Rossum | 1a62311 | 1996-08-08 18:53:41 +0000 | [diff] [blame] | 61 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 63 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 64 | /* Whether or not to use semaphores directly rather than emulating them with |
| 65 | * mutexes and condition variables: |
| 66 | */ |
Antoine Pitrou | 19f8edc | 2010-10-10 08:37:22 +0000 | [diff] [blame] | 67 | #if (defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) && \ |
| 68 | defined(HAVE_SEM_TIMEDWAIT)) |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 69 | # define USE_SEMAPHORES |
| 70 | #else |
| 71 | # undef USE_SEMAPHORES |
| 72 | #endif |
| 73 | |
| 74 | |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 75 | /* On platforms that don't use standard POSIX threads pthread_sigmask() |
| 76 | * isn't present. DEC threads uses sigprocmask() instead as do most |
| 77 | * other UNIX International compliant systems that don't have the full |
| 78 | * pthread implementation. |
| 79 | */ |
Jason Tishler | fac083d | 2003-07-22 15:20:49 +0000 | [diff] [blame] | 80 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 81 | # define SET_THREAD_SIGMASK pthread_sigmask |
| 82 | #else |
| 83 | # define SET_THREAD_SIGMASK sigprocmask |
| 84 | #endif |
| 85 | |
| 86 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 87 | /* We assume all modern POSIX systems have gettimeofday() */ |
| 88 | #ifdef GETTIMEOFDAY_NO_TZ |
| 89 | #define GETTIMEOFDAY(ptv) gettimeofday(ptv) |
| 90 | #else |
| 91 | #define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL) |
| 92 | #endif |
| 93 | |
| 94 | #define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \ |
| 95 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | struct timeval tv; \ |
| 97 | GETTIMEOFDAY(&tv); \ |
| 98 | tv.tv_usec += microseconds % 1000000; \ |
| 99 | tv.tv_sec += microseconds / 1000000; \ |
| 100 | tv.tv_sec += tv.tv_usec / 1000000; \ |
| 101 | tv.tv_usec %= 1000000; \ |
| 102 | ts.tv_sec = tv.tv_sec; \ |
| 103 | ts.tv_nsec = tv.tv_usec * 1000; \ |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 104 | } while(0) |
| 105 | |
| 106 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 107 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 108 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 109 | * following are undefined: |
| 110 | * -> a thread tries to lock a mutex it already has locked |
| 111 | * -> a thread tries to unlock a mutex locked by a different thread |
| 112 | * pthread mutexes are designed for serializing threads over short pieces |
| 113 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 114 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 115 | * |
| 116 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 117 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 118 | * instantly, it is, else the pair is used to block the thread until the |
| 119 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 120 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 122 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | char locked; /* 0=unlocked, 1=locked */ |
| 124 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 125 | pthread_cond_t lock_released; |
| 126 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 127 | } pthread_lock; |
| 128 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 129 | #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; } |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Initialization. |
| 133 | */ |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 134 | |
| 135 | #ifdef _HAVE_BSDI |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 136 | static |
| 137 | void _noop(void) |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 138 | { |
| 139 | } |
| 140 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 141 | static void |
| 142 | PyThread__init_thread(void) |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | /* DO AN INIT BY STARTING THE THREAD */ |
| 145 | static int dummy = 0; |
| 146 | pthread_t thread1; |
| 147 | pthread_create(&thread1, NULL, (void *) _noop, &dummy); |
| 148 | pthread_join(thread1, NULL); |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #else /* !_HAVE_BSDI */ |
| 152 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 153 | static void |
| 154 | PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 155 | { |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 156 | #if defined(_AIX) && defined(__GNUC__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | pthread_init(); |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 158 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 161 | #endif /* !_HAVE_BSDI */ |
| 162 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 163 | /* |
| 164 | * Thread support. |
| 165 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 166 | |
| 167 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 168 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 169 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | pthread_t th; |
| 172 | int status; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 173 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | pthread_attr_t attrs; |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 175 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 176 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | size_t tss; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 178 | #endif |
| 179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | dprintf(("PyThread_start_new_thread called\n")); |
| 181 | if (!initialized) |
| 182 | PyThread_init_thread(); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 184 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | if (pthread_attr_init(&attrs) != 0) |
| 186 | return -1; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 187 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 188 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | tss = (_pythread_stacksize != 0) ? _pythread_stacksize |
| 190 | : THREAD_STACK_SIZE; |
| 191 | if (tss != 0) { |
| 192 | if (pthread_attr_setstacksize(&attrs, tss) != 0) { |
| 193 | pthread_attr_destroy(&attrs); |
| 194 | return -1; |
| 195 | } |
| 196 | } |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 197 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 198 | #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 200 | #endif |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | status = pthread_create(&th, |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 203 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | &attrs, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 205 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | (pthread_attr_t*)NULL, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 207 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | (void* (*)(void *))func, |
| 209 | (void *)arg |
| 210 | ); |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 211 | |
Fred Drake | 03459a5 | 2001-11-09 16:00:41 +0000 | [diff] [blame] | 212 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | pthread_attr_destroy(&attrs); |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 214 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | if (status != 0) |
| 216 | return -1; |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | pthread_detach(th); |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 220 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | return (long) th; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 222 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | return (long) *(long *) &th; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 224 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 227 | /* XXX This implementation is considered (to quote Tim Peters) "inherently |
| 228 | hosed" because: |
Skip Montanaro | 6babcc2 | 2004-03-03 08:42:23 +0000 | [diff] [blame] | 229 | - 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] | 230 | - The cast to long is inherently unsafe. |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 231 | - It is not clear that the 'volatile' (for AIX?) are any longer necessary. |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 232 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 234 | PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | volatile pthread_t threadid; |
| 237 | if (!initialized) |
| 238 | PyThread_init_thread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | threadid = pthread_self(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | return (long) threadid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 244 | PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | dprintf(("PyThread_exit_thread called\n")); |
| 247 | if (!initialized) { |
| 248 | exit(0); |
| 249 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 252 | #ifdef USE_SEMAPHORES |
| 253 | |
| 254 | /* |
| 255 | * Lock support. |
| 256 | */ |
| 257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | PyThread_type_lock |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 259 | PyThread_allocate_lock(void) |
| 260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | sem_t *lock; |
| 262 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | dprintf(("PyThread_allocate_lock called\n")); |
| 265 | if (!initialized) |
| 266 | PyThread_init_thread(); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | lock = (sem_t *)malloc(sizeof(sem_t)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | if (lock) { |
| 271 | status = sem_init(lock,0,1); |
| 272 | CHECK_STATUS("sem_init"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | if (error) { |
| 275 | free((void *)lock); |
| 276 | lock = NULL; |
| 277 | } |
| 278 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 281 | return (PyThread_type_lock)lock; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | void |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 285 | PyThread_free_lock(PyThread_type_lock lock) |
| 286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | sem_t *thelock = (sem_t *)lock; |
| 288 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | if (!thelock) |
| 293 | return; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | status = sem_destroy(thelock); |
| 296 | CHECK_STATUS("sem_destroy"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | free((void *)thelock); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * As of February 2002, Cygwin thread implementations mistakenly report error |
| 303 | * codes in the return value of the sem_ calls (like the pthread_ functions). |
| 304 | * Correct implementations return -1 and put the code in errno. This supports |
| 305 | * either. |
| 306 | */ |
| 307 | static int |
| 308 | fix_status(int status) |
| 309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | return (status == -1) ? errno : status; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 313 | PyLockStatus |
| 314 | PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds, |
| 315 | int intr_flag) |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 316 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 317 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | sem_t *thelock = (sem_t *)lock; |
| 319 | int status, error = 0; |
| 320 | struct timespec ts; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 322 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n", |
| 323 | lock, microseconds, intr_flag)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | if (microseconds > 0) |
| 326 | MICROSECONDS_TO_TIMESPEC(microseconds, ts); |
| 327 | do { |
| 328 | if (microseconds > 0) |
| 329 | status = fix_status(sem_timedwait(thelock, &ts)); |
| 330 | else if (microseconds == 0) |
| 331 | status = fix_status(sem_trywait(thelock)); |
| 332 | else |
| 333 | status = fix_status(sem_wait(thelock)); |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 334 | /* Retry if interrupted by a signal, unless the caller wants to be |
| 335 | notified. */ |
| 336 | } while (!intr_flag && status == EINTR); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 338 | /* Don't check the status if we're stopping because of an interrupt. */ |
| 339 | if (!(intr_flag && status == EINTR)) { |
| 340 | if (microseconds > 0) { |
| 341 | if (status != ETIMEDOUT) |
| 342 | CHECK_STATUS("sem_timedwait"); |
| 343 | } |
| 344 | else if (microseconds == 0) { |
| 345 | if (status != EAGAIN) |
| 346 | CHECK_STATUS("sem_trywait"); |
| 347 | } |
| 348 | else { |
| 349 | CHECK_STATUS("sem_wait"); |
| 350 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 353 | if (status == 0) { |
| 354 | success = PY_LOCK_ACQUIRED; |
| 355 | } else if (intr_flag && status == EINTR) { |
| 356 | success = PY_LOCK_INTR; |
| 357 | } else { |
| 358 | success = PY_LOCK_FAILURE; |
| 359 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 361 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n", |
| 362 | lock, microseconds, intr_flag, success)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | return success; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | void |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 367 | PyThread_release_lock(PyThread_type_lock lock) |
| 368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | sem_t *thelock = (sem_t *)lock; |
| 370 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | status = sem_post(thelock); |
| 375 | CHECK_STATUS("sem_post"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | #else /* USE_SEMAPHORES */ |
| 379 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 380 | /* |
| 381 | * Lock support. |
| 382 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | PyThread_type_lock |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 384 | PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | pthread_lock *lock; |
| 387 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | dprintf(("PyThread_allocate_lock called\n")); |
| 390 | if (!initialized) |
| 391 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | lock = (pthread_lock *) malloc(sizeof(pthread_lock)); |
| 394 | if (lock) { |
| 395 | memset((void *)lock, '\0', sizeof(pthread_lock)); |
| 396 | lock->locked = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | status = pthread_mutex_init(&lock->mut, |
| 399 | pthread_mutexattr_default); |
| 400 | CHECK_STATUS("pthread_mutex_init"); |
| 401 | /* Mark the pthread mutex underlying a Python mutex as |
| 402 | pure happens-before. We can't simply mark the |
| 403 | Python-level mutex as a mutex because it can be |
| 404 | acquired and released in different threads, which |
| 405 | will cause errors. */ |
| 406 | _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | status = pthread_cond_init(&lock->lock_released, |
| 409 | pthread_condattr_default); |
| 410 | CHECK_STATUS("pthread_cond_init"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | if (error) { |
| 413 | free((void *)lock); |
| 414 | lock = 0; |
| 415 | } |
| 416 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 419 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 423 | PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | pthread_lock *thelock = (pthread_lock *)lock; |
| 426 | int status, error = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | status = pthread_mutex_destroy( &thelock->mut ); |
| 431 | CHECK_STATUS("pthread_mutex_destroy"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | status = pthread_cond_destroy( &thelock->lock_released ); |
| 434 | CHECK_STATUS("pthread_cond_destroy"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | free((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 439 | PyLockStatus |
| 440 | PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds, |
| 441 | int intr_flag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 443 | PyLockStatus success; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | pthread_lock *thelock = (pthread_lock *)lock; |
| 445 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 447 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) called\n", |
| 448 | lock, microseconds, intr_flag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | status = pthread_mutex_lock( &thelock->mut ); |
| 451 | CHECK_STATUS("pthread_mutex_lock[1]"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 452 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 453 | if (thelock->locked == 0) { |
| 454 | success = PY_LOCK_ACQUIRED; |
| 455 | } else if (microseconds == 0) { |
| 456 | success = PY_LOCK_FAILURE; |
| 457 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | struct timespec ts; |
| 459 | if (microseconds > 0) |
| 460 | MICROSECONDS_TO_TIMESPEC(microseconds, ts); |
| 461 | /* continue trying until we get the lock */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | /* mut must be locked by me -- part of the condition |
| 464 | * protocol */ |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 465 | success = PY_LOCK_FAILURE; |
| 466 | while (success == PY_LOCK_FAILURE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | if (microseconds > 0) { |
| 468 | status = pthread_cond_timedwait( |
| 469 | &thelock->lock_released, |
| 470 | &thelock->mut, &ts); |
| 471 | if (status == ETIMEDOUT) |
| 472 | break; |
| 473 | CHECK_STATUS("pthread_cond_timed_wait"); |
| 474 | } |
| 475 | else { |
| 476 | status = pthread_cond_wait( |
| 477 | &thelock->lock_released, |
| 478 | &thelock->mut); |
| 479 | CHECK_STATUS("pthread_cond_wait"); |
| 480 | } |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 481 | |
| 482 | if (intr_flag && status == 0 && thelock->locked) { |
| 483 | /* We were woken up, but didn't get the lock. We probably received |
| 484 | * a signal. Return PY_LOCK_INTR to allow the caller to handle |
| 485 | * it and retry. */ |
| 486 | success = PY_LOCK_INTR; |
| 487 | break; |
| 488 | } else if (status == 0 && !thelock->locked) { |
| 489 | success = PY_LOCK_ACQUIRED; |
| 490 | } else { |
| 491 | success = PY_LOCK_FAILURE; |
| 492 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | } |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 495 | if (success == PY_LOCK_ACQUIRED) thelock->locked = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | status = pthread_mutex_unlock( &thelock->mut ); |
| 497 | CHECK_STATUS("pthread_mutex_unlock[1]"); |
Martin v. Löwis | 1509a15 | 2003-04-18 11:11:09 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 499 | if (error) success = PY_LOCK_FAILURE; |
| 500 | dprintf(("PyThread_acquire_lock_timed(%p, %lld, %d) -> %d\n", |
| 501 | lock, microseconds, intr_flag, success)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 506 | PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 507 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | pthread_lock *thelock = (pthread_lock *)lock; |
| 509 | int status, error = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | status = pthread_mutex_lock( &thelock->mut ); |
| 514 | CHECK_STATUS("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | thelock->locked = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | status = pthread_mutex_unlock( &thelock->mut ); |
| 519 | CHECK_STATUS("pthread_mutex_unlock[3]"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 522 | status = pthread_cond_signal( &thelock->lock_released ); |
| 523 | CHECK_STATUS("pthread_cond_signal"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 524 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 525 | |
| 526 | #endif /* USE_SEMAPHORES */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 528 | int |
| 529 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
| 530 | { |
| 531 | return PyThread_acquire_lock_timed(lock, waitflag ? -1 : 0, /*intr_flag=*/0); |
| 532 | } |
| 533 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 534 | /* set the thread stack size. |
| 535 | * Return 0 if size is valid, -1 if size is invalid, |
| 536 | * -2 if setting stack size is not supported. |
| 537 | */ |
| 538 | static int |
| 539 | _pythread_pthread_set_stacksize(size_t size) |
| 540 | { |
| 541 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | pthread_attr_t attrs; |
| 543 | size_t tss_min; |
| 544 | int rc = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 545 | #endif |
| 546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | /* set to default */ |
| 548 | if (size == 0) { |
| 549 | _pythread_stacksize = 0; |
| 550 | return 0; |
| 551 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 552 | |
| 553 | #if defined(THREAD_STACK_SIZE) |
| 554 | #if defined(PTHREAD_STACK_MIN) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN |
| 556 | : THREAD_STACK_MIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 557 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | tss_min = THREAD_STACK_MIN; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 559 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | if (size >= tss_min) { |
| 561 | /* validate stack size by setting thread attribute */ |
| 562 | if (pthread_attr_init(&attrs) == 0) { |
| 563 | rc = pthread_attr_setstacksize(&attrs, size); |
| 564 | pthread_attr_destroy(&attrs); |
| 565 | if (rc == 0) { |
| 566 | _pythread_stacksize = size; |
| 567 | return 0; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 572 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 574 | #endif |
| 575 | } |
| 576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x) |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 578 | |
| 579 | #define Py_HAVE_NATIVE_TLS |
| 580 | |
| 581 | int |
| 582 | PyThread_create_key(void) |
| 583 | { |
| 584 | pthread_key_t key; |
| 585 | int fail = pthread_key_create(&key, NULL); |
| 586 | return fail ? -1 : key; |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | PyThread_delete_key(int key) |
| 591 | { |
| 592 | pthread_key_delete(key); |
| 593 | } |
| 594 | |
| 595 | void |
| 596 | PyThread_delete_key_value(int key) |
| 597 | { |
| 598 | pthread_setspecific(key, NULL); |
| 599 | } |
| 600 | |
| 601 | int |
| 602 | PyThread_set_key_value(int key, void *value) |
| 603 | { |
| 604 | int fail; |
| 605 | void *oldValue = pthread_getspecific(key); |
| 606 | if (oldValue != NULL) |
| 607 | return 0; |
| 608 | fail = pthread_setspecific(key, value); |
| 609 | return fail ? -1 : 0; |
| 610 | } |
| 611 | |
| 612 | void * |
| 613 | PyThread_get_key_value(int key) |
| 614 | { |
| 615 | return pthread_getspecific(key); |
| 616 | } |
| 617 | |
| 618 | void |
| 619 | PyThread_ReInitTLS(void) |
| 620 | {} |