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