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