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