blob: 08cf6dd05190e40ca443a0ab5b88f1a150917693 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001/***********************************************************
2Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
4
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>
28#include </usr/include/thread.h>
29
30
31/*
32 * Initialization.
33 */
34static void _init_thread _P0()
35{
36}
37
38/*
39 * Thread support.
40 */
41struct func_arg {
42 void (*func) _P((void *));
43 void *arg;
44};
45
46static void *new_func _P1(funcarg, void *funcarg)
47{
48 void (*func) _P((void *));
49 void *arg;
50
51 func = ((struct func_arg *) funcarg)->func;
52 arg = ((struct func_arg *) funcarg)->arg;
53 free(funcarg);
54 (*func)(arg);
55 return 0;
56}
57
58
59int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
60{
61 struct func_arg *funcarg;
62 int success = 0; /* init not needed when SOLARIS_THREADS and */
63 /* C_THREADS implemented properly */
64
65 dprintf(("start_new_thread called\n"));
66 if (!initialized)
67 init_thread();
68 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
69 funcarg->func = func;
70 funcarg->arg = arg;
71 if (thr_create(0, 0, new_func, funcarg, THR_NEW_LWP, 0)) {
72 perror("thr_create");
73 free((void *) funcarg);
74 success = -1;
75 }
76 return success < 0 ? 0 : 1;
77}
78
79static void do_exit_thread _P1(no_cleanup, int no_cleanup)
80{
81 dprintf(("exit_thread called\n"));
82 if (!initialized)
83 if (no_cleanup)
84 _exit(0);
85 else
86 exit(0);
87 thr_exit(0);
88}
89
90void exit_thread _P0()
91{
92 do_exit_thread(0);
93}
94
95void _exit_thread _P0()
96{
97 do_exit_thread(1);
98}
99
100#ifndef NO_EXIT_PROG
101static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
102{
103 dprintf(("exit_prog(%d) called\n", status));
104 if (!initialized)
105 if (no_cleanup)
106 _exit(status);
107 else
108 exit(status);
109 if (no_cleanup)
110 _exit(status);
111 else
112 exit(status);
113}
114
115void exit_prog _P1(status, int status)
116{
117 do_exit_prog(status, 0);
118}
119
120void _exit_prog _P1(status, int status)
121{
122 do_exit_prog(status, 1);
123}
124#endif /* NO_EXIT_PROG */
125
126/*
127 * Lock support.
128 */
129type_lock allocate_lock _P0()
130{
131 mutex_t *lock;
132
133 dprintf(("allocate_lock called\n"));
134 if (!initialized)
135 init_thread();
136
137 lock = (mutex_t *) malloc(sizeof(mutex_t));
138 if (mutex_init(lock, USYNC_THREAD, 0)) {
139 perror("mutex_init");
140 free((void *) lock);
141 lock = 0;
142 }
143 dprintf(("allocate_lock() -> %lx\n", (long)lock));
144 return (type_lock) lock;
145}
146
147void free_lock _P1(lock, type_lock lock)
148{
149 dprintf(("free_lock(%lx) called\n", (long)lock));
150 mutex_destroy((mutex_t *) lock);
151 free((void *) lock);
152}
153
154int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
155{
156 int success;
157
158 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
159 if (waitflag)
160 success = mutex_lock((mutex_t *) lock);
161 else
162 success = mutex_trylock((mutex_t *) lock);
163 if (success < 0)
164 perror(waitflag ? "mutex_lock" : "mutex_trylock");
165 else
166 success = !success; /* solaris does it the other way round */
167 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
168 return success;
169}
170
171void release_lock _P1(lock, type_lock lock)
172{
173 dprintf(("release_lock(%lx) called\n", (long)lock));
174 if (mutex_unlock((mutex_t *) lock))
175 perror("mutex_unlock");
176}
177
178/*
179 * Semaphore support.
180 */
181type_sema allocate_sema _P1(value, int value)
182{
183 sema_t *sema;
184 dprintf(("allocate_sema called\n"));
185 if (!initialized)
186 init_thread();
187
188 sema = (sema_t *) malloc(sizeof(sema_t));
189 if (sema_init(sema, value, USYNC_THREAD, 0)) {
190 perror("sema_init");
191 free((void *) sema);
192 sema = 0;
193 }
194 dprintf(("allocate_sema() -> %lx\n", (long) sema));
195 return (type_sema) sema;
196}
197
198void free_sema _P1(sema, type_sema sema)
199{
200 dprintf(("free_sema(%lx) called\n", (long) sema));
201 if (sema_destroy((sema_t *) sema))
202 perror("sema_destroy");
203 free((void *) sema);
204}
205
206void down_sema _P1(sema, type_sema sema)
207{
208 dprintf(("down_sema(%lx) called\n", (long) sema));
209 if (sema_wait((sema_t *) sema))
210 perror("sema_wait");
211 dprintf(("down_sema(%lx) return\n", (long) sema));
212}
213
214void up_sema _P1(sema, type_sema sema)
215{
216 dprintf(("up_sema(%lx)\n", (long) sema));
217 if (sema_post((sema_t *) sema))
218 perror("sema_post");
219}