move sections
[python/dscho.git] / Python / thread_cthread.h
blob1b3e3904cdcc38c23096502e47303a1899875ca5
2 #ifdef MACH_C_THREADS
3 #include <mach/cthreads.h>
4 #endif
6 #ifdef HURD_C_THREADS
7 #include <cthreads.h>
8 #endif
11 * Initialization.
13 static void
14 PyThread__init_thread(void)
16 #ifndef HURD_C_THREADS
17 /* Roland McGrath said this should not be used since this is
18 done while linking to threads */
19 cthread_init();
20 #else
21 /* do nothing */
23 #endif
27 * Thread support.
29 long
30 PyThread_start_new_thread(void (*func)(void *), void *arg)
32 int success = 0; /* init not needed when SOLARIS_THREADS and */
33 /* C_THREADS implemented properly */
35 dprintf(("PyThread_start_new_thread called\n"));
36 if (!initialized)
37 PyThread_init_thread();
38 /* looks like solaris detaches the thread to never rejoin
39 * so well do it here
41 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
42 return success < 0 ? -1 : 0;
45 long
46 PyThread_get_thread_ident(void)
48 if (!initialized)
49 PyThread_init_thread();
50 return (long) cthread_self();
53 void
54 PyThread_exit_thread(void)
56 dprintf(("PyThread_exit_thread called\n"));
57 if (!initialized)
58 exit(0);
59 cthread_exit(0);
63 * Lock support.
65 PyThread_type_lock
66 PyThread_allocate_lock(void)
68 mutex_t lock;
70 dprintf(("PyThread_allocate_lock called\n"));
71 if (!initialized)
72 PyThread_init_thread();
74 lock = mutex_alloc();
75 if (mutex_init(lock)) {
76 perror("mutex_init");
77 free((void *) lock);
78 lock = 0;
80 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
81 return (PyThread_type_lock) lock;
84 void
85 PyThread_free_lock(PyThread_type_lock lock)
87 dprintf(("PyThread_free_lock(%p) called\n", lock));
88 mutex_free(lock);
91 int
92 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
94 int success = FALSE;
96 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
97 if (waitflag) { /* blocking */
98 mutex_lock((mutex_t)lock);
99 success = TRUE;
100 } else { /* non blocking */
101 success = mutex_try_lock((mutex_t)lock);
103 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
104 return success;
107 void
108 PyThread_release_lock(PyThread_type_lock lock)
110 dprintf(("PyThread_release_lock(%p) called\n", lock));
111 mutex_unlock((mutex_t )lock);