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" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 9 | #include "internal/pystate.h" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 10 | |
Matthias Klose | a2542be | 2004-08-16 11:35:51 +0000 | [diff] [blame] | 11 | #ifndef _POSIX_THREADS |
| 12 | /* This means pthreads are not implemented in libc headers, hence the macro |
| 13 | not present in unistd.h. But they still can be implemented as an external |
| 14 | library (e.g. gnu pth in pthread emulation) */ |
| 15 | # ifdef HAVE_PTHREAD_H |
| 16 | # include <pthread.h> /* _POSIX_THREADS */ |
| 17 | # endif |
| 18 | #endif |
| 19 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 20 | #ifndef DONT_HAVE_STDIO_H |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 22 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 26 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 28 | #ifndef _POSIX_THREADS |
| 29 | |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 30 | /* 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] | 31 | enough of the Posix threads package is implemented to support python |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 32 | threads. |
| 33 | |
| 34 | 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] | 35 | 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] | 36 | of a pa-risc system. |
| 37 | */ |
| 38 | #ifdef __hpux |
| 39 | #ifdef _SC_THREADS |
| 40 | #define _POSIX_THREADS |
| 41 | #endif |
| 42 | #endif |
| 43 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 44 | #endif /* _POSIX_THREADS */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 47 | #ifdef Py_DEBUG |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 48 | static int thread_debug = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | #define dprintf(args) (void)((thread_debug & 1) && printf args) |
| 50 | #define d2printf(args) ((thread_debug & 8) && printf args) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 51 | #else |
| 52 | #define dprintf(args) |
| 53 | #define d2printf(args) |
| 54 | #endif |
| 55 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 56 | static int initialized; |
| 57 | |
Thomas Wouters | 8ec68fd | 2000-07-24 14:39:50 +0000 | [diff] [blame] | 58 | static void PyThread__init_thread(void); /* Forward */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 59 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 60 | void |
| 61 | PyThread_init_thread(void) |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 63 | #ifdef Py_DEBUG |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 64 | const char *p = Py_GETENV("PYTHONTHREADDEBUG"); |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | if (p) { |
| 67 | if (*p) |
| 68 | thread_debug = atoi(p); |
| 69 | else |
| 70 | thread_debug = 1; |
| 71 | } |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 72 | #endif /* Py_DEBUG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | if (initialized) |
| 74 | return; |
| 75 | initialized = 1; |
| 76 | dprintf(("PyThread_init_thread called\n")); |
| 77 | PyThread__init_thread(); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Masayuki Yamamoto | aa0aa04 | 2017-07-03 20:34:38 +0900 | [diff] [blame] | 80 | #if defined(_POSIX_THREADS) |
| 81 | # define PYTHREAD_NAME "pthread" |
| 82 | # include "thread_pthread.h" |
| 83 | #elif defined(NT_THREADS) |
| 84 | # define PYTHREAD_NAME "nt" |
| 85 | # include "thread_nt.h" |
| 86 | #else |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 87 | # error "Require native threads. See https://bugs.python.org/issue31370" |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | /* return the current thread stack size */ |
| 92 | size_t |
| 93 | PyThread_get_stacksize(void) |
| 94 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 95 | return PyThreadState_GET()->interp->pythread_stacksize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* Only platforms defining a THREAD_SET_STACKSIZE() macro |
| 99 | in thread_<platform>.h support changing the stack size. |
| 100 | Return 0 if stack size is valid, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | -1 if stack size value is invalid, |
| 102 | -2 if setting stack size is not supported. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 103 | int |
| 104 | PyThread_set_stacksize(size_t size) |
| 105 | { |
| 106 | #if defined(THREAD_SET_STACKSIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | return THREAD_SET_STACKSIZE(size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 108 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 110 | #endif |
| 111 | } |
| 112 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 113 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 114 | /* Thread Specific Storage (TSS) API |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 115 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 116 | Cross-platform components of TSS API implementation. |
| 117 | */ |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 118 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 119 | Py_tss_t * |
| 120 | PyThread_tss_alloc(void) |
| 121 | { |
| 122 | Py_tss_t *new_key = (Py_tss_t *)PyMem_RawMalloc(sizeof(Py_tss_t)); |
| 123 | if (new_key == NULL) { |
| 124 | return NULL; |
| 125 | } |
| 126 | new_key->_is_initialized = 0; |
| 127 | return new_key; |
| 128 | } |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 129 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 130 | void |
| 131 | PyThread_tss_free(Py_tss_t *key) |
| 132 | { |
| 133 | if (key != NULL) { |
| 134 | PyThread_tss_delete(key); |
| 135 | PyMem_RawFree((void *)key); |
| 136 | } |
| 137 | } |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 138 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 139 | int |
| 140 | PyThread_tss_is_created(Py_tss_t *key) |
| 141 | { |
| 142 | assert(key != NULL); |
| 143 | return key->_is_initialized; |
| 144 | } |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 145 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 146 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 147 | PyDoc_STRVAR(threadinfo__doc__, |
| 148 | "sys.thread_info\n\ |
| 149 | \n\ |
| 150 | A struct sequence holding information about the thread implementation."); |
| 151 | |
| 152 | static PyStructSequence_Field threadinfo_fields[] = { |
| 153 | {"name", "name of the thread implementation"}, |
| 154 | {"lock", "name of the lock implementation"}, |
| 155 | {"version", "name and version of the thread library"}, |
| 156 | {0} |
| 157 | }; |
| 158 | |
| 159 | static PyStructSequence_Desc threadinfo_desc = { |
| 160 | "sys.thread_info", /* name */ |
| 161 | threadinfo__doc__, /* doc */ |
| 162 | threadinfo_fields, /* fields */ |
| 163 | 3 |
| 164 | }; |
| 165 | |
| 166 | static PyTypeObject ThreadInfoType; |
| 167 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 168 | PyObject* |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 169 | PyThread_GetInfo(void) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 170 | { |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 171 | PyObject *threadinfo, *value; |
| 172 | int pos = 0; |
Victor Stinner | e07f522 | 2011-04-20 12:23:26 +0200 | [diff] [blame] | 173 | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
| 174 | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 175 | char buffer[255]; |
| 176 | int len; |
Victor Stinner | e07f522 | 2011-04-20 12:23:26 +0200 | [diff] [blame] | 177 | #endif |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 178 | |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 179 | if (ThreadInfoType.tp_name == 0) { |
| 180 | if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0) |
| 181 | return NULL; |
| 182 | } |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 183 | |
| 184 | threadinfo = PyStructSequence_New(&ThreadInfoType); |
| 185 | if (threadinfo == NULL) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 186 | return NULL; |
| 187 | |
| 188 | value = PyUnicode_FromString(PYTHREAD_NAME); |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 189 | if (value == NULL) { |
| 190 | Py_DECREF(threadinfo); |
| 191 | return NULL; |
| 192 | } |
| 193 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 194 | |
| 195 | #ifdef _POSIX_THREADS |
| 196 | #ifdef USE_SEMAPHORES |
| 197 | value = PyUnicode_FromString("semaphore"); |
| 198 | #else |
| 199 | value = PyUnicode_FromString("mutex+cond"); |
| 200 | #endif |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 201 | if (value == NULL) { |
| 202 | Py_DECREF(threadinfo); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 203 | return NULL; |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 204 | } |
| 205 | #else |
| 206 | Py_INCREF(Py_None); |
| 207 | value = Py_None; |
| 208 | #endif |
| 209 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 210 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 211 | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
| 212 | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
| 213 | value = NULL; |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 214 | len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer)); |
Victor Stinner | 98ea54c | 2014-08-15 23:30:40 +0200 | [diff] [blame] | 215 | if (1 < len && (size_t)len < sizeof(buffer)) { |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 216 | value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
| 217 | if (value == NULL) |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 218 | PyErr_Clear(); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 219 | } |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 220 | if (value == NULL) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 221 | #endif |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 222 | { |
| 223 | Py_INCREF(Py_None); |
| 224 | value = Py_None; |
| 225 | } |
| 226 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
| 227 | return threadinfo; |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 228 | } |