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