1 
2 #ifdef MACH_C_THREADS
3 #include <mach/cthreads.h>
4 #endif
5 
6 #ifdef HURD_C_THREADS
7 #include <cthreads.h>
8 #endif
9 
10 /*
11  * Initialization.
12  */
13 static void
PyThread__init_thread(void)14 PyThread__init_thread(void)
15 {
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 */
22     ;
23 #endif
24 }
25 
26 /*
27  * Thread support.
28  */
29 long
PyThread_start_new_thread(void (* func)(void *),void * arg)30 PyThread_start_new_thread(void (*func)(void *), void *arg)
31 {
32     int success = 0;            /* init not needed when SOLARIS_THREADS and */
33                 /* C_THREADS implemented properly */
34 
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
40      */
41     cthread_detach(cthread_fork((cthread_fn_t) func, arg));
42     return success < 0 ? -1 : 0;
43 }
44 
45 long
PyThread_get_thread_ident(void)46 PyThread_get_thread_ident(void)
47 {
48     if (!initialized)
49         PyThread_init_thread();
50     return (long) cthread_self();
51 }
52 
53 void
PyThread_exit_thread(void)54 PyThread_exit_thread(void)
55 {
56     dprintf(("PyThread_exit_thread called\n"));
57     if (!initialized)
58         exit(0);
59     cthread_exit(0);
60 }
61 
62 /*
63  * Lock support.
64  */
65 PyThread_type_lock
PyThread_allocate_lock(void)66 PyThread_allocate_lock(void)
67 {
68     mutex_t lock;
69 
70     dprintf(("PyThread_allocate_lock called\n"));
71     if (!initialized)
72         PyThread_init_thread();
73 
74     lock = mutex_alloc();
75     if (mutex_init(lock)) {
76         perror("mutex_init");
77         free((void *) lock);
78         lock = 0;
79     }
80     dprintf(("PyThread_allocate_lock() -> %p\n", lock));
81     return (PyThread_type_lock) lock;
82 }
83 
84 void
PyThread_free_lock(PyThread_type_lock lock)85 PyThread_free_lock(PyThread_type_lock lock)
86 {
87     dprintf(("PyThread_free_lock(%p) called\n", lock));
88     mutex_free(lock);
89 }
90 
91 int
PyThread_acquire_lock(PyThread_type_lock lock,int waitflag)92 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
93 {
94     int success = FALSE;
95 
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);
102     }
103     dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
104     return success;
105 }
106 
107 void
PyThread_release_lock(PyThread_type_lock lock)108 PyThread_release_lock(PyThread_type_lock lock)
109 {
110     dprintf(("PyThread_release_lock(%p) called\n", lock));
111     mutex_unlock((mutex_t )lock);
112 }
113