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