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