blob: c1740ce71bbc6d94eabe29e21d6324999ad20113 [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
32#include <stdlib.h>
33#include <stdio.h>
34#include <unistd.h>
Guido van Rossumcf1474b1996-10-08 14:17:53 +000035#include <errno.h>
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000036#include </usr/include/thread.h>
Guido van Rossum6eea3261996-08-26 14:58:54 +000037#undef _POSIX_THREADS
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000038
39
40/*
41 * Initialization.
42 */
43static void _init_thread _P0()
44{
45}
46
47/*
48 * Thread support.
49 */
50struct func_arg {
51 void (*func) _P((void *));
52 void *arg;
53};
54
55static void *new_func _P1(funcarg, void *funcarg)
56{
57 void (*func) _P((void *));
58 void *arg;
59
60 func = ((struct func_arg *) funcarg)->func;
61 arg = ((struct func_arg *) funcarg)->arg;
62 free(funcarg);
63 (*func)(arg);
64 return 0;
65}
66
67
68int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
69{
70 struct func_arg *funcarg;
71 int success = 0; /* init not needed when SOLARIS_THREADS and */
72 /* C_THREADS implemented properly */
73
74 dprintf(("start_new_thread called\n"));
75 if (!initialized)
76 init_thread();
77 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
78 funcarg->func = func;
79 funcarg->arg = arg;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000080 if (thr_create(0, 0, new_func, funcarg, THR_DETACHED, 0)) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000081 perror("thr_create");
82 free((void *) funcarg);
83 success = -1;
84 }
85 return success < 0 ? 0 : 1;
86}
87
Guido van Rossume944da81994-05-23 12:43:41 +000088long get_thread_ident _P0()
89{
90 if (!initialized)
91 init_thread();
92 return thr_self();
93}
94
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000095static void do_exit_thread _P1(no_cleanup, int no_cleanup)
96{
97 dprintf(("exit_thread called\n"));
98 if (!initialized)
99 if (no_cleanup)
100 _exit(0);
101 else
102 exit(0);
103 thr_exit(0);
104}
105
106void exit_thread _P0()
107{
108 do_exit_thread(0);
109}
110
111void _exit_thread _P0()
112{
113 do_exit_thread(1);
114}
115
116#ifndef NO_EXIT_PROG
117static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
118{
119 dprintf(("exit_prog(%d) called\n", status));
120 if (!initialized)
121 if (no_cleanup)
122 _exit(status);
123 else
124 exit(status);
125 if (no_cleanup)
126 _exit(status);
127 else
128 exit(status);
129}
130
131void exit_prog _P1(status, int status)
132{
133 do_exit_prog(status, 0);
134}
135
136void _exit_prog _P1(status, int status)
137{
138 do_exit_prog(status, 1);
139}
140#endif /* NO_EXIT_PROG */
141
142/*
143 * Lock support.
144 */
145type_lock allocate_lock _P0()
146{
147 mutex_t *lock;
148
149 dprintf(("allocate_lock called\n"));
150 if (!initialized)
151 init_thread();
152
153 lock = (mutex_t *) malloc(sizeof(mutex_t));
154 if (mutex_init(lock, USYNC_THREAD, 0)) {
155 perror("mutex_init");
156 free((void *) lock);
157 lock = 0;
158 }
159 dprintf(("allocate_lock() -> %lx\n", (long)lock));
160 return (type_lock) lock;
161}
162
163void free_lock _P1(lock, type_lock lock)
164{
165 dprintf(("free_lock(%lx) called\n", (long)lock));
166 mutex_destroy((mutex_t *) lock);
167 free((void *) lock);
168}
169
170int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
171{
172 int success;
173
174 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
175 if (waitflag)
176 success = mutex_lock((mutex_t *) lock);
177 else
178 success = mutex_trylock((mutex_t *) lock);
179 if (success < 0)
180 perror(waitflag ? "mutex_lock" : "mutex_trylock");
181 else
182 success = !success; /* solaris does it the other way round */
183 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
184 return success;
185}
186
187void release_lock _P1(lock, type_lock lock)
188{
189 dprintf(("release_lock(%lx) called\n", (long)lock));
190 if (mutex_unlock((mutex_t *) lock))
191 perror("mutex_unlock");
192}
193
194/*
195 * Semaphore support.
196 */
197type_sema allocate_sema _P1(value, int value)
198{
199 sema_t *sema;
200 dprintf(("allocate_sema called\n"));
201 if (!initialized)
202 init_thread();
203
204 sema = (sema_t *) malloc(sizeof(sema_t));
205 if (sema_init(sema, value, USYNC_THREAD, 0)) {
206 perror("sema_init");
207 free((void *) sema);
208 sema = 0;
209 }
210 dprintf(("allocate_sema() -> %lx\n", (long) sema));
211 return (type_sema) sema;
212}
213
214void free_sema _P1(sema, type_sema sema)
215{
216 dprintf(("free_sema(%lx) called\n", (long) sema));
217 if (sema_destroy((sema_t *) sema))
218 perror("sema_destroy");
219 free((void *) sema);
220}
221
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000222int down_sema _P2(sema, type_sema sema, waitflag, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000223{
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000224 int success;
225
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000226 dprintf(("down_sema(%lx) called\n", (long) sema));
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000227 if (waitflag)
228 success = sema_wait((sema_t *) sema);
229 else
230 success = sema_trywait((sema_t *) sema);
231 if (success < 0) {
232 if (errno == EBUSY)
233 success = 0;
234 else
235 perror("sema_wait");
236 }
237 else
238 success = !success;
239 dprintf(("down_sema(%lx) return %d\n", (long) sema, success));
240 return success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000241}
242
243void up_sema _P1(sema, type_sema sema)
244{
245 dprintf(("up_sema(%lx)\n", (long) sema));
246 if (sema_post((sema_t *) sema))
247 perror("sema_post");
248}