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 | |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 32 | #include <mach/cthreads.h> |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | /* |
| 36 | * Initialization. |
| 37 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 38 | static void PyThread__init_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 39 | { |
| 40 | cthread_init(); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Thread support. |
| 45 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 46 | 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] | 47 | { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 48 | int success = 0; /* init not needed when SOLARIS_THREADS and */ |
| 49 | /* C_THREADS implemented properly */ |
| 50 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 51 | dprintf(("PyThread_start_new_thread called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 52 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 53 | PyThread_init_thread(); |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 54 | /* looks like solaris detaches the thread to never rejoin |
| 55 | * so well do it here |
| 56 | */ |
| 57 | cthread_detach(cthread_fork((cthread_fn_t) func, arg)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 58 | return success < 0 ? 0 : 1; |
| 59 | } |
| 60 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 61 | long PyThread_get_thread_ident _P0() |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 62 | { |
| 63 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 64 | PyThread_init_thread(); |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 65 | return (long) cthread_self(); |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 68 | 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] | 69 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 70 | dprintf(("PyThread_exit_thread called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 71 | if (!initialized) |
| 72 | if (no_cleanup) |
| 73 | _exit(0); |
| 74 | else |
| 75 | exit(0); |
| 76 | cthread_exit(0); |
| 77 | } |
| 78 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 79 | void PyThread_exit_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 80 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 81 | do_PyThread_exit_thread(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 84 | void PyThread__exit_thread _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 85 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 86 | do_PyThread_exit_thread(1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | #ifndef NO_EXIT_PROG |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 90 | 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] | 91 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 92 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 93 | if (!initialized) |
| 94 | if (no_cleanup) |
| 95 | _exit(status); |
| 96 | else |
| 97 | exit(status); |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 98 | if (no_cleanup) |
| 99 | _exit(status); |
| 100 | else |
| 101 | exit(status); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 104 | void PyThread_exit_prog _P1(status, int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 105 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 106 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 109 | void PyThread__exit_prog _P1(status, int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 110 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 111 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 112 | } |
| 113 | #endif /* NO_EXIT_PROG */ |
| 114 | |
| 115 | /* |
| 116 | * Lock support. |
| 117 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 118 | PyThread_type_lock PyThread_allocate_lock _P0() |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 120 | mutex_t lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 122 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 123 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 124 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 126 | lock = mutex_alloc(); |
| 127 | if (mutex_init(lock)) { |
| 128 | perror("mutex_init"); |
| 129 | free((void *) lock); |
| 130 | lock = 0; |
| 131 | } |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 132 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 133 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 136 | void PyThread_free_lock _P1(lock, PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 137 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 138 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 139 | mutex_free(lock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 142 | 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] | 143 | { |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 144 | int success = FALSE; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 145 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 146 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 147 | if (waitflag) { /* blocking */ |
| 148 | mutex_lock(lock); |
| 149 | success = TRUE; |
| 150 | } else { /* non blocking */ |
| 151 | success = mutex_try_lock(lock); |
| 152 | } |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 153 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 154 | return success; |
| 155 | } |
| 156 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 157 | void PyThread_release_lock _P1(lock, PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 158 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 159 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 160 | mutex_unlock((mutex_t )lock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Semaphore support. |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 165 | * |
| 166 | * This implementation is ripped directly from the pthreads implementation. |
| 167 | * Which is to say that it is 100% non-functional at this time. |
| 168 | * |
| 169 | * Assuming the page is still up, documentation can be found at: |
| 170 | * |
| 171 | * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html |
| 172 | * |
| 173 | * Looking at the man page, it seems that one could easily implement a |
| 174 | * semaphore using a condition. |
| 175 | * |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 176 | */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 177 | PyThread_type_sema PyThread_allocate_sema _P1(value, int value) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 178 | { |
Guido van Rossum | 3ecebf1 | 1996-07-30 16:48:31 +0000 | [diff] [blame] | 179 | char *sema = 0; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 180 | dprintf(("PyThread_allocate_sema called\n")); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 181 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 182 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 183 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 184 | dprintf(("PyThread_allocate_sema() -> %p\n", sema)); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 185 | return (PyThread_type_sema) sema; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 188 | void PyThread_free_sema _P1(sema, PyThread_type_sema sema) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 189 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 190 | dprintf(("PyThread_free_sema(%p) called\n", sema)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 193 | 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] | 194 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 195 | dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag)); |
| 196 | dprintf(("PyThread_down_sema(%p) return\n", sema)); |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 197 | return -1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 200 | void PyThread_up_sema _P1(sema, PyThread_type_sema sema) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 201 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame^] | 202 | dprintf(("PyThread_up_sema(%p)\n", sema)); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 203 | } |