blob: 1e731ac5c093f992d2ae7cff69f1c50847b7b538 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3ecebf11996-07-30 16:48:31 +000025#include <mach/cthreads.h>
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000026
27
28/*
29 * Initialization.
30 */
31static void _init_thread _P0()
32{
33 cthread_init();
34}
35
36/*
37 * Thread support.
38 */
39int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
40{
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000041 int success = 0; /* init not needed when SOLARIS_THREADS and */
42 /* C_THREADS implemented properly */
43
44 dprintf(("start_new_thread called\n"));
45 if (!initialized)
46 init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000047 /* looks like solaris detaches the thread to never rejoin
48 * so well do it here
49 */
50 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000051 return success < 0 ? 0 : 1;
52}
53
Guido van Rossume944da81994-05-23 12:43:41 +000054long get_thread_ident _P0()
55{
56 if (!initialized)
57 init_thread();
Guido van Rossum3ecebf11996-07-30 16:48:31 +000058 return (long) cthread_self();
Guido van Rossume944da81994-05-23 12:43:41 +000059}
60
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000061static void do_exit_thread _P1(no_cleanup, int no_cleanup)
62{
63 dprintf(("exit_thread called\n"));
64 if (!initialized)
65 if (no_cleanup)
66 _exit(0);
67 else
68 exit(0);
69 cthread_exit(0);
70}
71
72void exit_thread _P0()
73{
74 do_exit_thread(0);
75}
76
77void _exit_thread _P0()
78{
79 do_exit_thread(1);
80}
81
82#ifndef NO_EXIT_PROG
83static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
84{
85 dprintf(("exit_prog(%d) called\n", status));
86 if (!initialized)
87 if (no_cleanup)
88 _exit(status);
89 else
90 exit(status);
Guido van Rossum3ecebf11996-07-30 16:48:31 +000091 if (no_cleanup)
92 _exit(status);
93 else
94 exit(status);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000095}
96
97void exit_prog _P1(status, int status)
98{
99 do_exit_prog(status, 0);
100}
101
102void _exit_prog _P1(status, int status)
103{
104 do_exit_prog(status, 1);
105}
106#endif /* NO_EXIT_PROG */
107
108/*
109 * Lock support.
110 */
111type_lock allocate_lock _P0()
112{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000113 mutex_t lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000114
115 dprintf(("allocate_lock called\n"));
116 if (!initialized)
117 init_thread();
118
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000119 lock = mutex_alloc();
120 if (mutex_init(lock)) {
121 perror("mutex_init");
122 free((void *) lock);
123 lock = 0;
124 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000125 dprintf(("allocate_lock() -> %lx\n", (long)lock));
126 return (type_lock) lock;
127}
128
129void free_lock _P1(lock, type_lock lock)
130{
131 dprintf(("free_lock(%lx) called\n", (long)lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000132 mutex_free(lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000133}
134
135int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
136{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000137 int success = FALSE;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000138
139 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000140 if (waitflag) { /* blocking */
141 mutex_lock(lock);
142 success = TRUE;
143 } else { /* non blocking */
144 success = mutex_try_lock(lock);
145 }
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
147 return success;
148}
149
150void release_lock _P1(lock, type_lock lock)
151{
152 dprintf(("release_lock(%lx) called\n", (long)lock));
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000153 mutex_unlock((mutex_t )lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154}
155
156/*
157 * Semaphore support.
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000158 *
159 * This implementation is ripped directly from the pthreads implementation.
160 * Which is to say that it is 100% non-functional at this time.
161 *
162 * Assuming the page is still up, documentation can be found at:
163 *
164 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
165 *
166 * Looking at the man page, it seems that one could easily implement a
167 * semaphore using a condition.
168 *
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000169 */
170type_sema allocate_sema _P1(value, int value)
171{
Guido van Rossum3ecebf11996-07-30 16:48:31 +0000172 char *sema = 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000173 dprintf(("allocate_sema called\n"));
174 if (!initialized)
175 init_thread();
176
177 dprintf(("allocate_sema() -> %lx\n", (long) sema));
178 return (type_sema) sema;
179}
180
181void free_sema _P1(sema, type_sema sema)
182{
183 dprintf(("free_sema(%lx) called\n", (long) sema));
184}
185
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000186int down_sema _P2(sema, type_sema sema, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000187{
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000188 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000189 dprintf(("down_sema(%lx) return\n", (long) sema));
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000190 return -1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000191}
192
193void up_sema _P1(sema, type_sema sema)
194{
195 dprintf(("up_sema(%lx)\n", (long) sema));
196}