blob: 82a00e72ba7b501bd66e472aec0a2630dd0fad8a [file] [log] [blame]
Guido van Rossum07bd90e2000-05-08 13:41:38 +00001
2/* GNU pth threads interface
3 http://www.gnu.org/software/pth
4 2000-05-03 Andy Dustman <andy@dustman.net>
5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006 Adapted from Posix threads interface
Guido van Rossum07bd90e2000-05-08 13:41:38 +00007 12 May 1997 -- david arnold <davida@pobox.com>
8 */
9
10#include <stdlib.h>
11#include <string.h>
12#include <pth.h>
13
14/* A pth mutex isn't sufficient to model the Python lock type
15 * because pth mutexes can be acquired multiple times by the
16 * same thread.
17 *
18 * The pth_lock struct implements a Python lock as a "locked?" bit
19 * and a <condition, mutex> pair. In general, if the bit can be acquired
20 * instantly, it is, else the pair is used to block the thread until the
21 * bit is cleared.
22 */
23
24typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 char locked; /* 0=unlocked, 1=locked */
26 /* a <cond, mutex> pair to handle an acquire of a locked lock */
27 pth_cond_t lock_released;
28 pth_mutex_t mut;
Guido van Rossum07bd90e2000-05-08 13:41:38 +000029} pth_lock;
30
31#define CHECK_STATUS(name) if (status == -1) { printf("%d ", status); perror(name); error = 1; }
32
Martin v. Löwis70849f82003-09-20 11:13:36 +000033pth_attr_t PyThread_attr;
34
Guido van Rossum07bd90e2000-05-08 13:41:38 +000035/*
36 * Initialization.
37 */
38
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000039static void PyThread__init_thread(void)
Guido van Rossum07bd90e2000-05-08 13:41:38 +000040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 pth_init();
42 PyThread_attr = pth_attr_new();
43 pth_attr_set(PyThread_attr, PTH_ATTR_STACK_SIZE, 1<<18);
44 pth_attr_set(PyThread_attr, PTH_ATTR_JOINABLE, FALSE);
Guido van Rossum07bd90e2000-05-08 13:41:38 +000045}
46
47/*
48 * Thread support.
49 */
50
51
Guido van Rossum3c288632001-10-16 21:13:49 +000052long PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum07bd90e2000-05-08 13:41:38 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 pth_t th;
55 dprintf(("PyThread_start_new_thread called\n"));
56 if (!initialized)
57 PyThread_init_thread();
Guido van Rossum07bd90e2000-05-08 13:41:38 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 th = pth_spawn(PyThread_attr,
60 (void* (*)(void *))func,
61 (void *)arg
62 );
Guido van Rossum07bd90e2000-05-08 13:41:38 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return th;
Guido van Rossum07bd90e2000-05-08 13:41:38 +000065}
66
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000067long PyThread_get_thread_ident(void)
Guido van Rossum07bd90e2000-05-08 13:41:38 +000068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 volatile pth_t threadid;
70 if (!initialized)
71 PyThread_init_thread();
72 /* Jump through some hoops for Alpha OSF/1 */
73 threadid = pth_self();
74 return (long) *(long *) &threadid;
Guido van Rossum07bd90e2000-05-08 13:41:38 +000075}
76
Amaury Forgeot d'Arc72aee3d2010-02-24 00:10:48 +000077void PyThread_exit_thread(void)
Guido van Rossum07bd90e2000-05-08 13:41:38 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 dprintf(("PyThread_exit_thread called\n"));
80 if (!initialized) {
81 exit(0);
82 }
Guido van Rossum07bd90e2000-05-08 13:41:38 +000083}
84
Guido van Rossum07bd90e2000-05-08 13:41:38 +000085/*
86 * Lock support.
87 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000088PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossum07bd90e2000-05-08 13:41:38 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 pth_lock *lock;
91 int status, error = 0;
Guido van Rossum07bd90e2000-05-08 13:41:38 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 dprintf(("PyThread_allocate_lock called\n"));
94 if (!initialized)
95 PyThread_init_thread();
Guido van Rossum07bd90e2000-05-08 13:41:38 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 lock = (pth_lock *) malloc(sizeof(pth_lock));
98 memset((void *)lock, '\0', sizeof(pth_lock));
99 if (lock) {
100 lock->locked = 0;
101 status = pth_mutex_init(&lock->mut);
102 CHECK_STATUS("pth_mutex_init");
103 status = pth_cond_init(&lock->lock_released);
104 CHECK_STATUS("pth_cond_init");
105 if (error) {
106 free((void *)lock);
107 lock = NULL;
108 }
109 }
110 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
111 return (PyThread_type_lock) lock;
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000112}
113
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000114void PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 pth_lock *thelock = (pth_lock *)lock;
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 free((void *)thelock);
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000121}
122
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000123int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 int success;
126 pth_lock *thelock = (pth_lock *)lock;
127 int status, error = 0;
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL);
132 CHECK_STATUS("pth_mutex_acquire[1]");
133 success = thelock->locked == 0;
134 if (success) thelock->locked = 1;
135 status = pth_mutex_release( &thelock->mut );
136 CHECK_STATUS("pth_mutex_release[1]");
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if ( !success && waitflag ) {
139 /* continue trying until we get the lock */
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 /* mut must be locked by me -- part of the condition
142 * protocol */
143 status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL );
144 CHECK_STATUS("pth_mutex_acquire[2]");
145 while ( thelock->locked ) {
146 status = pth_cond_await(&thelock->lock_released,
147 &thelock->mut, NULL);
148 CHECK_STATUS("pth_cond_await");
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 thelock->locked = 1;
151 status = pth_mutex_release( &thelock->mut );
152 CHECK_STATUS("pth_mutex_release[2]");
153 success = 1;
154 }
155 if (error) success = 0;
156 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
157 return success;
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000158}
159
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160void PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 pth_lock *thelock = (pth_lock *)lock;
163 int status, error = 0;
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 status = pth_mutex_acquire( &thelock->mut, 0, NULL );
168 CHECK_STATUS("pth_mutex_acquire[3]");
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 thelock->locked = 0;
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 status = pth_mutex_release( &thelock->mut );
173 CHECK_STATUS("pth_mutex_release[3]");
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 /* wake up someone (anyone, if any) waiting on the lock */
176 status = pth_cond_notify( &thelock->lock_released, 0 );
177 CHECK_STATUS("pth_cond_notify");
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000178}