Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 2 | /* Thread package. |
| 3 | This is intended to be usable independently from Python. |
| 4 | The implementation for system foobar is in a file thread_foobar.h |
| 5 | which is included by this file dependent on config settings. |
| 6 | Stuff shared by all thread_*.h files is collected here. */ |
| 7 | |
Martin v. Löwis | cdc4451 | 2002-01-12 11:05:12 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 9 | |
Matthias Klose | a2542be | 2004-08-16 11:35:51 +0000 | [diff] [blame] | 10 | #ifndef _POSIX_THREADS |
| 11 | /* This means pthreads are not implemented in libc headers, hence the macro |
| 12 | not present in unistd.h. But they still can be implemented as an external |
| 13 | library (e.g. gnu pth in pthread emulation) */ |
| 14 | # ifdef HAVE_PTHREAD_H |
| 15 | # include <pthread.h> /* _POSIX_THREADS */ |
| 16 | # endif |
| 17 | #endif |
| 18 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 19 | #ifndef DONT_HAVE_STDIO_H |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 21 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 25 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 27 | #ifndef _POSIX_THREADS |
| 28 | |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 29 | /* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 30 | enough of the Posix threads package is implemented to support python |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 31 | threads. |
| 32 | |
| 33 | This is valid for HP-UX 11.23 running on an ia64 system. If needed, add |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 34 | a check of __ia64 to verify that we're running on an ia64 system instead |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 35 | of a pa-risc system. |
| 36 | */ |
| 37 | #ifdef __hpux |
| 38 | #ifdef _SC_THREADS |
| 39 | #define _POSIX_THREADS |
| 40 | #endif |
| 41 | #endif |
| 42 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 43 | #endif /* _POSIX_THREADS */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 46 | #ifdef Py_DEBUG |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 47 | static int thread_debug = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | #define dprintf(args) (void)((thread_debug & 1) && printf args) |
| 49 | #define d2printf(args) ((thread_debug & 8) && printf args) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 50 | #else |
| 51 | #define dprintf(args) |
| 52 | #define d2printf(args) |
| 53 | #endif |
| 54 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 55 | static int initialized; |
| 56 | |
Thomas Wouters | 8ec68fd | 2000-07-24 14:39:50 +0000 | [diff] [blame] | 57 | static void PyThread__init_thread(void); /* Forward */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 59 | void |
| 60 | PyThread_init_thread(void) |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 62 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | char *p = Py_GETENV("PYTHONTHREADDEBUG"); |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (p) { |
| 66 | if (*p) |
| 67 | thread_debug = atoi(p); |
| 68 | else |
| 69 | thread_debug = 1; |
| 70 | } |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 71 | #endif /* Py_DEBUG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (initialized) |
| 73 | return; |
| 74 | initialized = 1; |
| 75 | dprintf(("PyThread_init_thread called\n")); |
| 76 | PyThread__init_thread(); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 79 | /* Support for runtime thread stack size tuning. |
| 80 | A value of 0 means using the platform's default stack size |
| 81 | or the size specified by the THREAD_STACK_SIZE macro. */ |
| 82 | static size_t _pythread_stacksize = 0; |
| 83 | |
Masayuki Yamamoto | aa0aa04 | 2017-07-03 20:34:38 +0900 | [diff] [blame] | 84 | #if defined(_POSIX_THREADS) |
| 85 | # define PYTHREAD_NAME "pthread" |
| 86 | # include "thread_pthread.h" |
| 87 | #elif defined(NT_THREADS) |
| 88 | # define PYTHREAD_NAME "nt" |
| 89 | # include "thread_nt.h" |
| 90 | #else |
| 91 | # error "Require native thread feature. See https://bugs.python.org/issue30832" |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 94 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 95 | /* return the current thread stack size */ |
| 96 | size_t |
| 97 | PyThread_get_stacksize(void) |
| 98 | { |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 99 | return _pythread_stacksize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Only platforms defining a THREAD_SET_STACKSIZE() macro |
| 103 | in thread_<platform>.h support changing the stack size. |
| 104 | Return 0 if stack size is valid, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | -1 if stack size value is invalid, |
| 106 | -2 if setting stack size is not supported. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 107 | int |
| 108 | PyThread_set_stacksize(size_t size) |
| 109 | { |
| 110 | #if defined(THREAD_SET_STACKSIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | return THREAD_SET_STACKSIZE(size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 112 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 114 | #endif |
| 115 | } |
| 116 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 117 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 118 | /* ------------------------------------------------------------------------ |
| 119 | Per-thread data ("key") support. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 120 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 121 | Use PyThread_create_key() to create a new key. This is typically shared |
| 122 | across threads. |
| 123 | |
| 124 | Use PyThread_set_key_value(thekey, value) to associate void* value with |
| 125 | thekey in the current thread. Each thread has a distinct mapping of thekey |
| 126 | to a void* value. Caution: if the current thread already has a mapping |
| 127 | for thekey, value is ignored. |
| 128 | |
| 129 | Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
| 130 | with thekey in the current thread. This returns NULL if no value is |
| 131 | associated with thekey in the current thread. |
| 132 | |
| 133 | Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
| 134 | value for thekey. PyThread_delete_key(thekey) forgets the values associated |
| 135 | with thekey across *all* threads. |
| 136 | |
| 137 | While some of these functions have error-return values, none set any |
| 138 | Python exception. |
| 139 | |
| 140 | None of the functions does memory management on behalf of the void* values. |
| 141 | You need to allocate and deallocate them yourself. If the void* values |
| 142 | happen to be PyObject*, these functions don't do refcount operations on |
| 143 | them either. |
| 144 | |
| 145 | The GIL does not need to be held when calling these functions; they supply |
| 146 | their own locking. This isn't true of PyThread_create_key(), though (see |
| 147 | next paragraph). |
| 148 | |
| 149 | There's a hidden assumption that PyThread_create_key() will be called before |
| 150 | any of the other functions are called. There's also a hidden assumption |
| 151 | that calls to PyThread_create_key() are serialized externally. |
| 152 | ------------------------------------------------------------------------ */ |
| 153 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 154 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 155 | PyDoc_STRVAR(threadinfo__doc__, |
| 156 | "sys.thread_info\n\ |
| 157 | \n\ |
| 158 | A struct sequence holding information about the thread implementation."); |
| 159 | |
| 160 | static PyStructSequence_Field threadinfo_fields[] = { |
| 161 | {"name", "name of the thread implementation"}, |
| 162 | {"lock", "name of the lock implementation"}, |
| 163 | {"version", "name and version of the thread library"}, |
| 164 | {0} |
| 165 | }; |
| 166 | |
| 167 | static PyStructSequence_Desc threadinfo_desc = { |
| 168 | "sys.thread_info", /* name */ |
| 169 | threadinfo__doc__, /* doc */ |
| 170 | threadinfo_fields, /* fields */ |
| 171 | 3 |
| 172 | }; |
| 173 | |
| 174 | static PyTypeObject ThreadInfoType; |
| 175 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 176 | PyObject* |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 177 | PyThread_GetInfo(void) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 178 | { |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 179 | PyObject *threadinfo, *value; |
| 180 | int pos = 0; |
Victor Stinner | e07f522 | 2011-04-20 12:23:26 +0200 | [diff] [blame] | 181 | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
| 182 | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 183 | char buffer[255]; |
| 184 | int len; |
Victor Stinner | e07f522 | 2011-04-20 12:23:26 +0200 | [diff] [blame] | 185 | #endif |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 186 | |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 187 | if (ThreadInfoType.tp_name == 0) { |
| 188 | if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0) |
| 189 | return NULL; |
| 190 | } |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 191 | |
| 192 | threadinfo = PyStructSequence_New(&ThreadInfoType); |
| 193 | if (threadinfo == NULL) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 194 | return NULL; |
| 195 | |
| 196 | value = PyUnicode_FromString(PYTHREAD_NAME); |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 197 | if (value == NULL) { |
| 198 | Py_DECREF(threadinfo); |
| 199 | return NULL; |
| 200 | } |
| 201 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 202 | |
| 203 | #ifdef _POSIX_THREADS |
| 204 | #ifdef USE_SEMAPHORES |
| 205 | value = PyUnicode_FromString("semaphore"); |
| 206 | #else |
| 207 | value = PyUnicode_FromString("mutex+cond"); |
| 208 | #endif |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 209 | if (value == NULL) { |
| 210 | Py_DECREF(threadinfo); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 211 | return NULL; |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 212 | } |
| 213 | #else |
| 214 | Py_INCREF(Py_None); |
| 215 | value = Py_None; |
| 216 | #endif |
| 217 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 218 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 219 | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
| 220 | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
| 221 | value = NULL; |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 222 | len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer)); |
Victor Stinner | 98ea54c | 2014-08-15 23:30:40 +0200 | [diff] [blame] | 223 | if (1 < len && (size_t)len < sizeof(buffer)) { |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 224 | value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
| 225 | if (value == NULL) |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 226 | PyErr_Clear(); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 227 | } |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 228 | if (value == NULL) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 229 | #endif |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 230 | { |
| 231 | Py_INCREF(Py_None); |
| 232 | value = Py_None; |
| 233 | } |
| 234 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
| 235 | return threadinfo; |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 236 | } |