Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 29 | |
| 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 | |
| 39 | struct lock { |
| 40 | int lock_locked; |
| 41 | cv_t lock_condvar; |
| 42 | mon_t lock_monitor; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * Initialization. |
| 48 | */ |
| 49 | static void _init_thread _P0() |
| 50 | { |
| 51 | lwp_setstkcache(STACKSIZE, NSTACKS); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Thread support. |
| 56 | */ |
| 57 | |
| 58 | |
| 59 | int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
| 60 | { |
| 61 | thread_t tid; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 62 | int success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 63 | 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 Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 70 | long 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 Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 80 | static 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 | |
| 91 | void exit_thread _P0() |
| 92 | { |
| 93 | do_exit_thread(0); |
| 94 | } |
| 95 | |
| 96 | void _exit_thread _P0() |
| 97 | { |
| 98 | do_exit_thread(1); |
| 99 | } |
| 100 | |
| 101 | #ifndef NO_EXIT_PROG |
| 102 | static 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 | |
| 113 | void exit_prog _P1(status, int status) |
| 114 | { |
| 115 | do_exit_prog(status, 0); |
| 116 | } |
| 117 | |
| 118 | void _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 | */ |
| 127 | type_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 | |
| 144 | void 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 | |
| 151 | int 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 | |
| 172 | void 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 | */ |
| 184 | type_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 | |
| 195 | void free_sema _P1(sema, type_sema sema) |
| 196 | { |
| 197 | dprintf(("free_sema(%lx) called\n", (long) sema)); |
| 198 | } |
| 199 | |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 200 | int down_sema _P2(sema, type_sema sema, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 201 | { |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 202 | dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 203 | dprintf(("down_sema(%lx) return\n", (long) sema)); |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 204 | return -1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void up_sema _P1(sema, type_sema sema) |
| 208 | { |
| 209 | dprintf(("up_sema(%lx)\n", (long) sema)); |
| 210 | } |