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 | |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +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 | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 19 | #define THREAD_STACK_SIZE 0 /* use default stack size */ |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 20 | #endif |
Ned Deily | 482f908 | 2011-05-28 00:11:54 -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 |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 33 | /* for safety, ensure a viable minimum stacksize */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 34 | #define THREAD_STACK_MIN 0x8000 /* 32kB */ |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +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 | c83ea13 | 2010-05-09 14:46:46 +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 | c83ea13 | 2010-05-09 14:46:46 +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 | |
Hye-Shik Chang | d478f34 | 2006-03-23 12:32:36 +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 | */ |
Martin v. Löwis | dfc33fd | 2003-01-21 10:14:41 +0000 | [diff] [blame] | 79 | #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES) |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 80 | # define USE_SEMAPHORES |
| 81 | #else |
| 82 | # undef USE_SEMAPHORES |
| 83 | #endif |
| 84 | |
| 85 | |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 86 | /* On platforms that don't use standard POSIX threads pthread_sigmask() |
| 87 | * isn't present. DEC threads uses sigprocmask() instead as do most |
| 88 | * other UNIX International compliant systems that don't have the full |
| 89 | * pthread implementation. |
| 90 | */ |
Jason Tishler | fac083d | 2003-07-22 15:20:49 +0000 | [diff] [blame] | 91 | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 92 | # define SET_THREAD_SIGMASK pthread_sigmask |
| 93 | #else |
| 94 | # define SET_THREAD_SIGMASK sigprocmask |
| 95 | #endif |
| 96 | |
| 97 | |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 98 | /* A pthread mutex isn't sufficient to model the Python lock type |
| 99 | * because, according to Draft 5 of the docs (P1003.4a/D5), both of the |
| 100 | * following are undefined: |
| 101 | * -> a thread tries to lock a mutex it already has locked |
| 102 | * -> a thread tries to unlock a mutex locked by a different thread |
| 103 | * pthread mutexes are designed for serializing threads over short pieces |
| 104 | * of code anyway, so wouldn't be an appropriate implementation of |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 105 | * Python's locks regardless. |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 106 | * |
| 107 | * The pthread_lock struct implements a Python lock as a "locked?" bit |
| 108 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 109 | * instantly, it is, else the pair is used to block the thread until the |
| 110 | * bit is cleared. 9 May 1994 tim@ksr.com |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 111 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 113 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 114 | char locked; /* 0=unlocked, 1=locked */ |
| 115 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 116 | pthread_cond_t lock_released; |
| 117 | pthread_mutex_t mut; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 118 | } pthread_lock; |
| 119 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 120 | #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; } |
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 | /* |
| 123 | * Initialization. |
| 124 | */ |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 125 | |
| 126 | #ifdef _HAVE_BSDI |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 127 | static |
| 128 | void _noop(void) |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 129 | { |
| 130 | } |
| 131 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 132 | static void |
| 133 | PyThread__init_thread(void) |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 135 | /* DO AN INIT BY STARTING THE THREAD */ |
| 136 | static int dummy = 0; |
| 137 | pthread_t thread1; |
| 138 | pthread_create(&thread1, NULL, (void *) _noop, &dummy); |
| 139 | pthread_join(thread1, NULL); |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #else /* !_HAVE_BSDI */ |
| 143 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 144 | static void |
| 145 | PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 147 | #if defined(_AIX) && defined(__GNUC__) |
Antoine Pitrou | 7e9cec0 | 2013-06-18 22:17:48 +0200 | [diff] [blame] | 148 | extern void pthread_init(void); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 149 | pthread_init(); |
Guido van Rossum | d21744a | 1998-09-10 03:04:40 +0000 | [diff] [blame] | 150 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Guido van Rossum | 9e46e56 | 1998-10-07 16:39:47 +0000 | [diff] [blame] | 153 | #endif /* !_HAVE_BSDI */ |
| 154 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 155 | /* |
| 156 | * Thread support. |
| 157 | */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 158 | |
| 159 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 160 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 161 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 162 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 163 | pthread_t th; |
| 164 | int status; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 165 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 166 | pthread_attr_t attrs; |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 167 | #endif |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 168 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 169 | size_t tss; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 170 | #endif |
| 171 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 172 | dprintf(("PyThread_start_new_thread called\n")); |
| 173 | if (!initialized) |
| 174 | PyThread_init_thread(); |
Guido van Rossum | d6353e2 | 1997-05-13 17:51:13 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 176 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 177 | if (pthread_attr_init(&attrs) != 0) |
| 178 | return -1; |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 179 | #endif |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 180 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 181 | tss = (_pythread_stacksize != 0) ? _pythread_stacksize |
| 182 | : THREAD_STACK_SIZE; |
| 183 | if (tss != 0) { |
| 184 | if (pthread_attr_setstacksize(&attrs, tss) != 0) { |
| 185 | pthread_attr_destroy(&attrs); |
| 186 | return -1; |
| 187 | } |
| 188 | } |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 189 | #endif |
Hye-Shik Chang | d478f34 | 2006-03-23 12:32:36 +0000 | [diff] [blame] | 190 | #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 191 | pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 192 | #endif |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 194 | status = pthread_create(&th, |
Guido van Rossum | d0b69ec | 2001-09-10 14:10:54 +0000 | [diff] [blame] | 195 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 196 | &attrs, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 197 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 198 | (pthread_attr_t*)NULL, |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 199 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 200 | (void* (*)(void *))func, |
| 201 | (void *)arg |
| 202 | ); |
Guido van Rossum | 8023099 | 2001-10-12 21:49:17 +0000 | [diff] [blame] | 203 | |
Fred Drake | 03459a5 | 2001-11-09 16:00:41 +0000 | [diff] [blame] | 204 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 205 | pthread_attr_destroy(&attrs); |
Jack Jansen | c51395d | 2001-08-29 15:24:53 +0000 | [diff] [blame] | 206 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 207 | if (status != 0) |
| 208 | return -1; |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 210 | pthread_detach(th); |
Martin v. Löwis | 910ae62 | 2003-04-19 07:44:52 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 212 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 213 | return (long) th; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 214 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | return (long) *(long *) &th; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 216 | #endif |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 219 | /* XXX This implementation is considered (to quote Tim Peters) "inherently |
| 220 | hosed" because: |
Skip Montanaro | 6babcc2 | 2004-03-03 08:42:23 +0000 | [diff] [blame] | 221 | - 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] | 222 | - The cast to long is inherently unsafe. |
| 223 | - It is not clear that the 'volatile' (for AIX?) and ugly casting in the |
| 224 | latter return statement (for Alpha OSF/1) are any longer necessary. |
| 225 | */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 226 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 227 | PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 228 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 229 | volatile pthread_t threadid; |
| 230 | if (!initialized) |
| 231 | PyThread_init_thread(); |
| 232 | /* Jump through some hoops for Alpha OSF/1 */ |
| 233 | threadid = pthread_self(); |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 234 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 235 | return (long) threadid; |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 236 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 237 | return (long) *(long *) &threadid; |
Trent Mick | 635f6fb | 2000-08-23 21:33:05 +0000 | [diff] [blame] | 238 | #endif |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 241 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 242 | PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 243 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 244 | dprintf(("PyThread_exit_thread called\n")); |
Antoine Pitrou | b9a4501 | 2014-11-21 02:04:21 +0100 | [diff] [blame] | 245 | if (!initialized) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 246 | exit(0); |
Antoine Pitrou | b9a4501 | 2014-11-21 02:04:21 +0100 | [diff] [blame] | 247 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 250 | #ifdef USE_SEMAPHORES |
| 251 | |
| 252 | /* |
| 253 | * Lock support. |
| 254 | */ |
| 255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 256 | PyThread_type_lock |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 257 | PyThread_allocate_lock(void) |
| 258 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 259 | sem_t *lock; |
| 260 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 262 | dprintf(("PyThread_allocate_lock called\n")); |
| 263 | if (!initialized) |
| 264 | PyThread_init_thread(); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 266 | lock = (sem_t *)malloc(sizeof(sem_t)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 268 | if (lock) { |
| 269 | status = sem_init(lock,0,1); |
| 270 | CHECK_STATUS("sem_init"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 272 | if (error) { |
| 273 | free((void *)lock); |
| 274 | lock = NULL; |
| 275 | } |
| 276 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 278 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 279 | return (PyThread_type_lock)lock; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 282 | void |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 283 | PyThread_free_lock(PyThread_type_lock lock) |
| 284 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 285 | sem_t *thelock = (sem_t *)lock; |
| 286 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 287 | |
Jesus Cea | 7ddd9c2 | 2012-12-05 14:41:11 +0100 | [diff] [blame] | 288 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 289 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 291 | if (!thelock) |
| 292 | return; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 294 | status = sem_destroy(thelock); |
| 295 | CHECK_STATUS("sem_destroy"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 297 | free((void *)thelock); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * As of February 2002, Cygwin thread implementations mistakenly report error |
| 302 | * codes in the return value of the sem_ calls (like the pthread_ functions). |
| 303 | * Correct implementations return -1 and put the code in errno. This supports |
| 304 | * either. |
| 305 | */ |
| 306 | static int |
| 307 | fix_status(int status) |
| 308 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 309 | return (status == -1) ? errno : status; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 312 | int |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 313 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
| 314 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 315 | int success; |
| 316 | sem_t *thelock = (sem_t *)lock; |
| 317 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 318 | |
Jesus Cea | 7ddd9c2 | 2012-12-05 14:41:11 +0100 | [diff] [blame] | 319 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 320 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 322 | do { |
| 323 | if (waitflag) |
| 324 | status = fix_status(sem_wait(thelock)); |
| 325 | else |
| 326 | status = fix_status(sem_trywait(thelock)); |
| 327 | } while (status == EINTR); /* Retry if interrupted by a signal */ |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 329 | if (waitflag) { |
| 330 | CHECK_STATUS("sem_wait"); |
| 331 | } else if (status != EAGAIN) { |
| 332 | CHECK_STATUS("sem_trywait"); |
| 333 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 335 | success = (status == 0) ? 1 : 0; |
| 336 | |
| 337 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 338 | return success; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | void |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 342 | PyThread_release_lock(PyThread_type_lock lock) |
| 343 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 344 | sem_t *thelock = (sem_t *)lock; |
| 345 | int status, error = 0; |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 346 | |
Jesus Cea | 7ddd9c2 | 2012-12-05 14:41:11 +0100 | [diff] [blame] | 347 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 348 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 349 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 350 | status = sem_post(thelock); |
| 351 | CHECK_STATUS("sem_post"); |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | #else /* USE_SEMAPHORES */ |
| 355 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 356 | /* |
| 357 | * Lock support. |
| 358 | */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 359 | PyThread_type_lock |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 360 | PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 361 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 362 | pthread_lock *lock; |
| 363 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 364 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 365 | dprintf(("PyThread_allocate_lock called\n")); |
| 366 | if (!initialized) |
| 367 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 369 | lock = (pthread_lock *) malloc(sizeof(pthread_lock)); |
| 370 | if (lock) { |
| 371 | memset((void *)lock, '\0', sizeof(pthread_lock)); |
| 372 | lock->locked = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 374 | status = pthread_mutex_init(&lock->mut, |
| 375 | pthread_mutexattr_default); |
| 376 | CHECK_STATUS("pthread_mutex_init"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 378 | status = pthread_cond_init(&lock->lock_released, |
| 379 | pthread_condattr_default); |
| 380 | CHECK_STATUS("pthread_cond_init"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 382 | if (error) { |
| 383 | free((void *)lock); |
| 384 | lock = 0; |
| 385 | } |
| 386 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 388 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 389 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 392 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 393 | PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 394 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 395 | pthread_lock *thelock = (pthread_lock *)lock; |
| 396 | int status, error = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | 7e9cec0 | 2013-06-18 22:17:48 +0200 | [diff] [blame] | 398 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 399 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 401 | status = pthread_mutex_destroy( &thelock->mut ); |
| 402 | CHECK_STATUS("pthread_mutex_destroy"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 404 | status = pthread_cond_destroy( &thelock->lock_released ); |
| 405 | CHECK_STATUS("pthread_cond_destroy"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 406 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 407 | free((void *)thelock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 410 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 411 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 412 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 413 | int success; |
| 414 | pthread_lock *thelock = (pthread_lock *)lock; |
| 415 | int status, error = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 417 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 419 | status = pthread_mutex_lock( &thelock->mut ); |
| 420 | CHECK_STATUS("pthread_mutex_lock[1]"); |
| 421 | success = thelock->locked == 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 423 | if ( !success && waitflag ) { |
| 424 | /* continue trying until we get the lock */ |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 426 | /* mut must be locked by me -- part of the condition |
| 427 | * protocol */ |
| 428 | while ( thelock->locked ) { |
| 429 | status = pthread_cond_wait(&thelock->lock_released, |
| 430 | &thelock->mut); |
| 431 | CHECK_STATUS("pthread_cond_wait"); |
| 432 | } |
| 433 | success = 1; |
| 434 | } |
| 435 | if (success) thelock->locked = 1; |
| 436 | status = pthread_mutex_unlock( &thelock->mut ); |
| 437 | CHECK_STATUS("pthread_mutex_unlock[1]"); |
Martin v. Löwis | 1509a15 | 2003-04-18 11:11:09 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 439 | if (error) success = 0; |
| 440 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 441 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 444 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 445 | PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 446 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 447 | pthread_lock *thelock = (pthread_lock *)lock; |
| 448 | int status, error = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | 7e9cec0 | 2013-06-18 22:17:48 +0200 | [diff] [blame] | 450 | (void) error; /* silence unused-but-set-variable warning */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 451 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 452 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 453 | status = pthread_mutex_lock( &thelock->mut ); |
| 454 | CHECK_STATUS("pthread_mutex_lock[3]"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 455 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 456 | thelock->locked = 0; |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 458 | status = pthread_mutex_unlock( &thelock->mut ); |
| 459 | CHECK_STATUS("pthread_mutex_unlock[3]"); |
Guido van Rossum | b98b1b3 | 1994-05-11 08:42:04 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 461 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 462 | status = pthread_cond_signal( &thelock->lock_released ); |
| 463 | CHECK_STATUS("pthread_cond_signal"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 464 | } |
Martin v. Löwis | cc89866 | 2002-03-17 09:53:51 +0000 | [diff] [blame] | 465 | |
| 466 | #endif /* USE_SEMAPHORES */ |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 467 | |
| 468 | /* set the thread stack size. |
| 469 | * Return 0 if size is valid, -1 if size is invalid, |
| 470 | * -2 if setting stack size is not supported. |
| 471 | */ |
| 472 | static int |
| 473 | _pythread_pthread_set_stacksize(size_t size) |
| 474 | { |
| 475 | #if defined(THREAD_STACK_SIZE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 476 | pthread_attr_t attrs; |
| 477 | size_t tss_min; |
| 478 | int rc = 0; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 479 | #endif |
| 480 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 481 | /* set to default */ |
| 482 | if (size == 0) { |
| 483 | _pythread_stacksize = 0; |
| 484 | return 0; |
| 485 | } |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 486 | |
| 487 | #if defined(THREAD_STACK_SIZE) |
| 488 | #if defined(PTHREAD_STACK_MIN) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 489 | tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN |
| 490 | : THREAD_STACK_MIN; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 491 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 492 | tss_min = THREAD_STACK_MIN; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 493 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 494 | if (size >= tss_min) { |
| 495 | /* validate stack size by setting thread attribute */ |
| 496 | if (pthread_attr_init(&attrs) == 0) { |
| 497 | rc = pthread_attr_setstacksize(&attrs, size); |
| 498 | pthread_attr_destroy(&attrs); |
| 499 | if (rc == 0) { |
| 500 | _pythread_stacksize = size; |
| 501 | return 0; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | return -1; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 506 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 507 | return -2; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 508 | #endif |
| 509 | } |
| 510 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 511 | #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x) |