blob: 5907f5cf709a1cb643a4c509ddd588fec102984c [file] [log] [blame]
Guido van Rossum34679b71993-01-26 13:33:44 +00001
Guido van Rossum1d5735e1994-08-30 08:27:36 +00002/* 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öwiscdc44512002-01-12 11:05:12 +00008#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02009#include "pycore_pystate.h" // _PyInterpreterState_GET()
Guido van Rossum1d5735e1994-08-30 08:27:36 +000010
Matthias Klosea2542be2004-08-16 11:35:51 +000011#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 Rossum2571cc81999-04-07 16:07:23 +000020#ifndef DONT_HAVE_STDIO_H
Guido van Rossum1d5735e1994-08-30 08:27:36 +000021#include <stdio.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000022#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024#include <stdlib.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +000025
Guido van Rossum1d5735e1994-08-30 08:27:36 +000026#ifndef _POSIX_THREADS
27
Guido van Rossum539c6622005-09-14 17:49:54 +000028/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melotti13925002011-03-16 11:05:33 +020029 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000030 threads.
31
32 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
Martin Panter7462b6492015-11-02 03:37:02 +000033 a check of __ia64 to verify that we're running on an ia64 system instead
Guido van Rossum539c6622005-09-14 17:49:54 +000034 of a pa-risc system.
35*/
36#ifdef __hpux
37#ifdef _SC_THREADS
38#define _POSIX_THREADS
39#endif
40#endif
41
Sjoerd Mullender66bca321993-12-03 16:54:45 +000042#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043
Dan Willemsenb3614d22019-10-07 16:06:34 -070044// ANDROID: dprintf macro is already defined by our glibc sysroot
45#ifdef dprintf
46#undef dprintf
47#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000048
Guido van Rossum408027e1996-12-30 16:17:54 +000049#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000050static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051#define dprintf(args) (void)((thread_debug & 1) && printf args)
52#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000053#else
54#define dprintf(args)
55#define d2printf(args)
56#endif
57
Guido van Rossum1984f1e1992-08-04 12:41:02 +000058static int initialized;
59
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000060static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000061
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000062void
63PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000064{
Guido van Rossum408027e1996-12-30 16:17:54 +000065#ifdef Py_DEBUG
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +020066 const char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (p) {
69 if (*p)
70 thread_debug = atoi(p);
71 else
72 thread_debug = 1;
73 }
Guido van Rossum408027e1996-12-30 16:17:54 +000074#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (initialized)
76 return;
77 initialized = 1;
78 dprintf(("PyThread_init_thread called\n"));
79 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000080}
81
Miss Islington (bot)a11158e2021-08-06 04:32:37 -070082void
83_PyThread_debug_deprecation(void)
84{
85#ifdef Py_DEBUG
86 if (thread_debug) {
87 // Flush previous dprintf() logs
88 fflush(stdout);
89 if (PyErr_WarnEx(PyExc_DeprecationWarning,
90 "The threading debug (PYTHONTHREADDEBUG environment "
91 "variable) is deprecated and will be removed "
92 "in Python 3.12",
93 0))
94 {
95 _PyErr_WriteUnraisableMsg("at Python startup", NULL);
96 }
97 }
98#endif
99}
100
Masayuki Yamamotoaa0aa042017-07-03 20:34:38 +0900101#if defined(_POSIX_THREADS)
102# define PYTHREAD_NAME "pthread"
103# include "thread_pthread.h"
104#elif defined(NT_THREADS)
105# define PYTHREAD_NAME "nt"
106# include "thread_nt.h"
107#else
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900108# error "Require native threads. See https://bugs.python.org/issue31370"
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000109#endif
110
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000111
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112/* return the current thread stack size */
113size_t
114PyThread_get_stacksize(void)
115{
Victor Stinner81a7be32020-04-14 15:14:01 +0200116 return _PyInterpreterState_GET()->pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000117}
118
119/* Only platforms defining a THREAD_SET_STACKSIZE() macro
120 in thread_<platform>.h support changing the stack size.
121 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 -1 if stack size value is invalid,
123 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124int
125PyThread_set_stacksize(size_t size)
126{
127#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000129#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000131#endif
132}
133
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000134
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900135/* Thread Specific Storage (TSS) API
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000136
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900137 Cross-platform components of TSS API implementation.
138*/
Tim Petersfda787f2004-10-09 22:33:09 +0000139
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900140Py_tss_t *
141PyThread_tss_alloc(void)
142{
143 Py_tss_t *new_key = (Py_tss_t *)PyMem_RawMalloc(sizeof(Py_tss_t));
144 if (new_key == NULL) {
145 return NULL;
146 }
147 new_key->_is_initialized = 0;
148 return new_key;
149}
Tim Petersfda787f2004-10-09 22:33:09 +0000150
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900151void
152PyThread_tss_free(Py_tss_t *key)
153{
154 if (key != NULL) {
155 PyThread_tss_delete(key);
156 PyMem_RawFree((void *)key);
157 }
158}
Tim Petersfda787f2004-10-09 22:33:09 +0000159
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900160int
161PyThread_tss_is_created(Py_tss_t *key)
162{
163 assert(key != NULL);
164 return key->_is_initialized;
165}
Tim Petersfda787f2004-10-09 22:33:09 +0000166
Victor Stinner754851f2011-04-19 23:58:51 +0200167
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200168PyDoc_STRVAR(threadinfo__doc__,
169"sys.thread_info\n\
170\n\
Paul Ganssle2bb6bf02019-09-12 03:50:29 +0100171A named tuple holding information about the thread implementation.");
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200172
173static PyStructSequence_Field threadinfo_fields[] = {
174 {"name", "name of the thread implementation"},
175 {"lock", "name of the lock implementation"},
176 {"version", "name and version of the thread library"},
177 {0}
178};
179
180static PyStructSequence_Desc threadinfo_desc = {
181 "sys.thread_info", /* name */
182 threadinfo__doc__, /* doc */
183 threadinfo_fields, /* fields */
184 3
185};
186
187static PyTypeObject ThreadInfoType;
188
Victor Stinner754851f2011-04-19 23:58:51 +0200189PyObject*
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200190PyThread_GetInfo(void)
Victor Stinner754851f2011-04-19 23:58:51 +0200191{
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200192 PyObject *threadinfo, *value;
193 int pos = 0;
Victor Stinnere07f5222011-04-20 12:23:26 +0200194#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
195 && defined(_CS_GNU_LIBPTHREAD_VERSION))
Victor Stinner754851f2011-04-19 23:58:51 +0200196 char buffer[255];
197 int len;
Victor Stinnere07f5222011-04-20 12:23:26 +0200198#endif
Victor Stinner754851f2011-04-19 23:58:51 +0200199
Victor Stinner1c8f0592013-07-22 22:24:54 +0200200 if (ThreadInfoType.tp_name == 0) {
201 if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0)
202 return NULL;
203 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200204
205 threadinfo = PyStructSequence_New(&ThreadInfoType);
206 if (threadinfo == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200207 return NULL;
208
209 value = PyUnicode_FromString(PYTHREAD_NAME);
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200210 if (value == NULL) {
211 Py_DECREF(threadinfo);
212 return NULL;
213 }
214 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200215
216#ifdef _POSIX_THREADS
217#ifdef USE_SEMAPHORES
218 value = PyUnicode_FromString("semaphore");
219#else
220 value = PyUnicode_FromString("mutex+cond");
221#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200222 if (value == NULL) {
223 Py_DECREF(threadinfo);
Victor Stinner754851f2011-04-19 23:58:51 +0200224 return NULL;
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200225 }
226#else
227 Py_INCREF(Py_None);
228 value = Py_None;
229#endif
230 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200231
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200232#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
233 && defined(_CS_GNU_LIBPTHREAD_VERSION))
234 value = NULL;
Victor Stinner754851f2011-04-19 23:58:51 +0200235 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
Victor Stinner98ea54c2014-08-15 23:30:40 +0200236 if (1 < len && (size_t)len < sizeof(buffer)) {
Victor Stinner754851f2011-04-19 23:58:51 +0200237 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
238 if (value == NULL)
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200239 PyErr_Clear();
Victor Stinner754851f2011-04-19 23:58:51 +0200240 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200241 if (value == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200242#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200243 {
244 Py_INCREF(Py_None);
245 value = Py_None;
246 }
247 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
248 return threadinfo;
Victor Stinner754851f2011-04-19 23:58:51 +0200249}