blob: a10f5728dc0ceb8cc560cf830851da862c680679 [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
Guido van Rossum1984f1e1992-08-04 12:41:02 +000044
Guido van Rossum408027e1996-12-30 16:17:54 +000045#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000046static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047#define dprintf(args) (void)((thread_debug & 1) && printf args)
48#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000049#else
50#define dprintf(args)
51#define d2printf(args)
52#endif
53
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054static int initialized;
55
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000056static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000057
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000058void
59PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000060{
Guido van Rossum408027e1996-12-30 16:17:54 +000061#ifdef Py_DEBUG
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +020062 const char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if (p) {
65 if (*p)
66 thread_debug = atoi(p);
67 else
68 thread_debug = 1;
69 }
Guido van Rossum408027e1996-12-30 16:17:54 +000070#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (initialized)
72 return;
73 initialized = 1;
74 dprintf(("PyThread_init_thread called\n"));
75 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000076}
77
Masayuki Yamamotoaa0aa042017-07-03 20:34:38 +090078#if defined(_POSIX_THREADS)
79# define PYTHREAD_NAME "pthread"
80# include "thread_pthread.h"
81#elif defined(NT_THREADS)
82# define PYTHREAD_NAME "nt"
83# include "thread_nt.h"
84#else
Masayuki Yamamoto731e1892017-10-06 19:41:34 +090085# error "Require native threads. See https://bugs.python.org/issue31370"
Guido van Rossumc3f82b61995-01-17 16:29:31 +000086#endif
87
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000088
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089/* return the current thread stack size */
90size_t
91PyThread_get_stacksize(void)
92{
Victor Stinner81a7be32020-04-14 15:14:01 +020093 return _PyInterpreterState_GET()->pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094}
95
96/* Only platforms defining a THREAD_SET_STACKSIZE() macro
97 in thread_<platform>.h support changing the stack size.
98 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 -1 if stack size value is invalid,
100 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101int
102PyThread_set_stacksize(size_t size)
103{
104#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000108#endif
109}
110
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000111
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900112/* Thread Specific Storage (TSS) API
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000113
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900114 Cross-platform components of TSS API implementation.
115*/
Tim Petersfda787f2004-10-09 22:33:09 +0000116
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900117Py_tss_t *
118PyThread_tss_alloc(void)
119{
120 Py_tss_t *new_key = (Py_tss_t *)PyMem_RawMalloc(sizeof(Py_tss_t));
121 if (new_key == NULL) {
122 return NULL;
123 }
124 new_key->_is_initialized = 0;
125 return new_key;
126}
Tim Petersfda787f2004-10-09 22:33:09 +0000127
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900128void
129PyThread_tss_free(Py_tss_t *key)
130{
131 if (key != NULL) {
132 PyThread_tss_delete(key);
133 PyMem_RawFree((void *)key);
134 }
135}
Tim Petersfda787f2004-10-09 22:33:09 +0000136
Masayuki Yamamoto731e1892017-10-06 19:41:34 +0900137int
138PyThread_tss_is_created(Py_tss_t *key)
139{
140 assert(key != NULL);
141 return key->_is_initialized;
142}
Tim Petersfda787f2004-10-09 22:33:09 +0000143
Victor Stinner754851f2011-04-19 23:58:51 +0200144
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200145PyDoc_STRVAR(threadinfo__doc__,
146"sys.thread_info\n\
147\n\
Raymond Hettinger71170742019-09-11 07:17:32 -0700148A named tuple holding information about the thread implementation.");
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200149
150static PyStructSequence_Field threadinfo_fields[] = {
151 {"name", "name of the thread implementation"},
152 {"lock", "name of the lock implementation"},
153 {"version", "name and version of the thread library"},
154 {0}
155};
156
157static PyStructSequence_Desc threadinfo_desc = {
158 "sys.thread_info", /* name */
159 threadinfo__doc__, /* doc */
160 threadinfo_fields, /* fields */
161 3
162};
163
164static PyTypeObject ThreadInfoType;
165
Victor Stinner754851f2011-04-19 23:58:51 +0200166PyObject*
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200167PyThread_GetInfo(void)
Victor Stinner754851f2011-04-19 23:58:51 +0200168{
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200169 PyObject *threadinfo, *value;
170 int pos = 0;
Victor Stinnere07f5222011-04-20 12:23:26 +0200171#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
172 && defined(_CS_GNU_LIBPTHREAD_VERSION))
Victor Stinner754851f2011-04-19 23:58:51 +0200173 char buffer[255];
174 int len;
Victor Stinnere07f5222011-04-20 12:23:26 +0200175#endif
Victor Stinner754851f2011-04-19 23:58:51 +0200176
Victor Stinner1c8f0592013-07-22 22:24:54 +0200177 if (ThreadInfoType.tp_name == 0) {
178 if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0)
179 return NULL;
180 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200181
182 threadinfo = PyStructSequence_New(&ThreadInfoType);
183 if (threadinfo == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200184 return NULL;
185
186 value = PyUnicode_FromString(PYTHREAD_NAME);
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200187 if (value == NULL) {
188 Py_DECREF(threadinfo);
189 return NULL;
190 }
191 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200192
193#ifdef _POSIX_THREADS
194#ifdef USE_SEMAPHORES
195 value = PyUnicode_FromString("semaphore");
196#else
197 value = PyUnicode_FromString("mutex+cond");
198#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200199 if (value == NULL) {
200 Py_DECREF(threadinfo);
Victor Stinner754851f2011-04-19 23:58:51 +0200201 return NULL;
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200202 }
203#else
204 Py_INCREF(Py_None);
205 value = Py_None;
206#endif
207 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200208
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200209#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
210 && defined(_CS_GNU_LIBPTHREAD_VERSION))
211 value = NULL;
Victor Stinner754851f2011-04-19 23:58:51 +0200212 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
Victor Stinner98ea54c2014-08-15 23:30:40 +0200213 if (1 < len && (size_t)len < sizeof(buffer)) {
Victor Stinner754851f2011-04-19 23:58:51 +0200214 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
215 if (value == NULL)
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200216 PyErr_Clear();
Victor Stinner754851f2011-04-19 23:58:51 +0200217 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200218 if (value == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200219#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200220 {
221 Py_INCREF(Py_None);
222 value = Py_None;
223 }
224 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
225 return threadinfo;
Victor Stinner754851f2011-04-19 23:58:51 +0200226}