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