blob: 6fd594fd30124e98932f06ba477261dcfad5b0b8 [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"
Guido van Rossum1d5735e1994-08-30 08:27:36 +00009
Matthias Klosea2542be2004-08-16 11:35:51 +000010#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 Rossum2571cc81999-04-07 16:07:23 +000019#ifndef DONT_HAVE_STDIO_H
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020#include <stdio.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000021#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023#include <stdlib.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024
Guido van Rossum49b56061998-10-01 20:42:43 +000025#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000026
Guido van Rossum1d5735e1994-08-30 08:27:36 +000027#ifndef _POSIX_THREADS
28
Guido van Rossum539c6622005-09-14 17:49:54 +000029/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melotti13925002011-03-16 11:05:33 +020030 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000031 threads.
32
33 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
Martin Panter7462b6492015-11-02 03:37:02 +000034 a check of __ia64 to verify that we're running on an ia64 system instead
Guido van Rossum539c6622005-09-14 17:49:54 +000035 of a pa-risc system.
36*/
37#ifdef __hpux
38#ifdef _SC_THREADS
39#define _POSIX_THREADS
40#endif
41#endif
42
Sjoerd Mullender66bca321993-12-03 16:54:45 +000043#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000044
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045
Guido van Rossum408027e1996-12-30 16:17:54 +000046#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000047static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define dprintf(args) (void)((thread_debug & 1) && printf args)
49#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000050#else
51#define dprintf(args)
52#define d2printf(args)
53#endif
54
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055static int initialized;
56
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000057static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000058
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000059void
60PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000061{
Guido van Rossum408027e1996-12-30 16:17:54 +000062#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (p) {
66 if (*p)
67 thread_debug = atoi(p);
68 else
69 thread_debug = 1;
70 }
Guido van Rossum408027e1996-12-30 16:17:54 +000071#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (initialized)
73 return;
74 initialized = 1;
75 dprintf(("PyThread_init_thread called\n"));
76 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000077}
78
Masayuki Yamamotoaa0aa042017-07-03 20:34:38 +090079#if defined(_POSIX_THREADS)
80# define PYTHREAD_NAME "pthread"
81# include "thread_pthread.h"
82#elif defined(NT_THREADS)
83# define PYTHREAD_NAME "nt"
84# include "thread_nt.h"
85#else
86# error "Require native thread feature. See https://bugs.python.org/issue30832"
Guido van Rossumc3f82b61995-01-17 16:29:31 +000087#endif
88
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000089
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090/* return the current thread stack size */
91size_t
92PyThread_get_stacksize(void)
93{
Eric Snow76d5abc2017-09-05 18:26:16 -070094 return PyThreadState_GET()->interp->pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095}
96
97/* Only platforms defining a THREAD_SET_STACKSIZE() macro
98 in thread_<platform>.h support changing the stack size.
99 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 -1 if stack size value is invalid,
101 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102int
103PyThread_set_stacksize(size_t size)
104{
105#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000107#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000109#endif
110}
111
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000112
Tim Petersfda787f2004-10-09 22:33:09 +0000113/* ------------------------------------------------------------------------
114Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000115
Tim Petersfda787f2004-10-09 22:33:09 +0000116Use PyThread_create_key() to create a new key. This is typically shared
117across threads.
118
119Use PyThread_set_key_value(thekey, value) to associate void* value with
120thekey in the current thread. Each thread has a distinct mapping of thekey
121to a void* value. Caution: if the current thread already has a mapping
122for thekey, value is ignored.
123
124Use PyThread_get_key_value(thekey) to retrieve the void* value associated
125with thekey in the current thread. This returns NULL if no value is
126associated with thekey in the current thread.
127
128Use PyThread_delete_key_value(thekey) to forget the current thread's associated
129value for thekey. PyThread_delete_key(thekey) forgets the values associated
130with thekey across *all* threads.
131
132While some of these functions have error-return values, none set any
133Python exception.
134
135None of the functions does memory management on behalf of the void* values.
136You need to allocate and deallocate them yourself. If the void* values
137happen to be PyObject*, these functions don't do refcount operations on
138them either.
139
140The GIL does not need to be held when calling these functions; they supply
141their own locking. This isn't true of PyThread_create_key(), though (see
142next paragraph).
143
144There's a hidden assumption that PyThread_create_key() will be called before
145any of the other functions are called. There's also a hidden assumption
146that calls to PyThread_create_key() are serialized externally.
147------------------------------------------------------------------------ */
148
Victor Stinner754851f2011-04-19 23:58:51 +0200149
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200150PyDoc_STRVAR(threadinfo__doc__,
151"sys.thread_info\n\
152\n\
153A struct sequence holding information about the thread implementation.");
154
155static PyStructSequence_Field threadinfo_fields[] = {
156 {"name", "name of the thread implementation"},
157 {"lock", "name of the lock implementation"},
158 {"version", "name and version of the thread library"},
159 {0}
160};
161
162static PyStructSequence_Desc threadinfo_desc = {
163 "sys.thread_info", /* name */
164 threadinfo__doc__, /* doc */
165 threadinfo_fields, /* fields */
166 3
167};
168
169static PyTypeObject ThreadInfoType;
170
Victor Stinner754851f2011-04-19 23:58:51 +0200171PyObject*
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200172PyThread_GetInfo(void)
Victor Stinner754851f2011-04-19 23:58:51 +0200173{
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200174 PyObject *threadinfo, *value;
175 int pos = 0;
Victor Stinnere07f5222011-04-20 12:23:26 +0200176#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
177 && defined(_CS_GNU_LIBPTHREAD_VERSION))
Victor Stinner754851f2011-04-19 23:58:51 +0200178 char buffer[255];
179 int len;
Victor Stinnere07f5222011-04-20 12:23:26 +0200180#endif
Victor Stinner754851f2011-04-19 23:58:51 +0200181
Victor Stinner1c8f0592013-07-22 22:24:54 +0200182 if (ThreadInfoType.tp_name == 0) {
183 if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0)
184 return NULL;
185 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200186
187 threadinfo = PyStructSequence_New(&ThreadInfoType);
188 if (threadinfo == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200189 return NULL;
190
191 value = PyUnicode_FromString(PYTHREAD_NAME);
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200192 if (value == NULL) {
193 Py_DECREF(threadinfo);
194 return NULL;
195 }
196 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200197
198#ifdef _POSIX_THREADS
199#ifdef USE_SEMAPHORES
200 value = PyUnicode_FromString("semaphore");
201#else
202 value = PyUnicode_FromString("mutex+cond");
203#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200204 if (value == NULL) {
205 Py_DECREF(threadinfo);
Victor Stinner754851f2011-04-19 23:58:51 +0200206 return NULL;
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200207 }
208#else
209 Py_INCREF(Py_None);
210 value = Py_None;
211#endif
212 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200213
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200214#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
215 && defined(_CS_GNU_LIBPTHREAD_VERSION))
216 value = NULL;
Victor Stinner754851f2011-04-19 23:58:51 +0200217 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
Victor Stinner98ea54c2014-08-15 23:30:40 +0200218 if (1 < len && (size_t)len < sizeof(buffer)) {
Victor Stinner754851f2011-04-19 23:58:51 +0200219 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
220 if (value == NULL)
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200221 PyErr_Clear();
Victor Stinner754851f2011-04-19 23:58:51 +0200222 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200223 if (value == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200224#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200225 {
226 Py_INCREF(Py_None);
227 value = Py_None;
228 }
229 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
230 return threadinfo;
Victor Stinner754851f2011-04-19 23:58:51 +0200231}