You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

421 lines
11 KiB

31 years ago
  1. /* Thread package.
  2. This is intended to be usable independently from Python.
  3. The implementation for system foobar is in a file thread_foobar.h
  4. which is included by this file dependent on config settings.
  5. Stuff shared by all thread_*.h files is collected here. */
  6. #include "Python.h"
  7. #ifndef _POSIX_THREADS
  8. /* This means pthreads are not implemented in libc headers, hence the macro
  9. not present in unistd.h. But they still can be implemented as an external
  10. library (e.g. gnu pth in pthread emulation) */
  11. # ifdef HAVE_PTHREAD_H
  12. # include <pthread.h> /* _POSIX_THREADS */
  13. # endif
  14. #endif
  15. #ifndef DONT_HAVE_STDIO_H
  16. #include <stdio.h>
  17. #endif
  18. #include <stdlib.h>
  19. #ifdef __sgi
  20. #ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.ac */
  21. #undef _POSIX_THREADS
  22. #endif
  23. #endif
  24. #include "pythread.h"
  25. #ifndef _POSIX_THREADS
  26. #ifdef __sgi
  27. #define SGI_THREADS
  28. #endif
  29. #ifdef HAVE_THREAD_H
  30. #define SOLARIS_THREADS
  31. #endif
  32. #if defined(sun) && !defined(SOLARIS_THREADS)
  33. #define SUN_LWP
  34. #endif
  35. /* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
  36. enough of the Posix threads package is implemented to support python
  37. threads.
  38. This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
  39. a check of __ia64 to verify that we're running on a ia64 system instead
  40. of a pa-risc system.
  41. */
  42. #ifdef __hpux
  43. #ifdef _SC_THREADS
  44. #define _POSIX_THREADS
  45. #endif
  46. #endif
  47. #endif /* _POSIX_THREADS */
  48. #ifdef Py_DEBUG
  49. static int thread_debug = 0;
  50. #define dprintf(args) (void)((thread_debug & 1) && printf args)
  51. #define d2printf(args) ((thread_debug & 8) && printf args)
  52. #else
  53. #define dprintf(args)
  54. #define d2printf(args)
  55. #endif
  56. static int initialized;
  57. static void PyThread__init_thread(void); /* Forward */
  58. void
  59. PyThread_init_thread(void)
  60. {
  61. #ifdef Py_DEBUG
  62. char *p = Py_GETENV("PYTHONTHREADDEBUG");
  63. if (p) {
  64. if (*p)
  65. thread_debug = atoi(p);
  66. else
  67. thread_debug = 1;
  68. }
  69. #endif /* Py_DEBUG */
  70. if (initialized)
  71. return;
  72. initialized = 1;
  73. dprintf(("PyThread_init_thread called\n"));
  74. PyThread__init_thread();
  75. }
  76. /* Support for runtime thread stack size tuning.
  77. A value of 0 means using the platform's default stack size
  78. or the size specified by the THREAD_STACK_SIZE macro. */
  79. static size_t _pythread_stacksize = 0;
  80. #ifdef SGI_THREADS
  81. #include "thread_sgi.h"
  82. #endif
  83. #ifdef SOLARIS_THREADS
  84. #include "thread_solaris.h"
  85. #endif
  86. #ifdef SUN_LWP
  87. #include "thread_lwp.h"
  88. #endif
  89. #ifdef HAVE_PTH
  90. #include "thread_pth.h"
  91. #undef _POSIX_THREADS
  92. #endif
  93. #ifdef _POSIX_THREADS
  94. #include "thread_pthread.h"
  95. #endif
  96. #ifdef C_THREADS
  97. #include "thread_cthread.h"
  98. #endif
  99. #ifdef NT_THREADS
  100. #include "thread_nt.h"
  101. #endif
  102. #ifdef OS2_THREADS
  103. #include "thread_os2.h"
  104. #endif
  105. #ifdef BEOS_THREADS
  106. #include "thread_beos.h"
  107. #endif
  108. #ifdef PLAN9_THREADS
  109. #include "thread_plan9.h"
  110. #endif
  111. #ifdef ATHEOS_THREADS
  112. #include "thread_atheos.h"
  113. #endif
  114. /*
  115. #ifdef FOOBAR_THREADS
  116. #include "thread_foobar.h"
  117. #endif
  118. */
  119. /* return the current thread stack size */
  120. size_t
  121. PyThread_get_stacksize(void)
  122. {
  123. return _pythread_stacksize;
  124. }
  125. /* Only platforms defining a THREAD_SET_STACKSIZE() macro
  126. in thread_<platform>.h support changing the stack size.
  127. Return 0 if stack size is valid,
  128. -1 if stack size value is invalid,
  129. -2 if setting stack size is not supported. */
  130. int
  131. PyThread_set_stacksize(size_t size)
  132. {
  133. #if defined(THREAD_SET_STACKSIZE)
  134. return THREAD_SET_STACKSIZE(size);
  135. #else
  136. return -2;
  137. #endif
  138. }
  139. #ifndef Py_HAVE_NATIVE_TLS
  140. /* If the platform has not supplied a platform specific
  141. TLS implementation, provide our own.
  142. This code stolen from "thread_sgi.h", where it was the only
  143. implementation of an existing Python TLS API.
  144. */
  145. /* ------------------------------------------------------------------------
  146. Per-thread data ("key") support.
  147. Use PyThread_create_key() to create a new key. This is typically shared
  148. across threads.
  149. Use PyThread_set_key_value(thekey, value) to associate void* value with
  150. thekey in the current thread. Each thread has a distinct mapping of thekey
  151. to a void* value. Caution: if the current thread already has a mapping
  152. for thekey, value is ignored.
  153. Use PyThread_get_key_value(thekey) to retrieve the void* value associated
  154. with thekey in the current thread. This returns NULL if no value is
  155. associated with thekey in the current thread.
  156. Use PyThread_delete_key_value(thekey) to forget the current thread's associated
  157. value for thekey. PyThread_delete_key(thekey) forgets the values associated
  158. with thekey across *all* threads.
  159. While some of these functions have error-return values, none set any
  160. Python exception.
  161. None of the functions does memory management on behalf of the void* values.
  162. You need to allocate and deallocate them yourself. If the void* values
  163. happen to be PyObject*, these functions don't do refcount operations on
  164. them either.
  165. The GIL does not need to be held when calling these functions; they supply
  166. their own locking. This isn't true of PyThread_create_key(), though (see
  167. next paragraph).
  168. There's a hidden assumption that PyThread_create_key() will be called before
  169. any of the other functions are called. There's also a hidden assumption
  170. that calls to PyThread_create_key() are serialized externally.
  171. ------------------------------------------------------------------------ */
  172. /* A singly-linked list of struct key objects remembers all the key->value
  173. * associations. File static keyhead heads the list. keymutex is used
  174. * to enforce exclusion internally.
  175. */
  176. struct key {
  177. /* Next record in the list, or NULL if this is the last record. */
  178. struct key *next;
  179. /* The thread id, according to PyThread_get_thread_ident(). */
  180. long id;
  181. /* The key and its associated value. */
  182. int key;
  183. void *value;
  184. };
  185. static struct key *keyhead = NULL;
  186. static PyThread_type_lock keymutex = NULL;
  187. static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
  188. /* Internal helper.
  189. * If the current thread has a mapping for key, the appropriate struct key*
  190. * is returned. NB: value is ignored in this case!
  191. * If there is no mapping for key in the current thread, then:
  192. * If value is NULL, NULL is returned.
  193. * Else a mapping of key to value is created for the current thread,
  194. * and a pointer to a new struct key* is returned; except that if
  195. * malloc() can't find room for a new struct key*, NULL is returned.
  196. * So when value==NULL, this acts like a pure lookup routine, and when
  197. * value!=NULL, this acts like dict.setdefault(), returning an existing
  198. * mapping if one exists, else creating a new mapping.
  199. *
  200. * Caution: this used to be too clever, trying to hold keymutex only
  201. * around the "p->next = keyhead; keyhead = p" pair. That allowed
  202. * another thread to mutate the list, via key deletion, concurrent with
  203. * find_key() crawling over the list. Hilarity ensued. For example, when
  204. * the for-loop here does "p = p->next", p could end up pointing at a
  205. * record that PyThread_delete_key_value() was concurrently free()'ing.
  206. * That could lead to anything, from failing to find a key that exists, to
  207. * segfaults. Now we lock the whole routine.
  208. */
  209. static struct key *
  210. find_key(int key, void *value)
  211. {
  212. struct key *p, *prev_p;
  213. long id = PyThread_get_thread_ident();
  214. if (!keymutex)
  215. return NULL;
  216. PyThread_acquire_lock(keymutex, 1);
  217. prev_p = NULL;
  218. for (p = keyhead; p != NULL; p = p->next) {
  219. if (p->id == id && p->key == key)
  220. goto Done;
  221. /* Sanity check. These states should never happen but if
  222. * they do we must abort. Otherwise we'll end up spinning in
  223. * in a tight loop with the lock held. A similar check is done
  224. * in pystate.c tstate_delete_common(). */
  225. if (p == prev_p)
  226. Py_FatalError("tls find_key: small circular list(!)");
  227. prev_p = p;
  228. if (p->next == keyhead)
  229. Py_FatalError("tls find_key: circular list(!)");
  230. }
  231. if (value == NULL) {
  232. assert(p == NULL);
  233. goto Done;
  234. }
  235. p = (struct key *)malloc(sizeof(struct key));
  236. if (p != NULL) {
  237. p->id = id;
  238. p->key = key;
  239. p->value = value;
  240. p->next = keyhead;
  241. keyhead = p;
  242. }
  243. Done:
  244. PyThread_release_lock(keymutex);
  245. return p;
  246. }
  247. /* Return a new key. This must be called before any other functions in
  248. * this family, and callers must arrange to serialize calls to this
  249. * function. No violations are detected.
  250. */
  251. int
  252. PyThread_create_key(void)
  253. {
  254. /* All parts of this function are wrong if it's called by multiple
  255. * threads simultaneously.
  256. */
  257. if (keymutex == NULL)
  258. keymutex = PyThread_allocate_lock();
  259. return ++nkeys;
  260. }
  261. /* Forget the associations for key across *all* threads. */
  262. void
  263. PyThread_delete_key(int key)
  264. {
  265. struct key *p, **q;
  266. PyThread_acquire_lock(keymutex, 1);
  267. q = &keyhead;
  268. while ((p = *q) != NULL) {
  269. if (p->key == key) {
  270. *q = p->next;
  271. free((void *)p);
  272. /* NB This does *not* free p->value! */
  273. }
  274. else
  275. q = &p->next;
  276. }
  277. PyThread_release_lock(keymutex);
  278. }
  279. /* Confusing: If the current thread has an association for key,
  280. * value is ignored, and 0 is returned. Else an attempt is made to create
  281. * an association of key to value for the current thread. 0 is returned
  282. * if that succeeds, but -1 is returned if there's not enough memory
  283. * to create the association. value must not be NULL.
  284. */
  285. int
  286. PyThread_set_key_value(int key, void *value)
  287. {
  288. struct key *p;
  289. assert(value != NULL);
  290. p = find_key(key, value);
  291. if (p == NULL)
  292. return -1;
  293. else
  294. return 0;
  295. }
  296. /* Retrieve the value associated with key in the current thread, or NULL
  297. * if the current thread doesn't have an association for key.
  298. */
  299. void *
  300. PyThread_get_key_value(int key)
  301. {
  302. struct key *p = find_key(key, NULL);
  303. if (p == NULL)
  304. return NULL;
  305. else
  306. return p->value;
  307. }
  308. /* Forget the current thread's association for key, if any. */
  309. void
  310. PyThread_delete_key_value(int key)
  311. {
  312. long id = PyThread_get_thread_ident();
  313. struct key *p, **q;
  314. PyThread_acquire_lock(keymutex, 1);
  315. q = &keyhead;
  316. while ((p = *q) != NULL) {
  317. if (p->key == key && p->id == id) {
  318. *q = p->next;
  319. free((void *)p);
  320. /* NB This does *not* free p->value! */
  321. break;
  322. }
  323. else
  324. q = &p->next;
  325. }
  326. PyThread_release_lock(keymutex);
  327. }
  328. /* Forget everything not associated with the current thread id.
  329. * This function is called from PyOS_AfterFork(). It is necessary
  330. * because other thread ids which were in use at the time of the fork
  331. * may be reused for new threads created in the forked process.
  332. */
  333. void
  334. PyThread_ReInitTLS(void)
  335. {
  336. long id = PyThread_get_thread_ident();
  337. struct key *p, **q;
  338. if (!keymutex)
  339. return;
  340. /* As with interpreter_lock in PyEval_ReInitThreads()
  341. we just create a new lock without freeing the old one */
  342. keymutex = PyThread_allocate_lock();
  343. /* Delete all keys which do not match the current thread id */
  344. q = &keyhead;
  345. while ((p = *q) != NULL) {
  346. if (p->id != id) {
  347. *q = p->next;
  348. free((void *)p);
  349. /* NB This does *not* free p->value! */
  350. }
  351. else
  352. q = &p->next;
  353. }
  354. }
  355. #endif /* Py_HAVE_NATIVE_TLS */