blob: 07bd190d2700ffaa1552a290ef1d244d881b8723 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001
Guido van Rossum3ecebf11996-07-30 16:48:31 +00002#include <mach/cthreads.h>
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00003
4
5/*
6 * Initialization.
7 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00008static void
9PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000010{
11 cthread_init();
12}
13
14/*
15 * Thread support.
16 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000017int
18PyThread_start_new_thread(func, void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000019{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000020 int success = 0; /* init not needed when SOLARIS_THREADS and */
21 /* C_THREADS implemented properly */
22
Guido van Rossum65d5b571998-12-21 19:32:43 +000023 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000024 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000025 PyThread_init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000026 /* looks like solaris detaches the thread to never rejoin
27 * so well do it here
28 */
29 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000030 return success < 0 ? 0 : 1;
31}
32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000033long
34PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +000035{
36 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000037 PyThread_init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000038 return (long) cthread_self();
Guido van Rossume944da81994-05-23 12:43:41 +000039}
40
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000041static void
42do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000043{
Guido van Rossum65d5b571998-12-21 19:32:43 +000044 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000045 if (!initialized)
46 if (no_cleanup)
47 _exit(0);
48 else
49 exit(0);
50 cthread_exit(0);
51}
52
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053void
54PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000055{
Guido van Rossum65d5b571998-12-21 19:32:43 +000056 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000057}
58
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000059void
60PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000061{
Guido van Rossum65d5b571998-12-21 19:32:43 +000062 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000063}
64
65#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066static
67void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000068{
Guido van Rossum65d5b571998-12-21 19:32:43 +000069 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000070 if (!initialized)
71 if (no_cleanup)
72 _exit(status);
73 else
74 exit(status);
Guido van Rossum3ecebf11996-07-30 16:48:31 +000075 if (no_cleanup)
76 _exit(status);
77 else
78 exit(status);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000079}
80
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000081void
82PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000083{
Guido van Rossum65d5b571998-12-21 19:32:43 +000084 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000085}
86
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000087void
88PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089{
Guido van Rossum65d5b571998-12-21 19:32:43 +000090 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000091}
92#endif /* NO_EXIT_PROG */
93
94/*
95 * Lock support.
96 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000097PyThread_type_lock
98PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000099{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000100 mutex_t lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000101
Guido van Rossum65d5b571998-12-21 19:32:43 +0000102 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000103 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000104 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000105
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000106 lock = mutex_alloc();
107 if (mutex_init(lock)) {
108 perror("mutex_init");
109 free((void *) lock);
110 lock = 0;
111 }
Fred Drakea44d3532000-06-30 15:01:00 +0000112 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000113 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000114}
115
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116void
117PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000118{
Fred Drakea44d3532000-06-30 15:01:00 +0000119 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000120 mutex_free(lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000121}
122
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000123int
124PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000125{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000126 int success = FALSE;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127
Fred Drakea44d3532000-06-30 15:01:00 +0000128 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000129 if (waitflag) { /* blocking */
130 mutex_lock(lock);
131 success = TRUE;
132 } else { /* non blocking */
133 success = mutex_try_lock(lock);
134 }
Fred Drakea44d3532000-06-30 15:01:00 +0000135 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000136 return success;
137}
138
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000139void
140PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000141{
Fred Drakea44d3532000-06-30 15:01:00 +0000142 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000143 mutex_unlock((mutex_t )lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000144}
145
146/*
147 * Semaphore support.
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000148 *
149 * This implementation is ripped directly from the pthreads implementation.
150 * Which is to say that it is 100% non-functional at this time.
151 *
152 * Assuming the page is still up, documentation can be found at:
153 *
154 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
155 *
156 * Looking at the man page, it seems that one could easily implement a
157 * semaphore using a condition.
158 *
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000159 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000160PyThread_type_sema
161PyThread_allocate_sema(int value)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000162{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000163 char *sema = 0;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000164 dprintf(("PyThread_allocate_sema called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000165 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000166 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000167
Fred Drakea44d3532000-06-30 15:01:00 +0000168 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000169 return (PyThread_type_sema) sema;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000170}
171
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000172void
173PyThread_free_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000174{
Fred Drakea44d3532000-06-30 15:01:00 +0000175 dprintf(("PyThread_free_sema(%p) called\n", sema));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000176}
177
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000178int
179PyThread_down_sema(PyThread_type_sema sema, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000180{
Fred Drakea44d3532000-06-30 15:01:00 +0000181 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
182 dprintf(("PyThread_down_sema(%p) return\n", sema));
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000183 return -1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000184}
185
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000186void
187PyThread_up_sema(PyThread_type_sema sema)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000188{
Fred Drakea44d3532000-06-30 15:01:00 +0000189 dprintf(("PyThread_up_sema(%p)\n", sema));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000190}