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 <stdio.h> |
| 34 | #include <unistd.h> |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 35 | #include <errno.h> |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 36 | #include </usr/include/thread.h> |
Guido van Rossum | 6eea326 | 1996-08-26 14:58:54 +0000 | [diff] [blame] | 37 | #undef _POSIX_THREADS |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | /* |
| 41 | * Initialization. |
| 42 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 43 | static void PyThread__init_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Thread support. |
| 49 | */ |
| 50 | struct func_arg { |
| 51 | void (*func) _P((void *)); |
| 52 | void *arg; |
| 53 | }; |
| 54 | |
| 55 | static void *new_func _P1(funcarg, void *funcarg) |
| 56 | { |
| 57 | void (*func) _P((void *)); |
| 58 | void *arg; |
| 59 | |
| 60 | func = ((struct func_arg *) funcarg)->func; |
| 61 | arg = ((struct func_arg *) funcarg)->arg; |
| 62 | free(funcarg); |
| 63 | (*func)(arg); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 68 | int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 69 | { |
| 70 | struct func_arg *funcarg; |
| 71 | int success = 0; /* init not needed when SOLARIS_THREADS and */ |
| 72 | /* C_THREADS implemented properly */ |
| 73 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 74 | dprintf(("PyThread_start_new_thread called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 75 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 76 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 77 | funcarg = (struct func_arg *) malloc(sizeof(struct func_arg)); |
| 78 | funcarg->func = func; |
| 79 | funcarg->arg = arg; |
Guido van Rossum | 495894e | 1999-04-13 14:32:12 +0000 | [diff] [blame] | 80 | if (thr_create(0, 0, new_func, funcarg, |
| 81 | THR_DETACHED | THR_NEW_LWP, 0)) { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 82 | perror("thr_create"); |
| 83 | free((void *) funcarg); |
| 84 | success = -1; |
| 85 | } |
| 86 | return success < 0 ? 0 : 1; |
| 87 | } |
| 88 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 89 | long PyThread_get_thread_ident _P0() |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 90 | { |
| 91 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 92 | PyThread_init_thread(); |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 93 | return thr_self(); |
| 94 | } |
| 95 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 96 | static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 97 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 98 | dprintf(("PyThread_exit_thread called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 99 | if (!initialized) |
| 100 | if (no_cleanup) |
| 101 | _exit(0); |
| 102 | else |
| 103 | exit(0); |
| 104 | thr_exit(0); |
| 105 | } |
| 106 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 107 | void PyThread_exit_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 108 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 109 | do_PyThread_exit_thread(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 112 | void PyThread__exit_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 114 | do_PyThread_exit_thread(1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | #ifndef NO_EXIT_PROG |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 118 | static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 120 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 121 | if (!initialized) |
| 122 | if (no_cleanup) |
| 123 | _exit(status); |
| 124 | else |
| 125 | exit(status); |
| 126 | if (no_cleanup) |
| 127 | _exit(status); |
| 128 | else |
| 129 | exit(status); |
| 130 | } |
| 131 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 132 | void PyThread_exit_prog _P1(status, int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 133 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 134 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 137 | void PyThread__exit_prog _P1(status, int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 138 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 139 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 140 | } |
| 141 | #endif /* NO_EXIT_PROG */ |
| 142 | |
| 143 | /* |
| 144 | * Lock support. |
| 145 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 146 | PyThread_type_lock PyThread_allocate_lock _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 147 | { |
| 148 | mutex_t *lock; |
| 149 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 150 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 151 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 152 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 153 | |
| 154 | lock = (mutex_t *) malloc(sizeof(mutex_t)); |
| 155 | if (mutex_init(lock, USYNC_THREAD, 0)) { |
| 156 | perror("mutex_init"); |
| 157 | free((void *) lock); |
| 158 | lock = 0; |
| 159 | } |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 160 | dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock)); |
| 161 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 164 | void PyThread_free_lock _P1(lock, PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 165 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 166 | dprintf(("PyThread_free_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 167 | mutex_destroy((mutex_t *) lock); |
| 168 | free((void *) lock); |
| 169 | } |
| 170 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 171 | int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 172 | { |
| 173 | int success; |
| 174 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 175 | dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 176 | if (waitflag) |
| 177 | success = mutex_lock((mutex_t *) lock); |
| 178 | else |
| 179 | success = mutex_trylock((mutex_t *) lock); |
| 180 | if (success < 0) |
| 181 | perror(waitflag ? "mutex_lock" : "mutex_trylock"); |
| 182 | else |
| 183 | success = !success; /* solaris does it the other way round */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 184 | dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 185 | return success; |
| 186 | } |
| 187 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 188 | void PyThread_release_lock _P1(lock, PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 189 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 190 | dprintf(("PyThread_release_lock(%lx) called\n", (long)lock)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 191 | if (mutex_unlock((mutex_t *) lock)) |
| 192 | perror("mutex_unlock"); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Semaphore support. |
| 197 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 198 | PyThread_type_sema PyThread_allocate_sema _P1(value, int value) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 199 | { |
| 200 | sema_t *sema; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 201 | dprintf(("PyThread_allocate_sema called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 202 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 203 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 204 | |
| 205 | sema = (sema_t *) malloc(sizeof(sema_t)); |
| 206 | if (sema_init(sema, value, USYNC_THREAD, 0)) { |
| 207 | perror("sema_init"); |
| 208 | free((void *) sema); |
| 209 | sema = 0; |
| 210 | } |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 211 | dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema)); |
| 212 | return (PyThread_type_sema) sema; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 215 | void PyThread_free_sema _P1(sema, PyThread_type_sema sema) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 216 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 217 | dprintf(("PyThread_free_sema(%lx) called\n", (long) sema)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 218 | if (sema_destroy((sema_t *) sema)) |
| 219 | perror("sema_destroy"); |
| 220 | free((void *) sema); |
| 221 | } |
| 222 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 223 | int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 224 | { |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 225 | int success; |
| 226 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 227 | dprintf(("PyThread_down_sema(%lx) called\n", (long) sema)); |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 228 | if (waitflag) |
| 229 | success = sema_wait((sema_t *) sema); |
| 230 | else |
| 231 | success = sema_trywait((sema_t *) sema); |
| 232 | if (success < 0) { |
| 233 | if (errno == EBUSY) |
| 234 | success = 0; |
| 235 | else |
| 236 | perror("sema_wait"); |
| 237 | } |
| 238 | else |
| 239 | success = !success; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 240 | dprintf(("PyThread_down_sema(%lx) return %d\n", (long) sema, success)); |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 241 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 244 | void PyThread_up_sema _P1(sema, PyThread_type_sema sema) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 246 | dprintf(("PyThread_up_sema(%lx)\n", (long) sema)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 247 | if (sema_post((sema_t *) sema)) |
| 248 | perror("sema_post"); |
| 249 | } |