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