blob: b28e602d930c35cb931d1558be8ccd4573f79b94 [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 <lwp/lwp.h>
27#include <lwp/stackdep.h>
28
29#define STACKSIZE 1000 /* stacksize for a thread */
30#define NSTACKS 2 /* # stacks to be put in cache initialy */
31
32struct lock {
33 int lock_locked;
34 cv_t lock_condvar;
35 mon_t lock_monitor;
36};
37
38
39/*
40 * Initialization.
41 */
42static void _init_thread _P0()
43{
44 lwp_setstkcache(STACKSIZE, NSTACKS);
45}
46
47/*
48 * Thread support.
49 */
50
51
52int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
53{
54 thread_t tid;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000055 int success = 0; /* init not needed when SOLARIS_THREADS and */
56 /* C_THREADS implemented properly */
57
58 dprintf(("start_new_thread called\n"));
59 if (!initialized)
60 init_thread();
61 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
62 return success < 0 ? 0 : 1;
63}
64
65static void do_exit_thread _P1(no_cleanup, int no_cleanup)
66{
67 dprintf(("exit_thread called\n"));
68 if (!initialized)
69 if (no_cleanup)
70 _exit(0);
71 else
72 exit(0);
73 lwp_destroy(SELF);
74}
75
76void exit_thread _P0()
77{
78 do_exit_thread(0);
79}
80
81void _exit_thread _P0()
82{
83 do_exit_thread(1);
84}
85
86#ifndef NO_EXIT_PROG
87static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
88{
89 dprintf(("exit_prog(%d) called\n", status));
90 if (!initialized)
91 if (no_cleanup)
92 _exit(status);
93 else
94 exit(status);
95 pod_exit(status);
96}
97
98void exit_prog _P1(status, int status)
99{
100 do_exit_prog(status, 0);
101}
102
103void _exit_prog _P1(status, int status)
104{
105 do_exit_prog(status, 1);
106}
107#endif /* NO_EXIT_PROG */
108
109/*
110 * Lock support.
111 */
112type_lock allocate_lock _P0()
113{
114 struct lock *lock;
115 extern char *malloc();
116
117 dprintf(("allocate_lock called\n"));
118 if (!initialized)
119 init_thread();
120
121 lock = (struct lock *) malloc(sizeof(struct lock));
122 lock->lock_locked = 0;
123 (void) mon_create(&lock->lock_monitor);
124 (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
125 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));
132 mon_destroy(((struct lock *) lock)->lock_monitor);
133 free((char *) lock);
134}
135
136int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
137{
138 int success;
139
140 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
141 success = 0;
142
143 (void) mon_enter(((struct lock *) lock)->lock_monitor);
144 if (waitflag)
145 while (((struct lock *) lock)->lock_locked)
146 cv_wait(((struct lock *) lock)->lock_condvar);
147 if (!((struct lock *) lock)->lock_locked) {
148 success = 1;
149 ((struct lock *) lock)->lock_locked = 1;
150 }
151 cv_broadcast(((struct lock *) lock)->lock_condvar);
152 mon_exit(((struct lock *) lock)->lock_monitor);
153 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
154 return success;
155}
156
157void release_lock _P1(lock, type_lock lock)
158{
159 dprintf(("release_lock(%lx) called\n", (long)lock));
160 (void) mon_enter(((struct lock *) lock)->lock_monitor);
161 ((struct lock *) lock)->lock_locked = 0;
162 cv_broadcast(((struct lock *) lock)->lock_condvar);
163 mon_exit(((struct lock *) lock)->lock_monitor);
164}
165
166/*
167 * Semaphore support.
168 */
169type_sema allocate_sema _P1(value, int value)
170{
171 type_sema sema = 0;
172 dprintf(("allocate_sema called\n"));
173 if (!initialized)
174 init_thread();
175
176 dprintf(("allocate_sema() -> %lx\n", (long) sema));
177 return (type_sema) sema;
178}
179
180void free_sema _P1(sema, type_sema sema)
181{
182 dprintf(("free_sema(%lx) called\n", (long) sema));
183}
184
185void down_sema _P1(sema, type_sema sema)
186{
187 dprintf(("down_sema(%lx) called\n", (long) sema));
188 dprintf(("down_sema(%lx) return\n", (long) sema));
189}
190
191void up_sema _P1(sema, type_sema sema)
192{
193 dprintf(("up_sema(%lx)\n", (long) sema));
194}