Update for release.
[python/dscho.git] / Python / thread_pth.h
blob3b97981ff9ebcd07aa0df1f693a244daf66e25b0
2 /* GNU pth threads interface
3 http://www.gnu.org/software/pth
4 2000-05-03 Andy Dustman <andy@dustman.net>
6 Adapted from Posix threads interface
7 12 May 1997 -- david arnold <davida@pobox.com>
8 */
10 #include <stdlib.h>
11 #include <string.h>
12 #include <pth.h>
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.
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.
24 typedef struct {
25 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;
29 } pth_lock;
31 #define CHECK_STATUS(name) if (status == -1) { printf("%d ", status); perror(name); error = 1; }
34 * Initialization.
37 static void PyThread__init_thread(void)
39 pth_init();
43 * Thread support.
47 long PyThread_start_new_thread(void (*func)(void *), void *arg)
49 pth_t th;
50 dprintf(("PyThread_start_new_thread called\n"));
51 if (!initialized)
52 PyThread_init_thread();
54 th = pth_spawn(PTH_ATTR_DEFAULT,
55 (void* (*)(void *))func,
56 (void *)arg
59 return th;
62 long PyThread_get_thread_ident(void)
64 volatile pth_t threadid;
65 if (!initialized)
66 PyThread_init_thread();
67 /* Jump through some hoops for Alpha OSF/1 */
68 threadid = pth_self();
69 return (long) *(long *) &threadid;
72 static void do_PyThread_exit_thread(int no_cleanup)
74 dprintf(("PyThread_exit_thread called\n"));
75 if (!initialized) {
76 if (no_cleanup)
77 _exit(0);
78 else
79 exit(0);
83 void PyThread_exit_thread(void)
85 do_PyThread_exit_thread(0);
88 void PyThread__exit_thread(void)
90 do_PyThread_exit_thread(1);
93 #ifndef NO_EXIT_PROG
94 static void do_PyThread_exit_prog(int status, int no_cleanup)
96 dprintf(("PyThread_exit_prog(%d) called\n", status));
97 if (!initialized)
98 if (no_cleanup)
99 _exit(status);
100 else
101 exit(status);
104 void PyThread_exit_prog(int status)
106 do_PyThread_exit_prog(status, 0);
109 void PyThread__exit_prog(int status)
111 do_PyThread_exit_prog(status, 1);
113 #endif /* NO_EXIT_PROG */
116 * Lock support.
118 PyThread_type_lock PyThread_allocate_lock(void)
120 pth_lock *lock;
121 int status, error = 0;
123 dprintf(("PyThread_allocate_lock called\n"));
124 if (!initialized)
125 PyThread_init_thread();
127 lock = (pth_lock *) malloc(sizeof(pth_lock));
128 memset((void *)lock, '\0', sizeof(pth_lock));
129 if (lock) {
130 lock->locked = 0;
131 status = pth_mutex_init(&lock->mut);
132 CHECK_STATUS("pth_mutex_init");
133 status = pth_cond_init(&lock->lock_released);
134 CHECK_STATUS("pth_cond_init");
135 if (error) {
136 free((void *)lock);
137 lock = NULL;
140 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
141 return (PyThread_type_lock) lock;
144 void PyThread_free_lock(PyThread_type_lock lock)
146 pth_lock *thelock = (pth_lock *)lock;
148 dprintf(("PyThread_free_lock(%p) called\n", lock));
150 free((void *)thelock);
153 int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
155 int success;
156 pth_lock *thelock = (pth_lock *)lock;
157 int status, error = 0;
159 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
161 status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL);
162 CHECK_STATUS("pth_mutex_acquire[1]");
163 success = thelock->locked == 0;
164 if (success) thelock->locked = 1;
165 status = pth_mutex_release( &thelock->mut );
166 CHECK_STATUS("pth_mutex_release[1]");
168 if ( !success && waitflag ) {
169 /* continue trying until we get the lock */
171 /* mut must be locked by me -- part of the condition
172 * protocol */
173 status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL );
174 CHECK_STATUS("pth_mutex_acquire[2]");
175 while ( thelock->locked ) {
176 status = pth_cond_await(&thelock->lock_released,
177 &thelock->mut, NULL);
178 CHECK_STATUS("pth_cond_await");
180 thelock->locked = 1;
181 status = pth_mutex_release( &thelock->mut );
182 CHECK_STATUS("pth_mutex_release[2]");
183 success = 1;
185 if (error) success = 0;
186 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
187 return success;
190 void PyThread_release_lock(PyThread_type_lock lock)
192 pth_lock *thelock = (pth_lock *)lock;
193 int status, error = 0;
195 dprintf(("PyThread_release_lock(%p) called\n", lock));
197 status = pth_mutex_acquire( &thelock->mut, 0, NULL );
198 CHECK_STATUS("pth_mutex_acquire[3]");
200 thelock->locked = 0;
202 status = pth_mutex_release( &thelock->mut );
203 CHECK_STATUS("pth_mutex_release[3]");
205 /* wake up someone (anyone, if any) waiting on the lock */
206 status = pth_cond_notify( &thelock->lock_released, 0 );
207 CHECK_STATUS("pth_cond_notify");