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