Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 25 | #include "thread.h" |
| 26 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 27 | #ifdef DEBUG |
Sjoerd Mullender | d10d829 | 1992-09-11 15:19:27 +0000 | [diff] [blame] | 28 | static int thread_debug = 0; |
| 29 | #define dprintf(args) (thread_debug && printf args) |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 30 | #else |
| 31 | #define dprintf(args) |
| 32 | #endif |
| 33 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 34 | #ifdef __sgi |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <signal.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/prctl.h> |
| 40 | #include <ulocks.h> |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 41 | #include <errno.h> |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 42 | |
Sjoerd Mullender | 76ab5fe | 1993-01-13 12:49:46 +0000 | [diff] [blame] | 43 | #define HDR_SIZE 2680 /* sizeof(ushdr_t) */ |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 44 | #define MAXPROC 100 /* max # of threads that can be started */ |
| 45 | |
Sjoerd Mullender | cbaddb5 | 1993-10-12 14:10:58 +0000 | [diff] [blame^] | 46 | /*static*/ usptr_t *shared_arena; |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 47 | static ulock_t count_lock; /* protection for some variables */ |
| 48 | static ulock_t wait_lock; /* lock used to wait for other threads */ |
| 49 | static int waiting_for_threads; /* protected by count_lock */ |
| 50 | static int nthreads; /* protected by count_lock */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 51 | static int exit_status; |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 52 | static int do_exit; /* indicates that the program is to exit */ |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 53 | static int exiting; /* we're already exiting (for maybe_exit) */ |
| 54 | static pid_t my_pid; /* PID of main thread */ |
| 55 | static pid_t pidlist[MAXPROC]; /* PIDs of other threads */ |
| 56 | static int maxpidindex; /* # of PIDs in pidlist */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 57 | #endif |
| 58 | #ifdef sun |
| 59 | #include <lwp/lwp.h> |
| 60 | #include <lwp/stackdep.h> |
| 61 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 62 | #define STACKSIZE 1000 /* stacksize for a thread */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 63 | #define NSTACKS 2 /* # stacks to be put in cache initialy */ |
| 64 | |
| 65 | struct lock { |
| 66 | int lock_locked; |
| 67 | cv_t lock_condvar; |
| 68 | mon_t lock_monitor; |
| 69 | }; |
| 70 | #endif |
| 71 | #ifdef C_THREADS |
| 72 | #include <cthreads.h> |
| 73 | #endif |
| 74 | |
| 75 | #ifdef __STDC__ |
| 76 | #define _P(args) args |
| 77 | #define _P0() (void) |
| 78 | #define _P1(v,t) (t) |
| 79 | #define _P2(v1,t1,v2,t2) (t1,t2) |
| 80 | #else |
| 81 | #define _P(args) () |
| 82 | #define _P0() () |
| 83 | #define _P1(v,t) (v) t; |
| 84 | #define _P2(v1,t1,v2,t2) (v1,v2) t1; t2; |
| 85 | #endif |
| 86 | |
| 87 | static int initialized; |
| 88 | |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 89 | #ifdef __sgi |
| 90 | /* |
| 91 | * This routine is called as a signal handler when another thread |
| 92 | * exits. When that happens, we must see whether we have to exit as |
| 93 | * well (because of an exit_prog()) or whether we should continue on. |
| 94 | */ |
| 95 | static void exit_sig _P0() |
| 96 | { |
| 97 | dprintf(("exit_sig called\n")); |
| 98 | if (exiting && getpid() == my_pid) { |
| 99 | dprintf(("already exiting\n")); |
| 100 | return; |
| 101 | } |
| 102 | if (do_exit) { |
| 103 | dprintf(("exiting in exit_sig\n")); |
| 104 | exit_thread(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * This routine is called when a process calls exit(). If that wasn't |
| 110 | * done from the library, we do as if an exit_prog() was intended. |
| 111 | */ |
| 112 | static void maybe_exit _P0() |
| 113 | { |
| 114 | dprintf(("maybe_exit called\n")); |
| 115 | if (exiting) { |
| 116 | dprintf(("already exiting\n")); |
| 117 | return; |
| 118 | } |
| 119 | exit_prog(0); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | /* |
| 124 | * Initialization. |
| 125 | */ |
| 126 | void init_thread _P0() |
| 127 | { |
| 128 | #ifdef __sgi |
| 129 | struct sigaction s; |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 130 | #ifdef USE_DL |
| 131 | long addr, size; |
| 132 | #endif |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 133 | #endif |
| 134 | |
Sjoerd Mullender | d10d829 | 1992-09-11 15:19:27 +0000 | [diff] [blame] | 135 | #ifdef DEBUG |
| 136 | thread_debug = getenv("THREADDEBUG") != 0; |
| 137 | #endif |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 138 | if (initialized) |
| 139 | return; |
| 140 | initialized = 1; |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 141 | dprintf(("init_thread called\n")); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 142 | |
| 143 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 144 | #ifdef USE_DL |
| 145 | if ((size = usconfig(CONF_INITSIZE, 64*1024)) < 0) |
| 146 | perror("usconfig - CONF_INITSIZE (check)"); |
| 147 | if (usconfig(CONF_INITSIZE, size) < 0) |
| 148 | perror("usconfig - CONF_INITSIZE (reset)"); |
Sjoerd Mullender | 76ab5fe | 1993-01-13 12:49:46 +0000 | [diff] [blame] | 149 | addr = (long) dl_getrange(size + HDR_SIZE); |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 150 | dprintf(("trying to use addr %lx-%lx for shared arena\n", addr, addr+size)); |
| 151 | errno = 0; |
| 152 | if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0) |
| 153 | perror("usconfig - CONF_ATTACHADDR (set)"); |
| 154 | #endif |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 155 | if (usconfig(CONF_INITUSERS, 16) < 0) |
| 156 | perror("usconfig - CONF_INITUSERS"); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 157 | my_pid = getpid(); /* so that we know which is the main thread */ |
| 158 | atexit(maybe_exit); |
| 159 | s.sa_handler = exit_sig; |
| 160 | sigemptyset(&s.sa_mask); |
Sjoerd Mullender | d10d829 | 1992-09-11 15:19:27 +0000 | [diff] [blame] | 161 | /*sigaddset(&s.sa_mask, SIGUSR1);*/ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 162 | s.sa_flags = 0; |
| 163 | sigaction(SIGUSR1, &s, 0); |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 164 | if (prctl(PR_SETEXITSIG, SIGUSR1) < 0) |
| 165 | perror("prctl - PR_SETEXITSIG"); |
| 166 | if (usconfig(CONF_ARENATYPE, US_SHAREDONLY) < 0) |
| 167 | perror("usconfig - CONF_ARENATYPE"); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 168 | /*usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);*/ |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 169 | if ((shared_arena = usinit(tmpnam(0))) == 0) |
| 170 | perror("usinit"); |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 171 | #ifdef USE_DL |
| 172 | if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */ |
| 173 | perror("usconfig - CONF_ATTACHADDR (reset)"); |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 174 | #endif |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 175 | if ((count_lock = usnewlock(shared_arena)) == NULL) |
| 176 | perror("usnewlock (count_lock)"); |
| 177 | (void) usinitlock(count_lock); |
| 178 | if ((wait_lock = usnewlock(shared_arena)) == NULL) |
| 179 | perror("usnewlock (wait_lock)"); |
| 180 | dprintf(("arena start: %lx, arena size: %ld\n", (long) shared_arena, (long) usconfig(CONF_GETSIZE, shared_arena))); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 181 | #endif |
| 182 | #ifdef sun |
| 183 | lwp_setstkcache(STACKSIZE, NSTACKS); |
| 184 | #endif |
| 185 | #ifdef C_THREADS |
| 186 | cthread_init(); |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Thread support. |
| 192 | */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 193 | int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
| 194 | { |
| 195 | #ifdef sun |
| 196 | thread_t tid; |
| 197 | #endif |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 198 | #if defined(__sgi) && defined(USE_DL) |
| 199 | long addr, size; |
| 200 | static int local_initialized = 0; |
| 201 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 202 | int success = 0; /* init not needed when SOLARIS and */ |
| 203 | /* C_THREADS implemented properly */ |
| 204 | |
| 205 | dprintf(("start_new_thread called\n")); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 206 | if (!initialized) |
| 207 | init_thread(); |
| 208 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 209 | switch (ussetlock(count_lock)) { |
| 210 | case 0: return 0; |
| 211 | case -1: perror("ussetlock (count_lock)"); |
| 212 | } |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 213 | if (maxpidindex >= MAXPROC) |
| 214 | success = -1; |
| 215 | else { |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 216 | #ifdef USE_DL |
| 217 | if (!local_initialized) { |
| 218 | if ((size = usconfig(CONF_INITSIZE, 64*1024)) < 0) |
| 219 | perror("usconfig - CONF_INITSIZE (check)"); |
| 220 | if (usconfig(CONF_INITSIZE, size) < 0) |
| 221 | perror("usconfig - CONF_INITSIZE (reset)"); |
Sjoerd Mullender | 76ab5fe | 1993-01-13 12:49:46 +0000 | [diff] [blame] | 222 | addr = (long) dl_getrange(size + HDR_SIZE); |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 223 | dprintf(("trying to use addr %lx-%lx for sproc\n", addr, addr+size)); |
| 224 | errno = 0; |
| 225 | if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0) |
| 226 | perror("usconfig - CONF_ATTACHADDR (set)"); |
| 227 | } |
| 228 | #endif |
| 229 | if ((success = sproc(func, PR_SALL, arg)) < 0) |
| 230 | perror("sproc"); |
| 231 | #ifdef USE_DL |
| 232 | if (!local_initialized) { |
| 233 | if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */ |
| 234 | perror("usconfig - CONF_ATTACHADDR (reset)"); |
| 235 | local_initialized = 1; |
| 236 | } |
| 237 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 238 | if (success >= 0) { |
| 239 | nthreads++; |
| 240 | pidlist[maxpidindex++] = success; |
| 241 | } |
| 242 | } |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 243 | if (usunsetlock(count_lock) < 0) |
| 244 | perror("usunsetlock (count_lock)"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 245 | #endif |
| 246 | #ifdef SOLARIS |
| 247 | (void) thread_create(0, 0, func, arg, THREAD_NEW_LWP); |
| 248 | #endif |
| 249 | #ifdef sun |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 250 | success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 251 | #endif |
| 252 | #ifdef C_THREADS |
| 253 | (void) cthread_fork(func, arg); |
| 254 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 255 | return success < 0 ? 0 : 1; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 258 | static void do_exit_thread _P1(no_cleanup, int no_cleanup) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 259 | { |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 260 | dprintf(("exit_thread called\n")); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 261 | if (!initialized) |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 262 | if (no_cleanup) |
| 263 | _exit(0); |
| 264 | else |
| 265 | exit(0); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 266 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 267 | if (ussetlock(count_lock) < 0) |
| 268 | perror("ussetlock (count_lock)"); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 269 | nthreads--; |
| 270 | if (getpid() == my_pid) { |
| 271 | /* main thread; wait for other threads to exit */ |
| 272 | exiting = 1; |
| 273 | if (do_exit) { |
| 274 | int i; |
| 275 | |
| 276 | /* notify other threads */ |
Sjoerd Mullender | d10d829 | 1992-09-11 15:19:27 +0000 | [diff] [blame] | 277 | if (nthreads >= 0) { |
| 278 | dprintf(("kill other threads\n")); |
| 279 | for (i = 0; i < maxpidindex; i++) |
| 280 | (void) kill(pidlist[i], SIGKILL); |
| 281 | _exit(exit_status); |
| 282 | } |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 283 | } |
| 284 | waiting_for_threads = 1; |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 285 | if (ussetlock(wait_lock) < 0) |
| 286 | perror("ussetlock (wait_lock)"); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 287 | for (;;) { |
| 288 | if (nthreads < 0) { |
| 289 | dprintf(("really exit (%d)\n", exit_status)); |
| 290 | if (no_cleanup) |
| 291 | _exit(exit_status); |
| 292 | else |
| 293 | exit(exit_status); |
| 294 | } |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 295 | if (usunsetlock(count_lock) < 0) |
| 296 | perror("usunsetlock (count_lock)"); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 297 | dprintf(("waiting for other threads (%d)\n", nthreads)); |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 298 | if (ussetlock(wait_lock) < 0) |
| 299 | perror("ussetlock (wait_lock)"); |
| 300 | if (ussetlock(count_lock) < 0) |
| 301 | perror("ussetlock (count_lock)"); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | /* not the main thread */ |
| 305 | if (waiting_for_threads) { |
| 306 | dprintf(("main thread is waiting\n")); |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 307 | if (usunsetlock(wait_lock) < 0) |
| 308 | perror("usunsetlock (wait_lock)"); |
Sjoerd Mullender | d10d829 | 1992-09-11 15:19:27 +0000 | [diff] [blame] | 309 | } else if (do_exit) |
| 310 | (void) kill(my_pid, SIGUSR1); |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 311 | if (usunsetlock(count_lock) < 0) |
| 312 | perror("usunsetlock (count_lock)"); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 313 | _exit(0); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 314 | #endif |
| 315 | #ifdef SOLARIS |
| 316 | thread_exit(); |
| 317 | #endif |
| 318 | #ifdef sun |
| 319 | lwp_destroy(SELF); |
| 320 | #endif |
| 321 | #ifdef C_THREADS |
| 322 | cthread_exit(0); |
| 323 | #endif |
| 324 | } |
| 325 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 326 | void exit_thread _P0() |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 327 | { |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 328 | do_exit_thread(0); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 329 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 330 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 331 | void _exit_thread _P0() |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 332 | { |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 333 | do_exit_thread(1); |
| 334 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 336 | static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) |
| 337 | { |
| 338 | dprintf(("exit_prog(%d) called\n", status)); |
| 339 | if (!initialized) |
| 340 | if (no_cleanup) |
| 341 | _exit(status); |
| 342 | else |
| 343 | exit(status); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 344 | #ifdef __sgi |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 345 | do_exit = 1; |
| 346 | exit_status = status; |
| 347 | do_exit_thread(no_cleanup); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 348 | #endif |
| 349 | #ifdef sun |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 350 | pod_exit(status); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 351 | #endif |
| 352 | } |
| 353 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 354 | void exit_prog _P1(status, int status) |
| 355 | { |
| 356 | do_exit_prog(status, 0); |
| 357 | } |
| 358 | |
| 359 | void _exit_prog _P1(status, int status) |
| 360 | { |
| 361 | do_exit_prog(status, 1); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Lock support. |
| 366 | */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 367 | type_lock allocate_lock _P0() |
| 368 | { |
| 369 | #ifdef __sgi |
| 370 | ulock_t lock; |
| 371 | #endif |
| 372 | #ifdef sun |
| 373 | struct lock *lock; |
| 374 | extern char *malloc(); |
| 375 | #endif |
| 376 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 377 | dprintf(("allocate_lock called\n")); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 378 | if (!initialized) |
| 379 | init_thread(); |
| 380 | |
| 381 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 382 | if ((lock = usnewlock(shared_arena)) == NULL) |
| 383 | perror("usnewlock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 384 | (void) usinitlock(lock); |
| 385 | #endif |
| 386 | #ifdef sun |
| 387 | lock = (struct lock *) malloc(sizeof(struct lock)); |
| 388 | lock->lock_locked = 0; |
| 389 | (void) mon_create(&lock->lock_monitor); |
| 390 | (void) cv_create(&lock->lock_condvar, lock->lock_monitor); |
| 391 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 392 | dprintf(("allocate_lock() -> %lx\n", (long)lock)); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 393 | return (type_lock) lock; |
| 394 | } |
| 395 | |
| 396 | void free_lock _P1(lock, type_lock lock) |
| 397 | { |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 398 | dprintf(("free_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 399 | #ifdef __sgi |
| 400 | usfreelock((ulock_t) lock, shared_arena); |
| 401 | #endif |
| 402 | #ifdef sun |
| 403 | mon_destroy(((struct lock *) lock)->lock_monitor); |
| 404 | free((char *) lock); |
| 405 | #endif |
| 406 | } |
| 407 | |
| 408 | int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag) |
| 409 | { |
| 410 | int success; |
| 411 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 412 | dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag)); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 413 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 414 | errno = 0; /* clear it just in case */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 415 | if (waitflag) |
| 416 | success = ussetlock((ulock_t) lock); |
| 417 | else |
| 418 | success = uscsetlock((ulock_t) lock, 1); /* Try it once */ |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 419 | if (success < 0) |
| 420 | perror(waitflag ? "ussetlock" : "uscsetlock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 421 | #endif |
| 422 | #ifdef sun |
| 423 | success = 0; |
| 424 | |
| 425 | (void) mon_enter(((struct lock *) lock)->lock_monitor); |
| 426 | if (waitflag) |
| 427 | while (((struct lock *) lock)->lock_locked) |
| 428 | cv_wait(((struct lock *) lock)->lock_condvar); |
| 429 | if (!((struct lock *) lock)->lock_locked) { |
| 430 | success = 1; |
| 431 | ((struct lock *) lock)->lock_locked = 1; |
| 432 | } |
| 433 | cv_broadcast(((struct lock *) lock)->lock_condvar); |
| 434 | mon_exit(((struct lock *) lock)->lock_monitor); |
| 435 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 436 | dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success)); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 437 | return success; |
| 438 | } |
| 439 | |
| 440 | void release_lock _P1(lock, type_lock lock) |
| 441 | { |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 442 | dprintf(("release_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 443 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 444 | if (usunsetlock((ulock_t) lock) < 0) |
| 445 | perror("usunsetlock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 446 | #endif |
| 447 | #ifdef sun |
| 448 | (void) mon_enter(((struct lock *) lock)->lock_monitor); |
| 449 | ((struct lock *) lock)->lock_locked = 0; |
| 450 | cv_broadcast(((struct lock *) lock)->lock_condvar); |
| 451 | mon_exit(((struct lock *) lock)->lock_monitor); |
| 452 | #endif |
| 453 | } |
| 454 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 455 | /* |
| 456 | * Semaphore support. |
| 457 | */ |
| 458 | type_sema allocate_sema _P1(value, int value) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 459 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 460 | #ifdef __sgi |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 461 | usema_t *sema; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 462 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 463 | |
| 464 | dprintf(("allocate_sema called\n")); |
Sjoerd Mullender | d10d829 | 1992-09-11 15:19:27 +0000 | [diff] [blame] | 465 | if (!initialized) |
| 466 | init_thread(); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 467 | |
| 468 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 469 | if ((sema = usnewsema(shared_arena, value)) == NULL) |
| 470 | perror("usnewsema"); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 471 | dprintf(("allocate_sema() -> %lx\n", (long) sema)); |
| 472 | return (type_sema) sema; |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | void free_sema _P1(sema, type_sema sema) |
| 477 | { |
| 478 | dprintf(("free_sema(%lx) called\n", (long) sema)); |
| 479 | #ifdef __sgi |
| 480 | usfreesema((usema_t *) sema, shared_arena); |
| 481 | #endif |
| 482 | } |
| 483 | |
| 484 | void down_sema _P1(sema, type_sema sema) |
| 485 | { |
| 486 | dprintf(("down_sema(%lx) called\n", (long) sema)); |
| 487 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 488 | if (uspsema((usema_t *) sema) < 0) |
| 489 | perror("uspsema"); |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 490 | #endif |
| 491 | dprintf(("down_sema(%lx) return\n", (long) sema)); |
| 492 | } |
| 493 | |
| 494 | void up_sema _P1(sema, type_sema sema) |
| 495 | { |
| 496 | dprintf(("up_sema(%lx)\n", (long) sema)); |
| 497 | #ifdef __sgi |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 498 | if (usvsema((usema_t *) sema) < 0) |
| 499 | perror("usvsema"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 500 | #endif |
| 501 | } |