blob: 3f48d592ddb1a201e5f313e1d639e5defcee46fc [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000029
30******************************************************************/
31
Guido van Rossum3ecebf11996-07-30 16:48:31 +000032#include <mach/cthreads.h>
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000033
34
35/*
36 * Initialization.
37 */
38static void _init_thread _P0()
39{
40 cthread_init();
41}
42
43/*
44 * Thread support.
45 */
46int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
47{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000048 int success = 0; /* init not needed when SOLARIS_THREADS and */
49 /* C_THREADS implemented properly */
50
51 dprintf(("start_new_thread called\n"));
52 if (!initialized)
53 init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000054 /* looks like solaris detaches the thread to never rejoin
55 * so well do it here
56 */
57 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000058 return success < 0 ? 0 : 1;
59}
60
Guido van Rossume944da81994-05-23 12:43:41 +000061long get_thread_ident _P0()
62{
63 if (!initialized)
64 init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000065 return (long) cthread_self();
Guido van Rossume944da81994-05-23 12:43:41 +000066}
67
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000068static void do_exit_thread _P1(no_cleanup, int no_cleanup)
69{
70 dprintf(("exit_thread called\n"));
71 if (!initialized)
72 if (no_cleanup)
73 _exit(0);
74 else
75 exit(0);
76 cthread_exit(0);
77}
78
79void exit_thread _P0()
80{
81 do_exit_thread(0);
82}
83
84void _exit_thread _P0()
85{
86 do_exit_thread(1);
87}
88
89#ifndef NO_EXIT_PROG
90static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
91{
92 dprintf(("exit_prog(%d) called\n", status));
93 if (!initialized)
94 if (no_cleanup)
95 _exit(status);
96 else
97 exit(status);
Guido van Rossum3ecebf11996-07-30 16:48:31 +000098 if (no_cleanup)
99 _exit(status);
100 else
101 exit(status);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000102}
103
104void exit_prog _P1(status, int status)
105{
106 do_exit_prog(status, 0);
107}
108
109void _exit_prog _P1(status, int status)
110{
111 do_exit_prog(status, 1);
112}
113#endif /* NO_EXIT_PROG */
114
115/*
116 * Lock support.
117 */
118type_lock allocate_lock _P0()
119{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000120 mutex_t lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000121
122 dprintf(("allocate_lock called\n"));
123 if (!initialized)
124 init_thread();
125
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000126 lock = mutex_alloc();
127 if (mutex_init(lock)) {
128 perror("mutex_init");
129 free((void *) lock);
130 lock = 0;
131 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000132 dprintf(("allocate_lock() -> %lx\n", (long)lock));
133 return (type_lock) lock;
134}
135
136void free_lock _P1(lock, type_lock lock)
137{
138 dprintf(("free_lock(%lx) called\n", (long)lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000139 mutex_free(lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000140}
141
142int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
143{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000144 int success = FALSE;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000145
146 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000147 if (waitflag) { /* blocking */
148 mutex_lock(lock);
149 success = TRUE;
150 } else { /* non blocking */
151 success = mutex_try_lock(lock);
152 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000153 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
154 return success;
155}
156
157void release_lock _P1(lock, type_lock lock)
158{
159 dprintf(("release_lock(%lx) called\n", (long)lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000160 mutex_unlock((mutex_t )lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000161}
162
163/*
164 * Semaphore support.
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000165 *
166 * This implementation is ripped directly from the pthreads implementation.
167 * Which is to say that it is 100% non-functional at this time.
168 *
169 * Assuming the page is still up, documentation can be found at:
170 *
171 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
172 *
173 * Looking at the man page, it seems that one could easily implement a
174 * semaphore using a condition.
175 *
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000176 */
177type_sema allocate_sema _P1(value, int value)
178{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000179 char *sema = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000180 dprintf(("allocate_sema called\n"));
181 if (!initialized)
182 init_thread();
183
184 dprintf(("allocate_sema() -> %lx\n", (long) sema));
185 return (type_sema) sema;
186}
187
188void free_sema _P1(sema, type_sema sema)
189{
190 dprintf(("free_sema(%lx) called\n", (long) sema));
191}
192
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000193int down_sema _P2(sema, type_sema sema, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000194{
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000195 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000196 dprintf(("down_sema(%lx) return\n", (long) sema));
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000197 return -1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000198}
199
200void up_sema _P1(sema, type_sema sema)
201{
202 dprintf(("up_sema(%lx)\n", (long) sema));
203}