Updated for 2.1b2 distribution.
[python/dscho.git] / Python / thread_cthread.h
blob080505134dde6115d4530fe4d9cab4b85d893a40
2 #include <mach/cthreads.h>
5 /*
6 * Initialization.
7 */
8 static void
9 PyThread__init_thread(void)
11 cthread_init();
15 * Thread support.
17 int
18 PyThread_start_new_thread(void (*func)(void *), void *arg)
20 int success = 0; /* init not needed when SOLARIS_THREADS and */
21 /* C_THREADS implemented properly */
23 dprintf(("PyThread_start_new_thread called\n"));
24 if (!initialized)
25 PyThread_init_thread();
26 /* looks like solaris detaches the thread to never rejoin
27 * so well do it here
29 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
30 return success < 0 ? 0 : 1;
33 long
34 PyThread_get_thread_ident(void)
36 if (!initialized)
37 PyThread_init_thread();
38 return (long) cthread_self();
41 static void
42 do_PyThread_exit_thread(int no_cleanup)
44 dprintf(("PyThread_exit_thread called\n"));
45 if (!initialized)
46 if (no_cleanup)
47 _exit(0);
48 else
49 exit(0);
50 cthread_exit(0);
53 void
54 PyThread_exit_thread(void)
56 do_PyThread_exit_thread(0);
59 void
60 PyThread__exit_thread(void)
62 do_PyThread_exit_thread(1);
65 #ifndef NO_EXIT_PROG
66 static
67 void do_PyThread_exit_prog(int status, int no_cleanup)
69 dprintf(("PyThread_exit_prog(%d) called\n", status));
70 if (!initialized)
71 if (no_cleanup)
72 _exit(status);
73 else
74 exit(status);
75 if (no_cleanup)
76 _exit(status);
77 else
78 exit(status);
81 void
82 PyThread_exit_prog(int status)
84 do_PyThread_exit_prog(status, 0);
87 void
88 PyThread__exit_prog(int status)
90 do_PyThread_exit_prog(status, 1);
92 #endif /* NO_EXIT_PROG */
95 * Lock support.
97 PyThread_type_lock
98 PyThread_allocate_lock(void)
100 mutex_t lock;
102 dprintf(("PyThread_allocate_lock called\n"));
103 if (!initialized)
104 PyThread_init_thread();
106 lock = mutex_alloc();
107 if (mutex_init(lock)) {
108 perror("mutex_init");
109 free((void *) lock);
110 lock = 0;
112 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
113 return (PyThread_type_lock) lock;
116 void
117 PyThread_free_lock(PyThread_type_lock lock)
119 dprintf(("PyThread_free_lock(%p) called\n", lock));
120 mutex_free(lock);
124 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
126 int success = FALSE;
128 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
129 if (waitflag) { /* blocking */
130 mutex_lock(lock);
131 success = TRUE;
132 } else { /* non blocking */
133 success = mutex_try_lock(lock);
135 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
136 return success;
139 void
140 PyThread_release_lock(PyThread_type_lock lock)
142 dprintf(("PyThread_release_lock(%p) called\n", lock));
143 mutex_unlock((mutex_t )lock);
147 * Semaphore support.
149 * This implementation is ripped directly from the pthreads implementation.
150 * Which is to say that it is 100% non-functional at this time.
152 * Assuming the page is still up, documentation can be found at:
154 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
156 * Looking at the man page, it seems that one could easily implement a
157 * semaphore using a condition.
160 PyThread_type_sema
161 PyThread_allocate_sema(int value)
163 char *sema = 0;
164 dprintf(("PyThread_allocate_sema called\n"));
165 if (!initialized)
166 PyThread_init_thread();
168 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
169 return (PyThread_type_sema) sema;
172 void
173 PyThread_free_sema(PyThread_type_sema sema)
175 dprintf(("PyThread_free_sema(%p) called\n", sema));
179 PyThread_down_sema(PyThread_type_sema sema, int waitflag)
181 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
182 dprintf(("PyThread_down_sema(%p) return\n", sema));
183 return -1;
186 void
187 PyThread_up_sema(PyThread_type_sema sema)
189 dprintf(("PyThread_up_sema(%p)\n", sema));