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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | /* Roland McGrath said this should not be used since this is |
| 18 | done while linking to threads */ |
| 19 | cthread_init(); |
Martin v. Löwis | a6e9758 | 2002-01-01 18:41:33 +0000 | [diff] [blame] | 20 | #else |
| 21 | /* do nothing */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | ; |
Martin v. Löwis | a6e9758 | 2002-01-01 18:41:33 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | int success = 0; /* init not needed when SOLARIS_THREADS and */ |
| 33 | /* C_THREADS implemented properly */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | dprintf(("PyThread_start_new_thread called\n")); |
| 36 | if (!initialized) |
| 37 | PyThread_init_thread(); |
| 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)); |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | if (!initialized) |
| 49 | PyThread_init_thread(); |
| 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 | void |
| 54 | PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 55 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | dprintf(("PyThread_exit_thread called\n")); |
| 57 | if (!initialized) |
| 58 | exit(0); |
| 59 | cthread_exit(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 62 | /* |
| 63 | * Lock support. |
| 64 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 65 | PyThread_type_lock |
| 66 | PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 67 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | mutex_t lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | dprintf(("PyThread_allocate_lock called\n")); |
| 71 | if (!initialized) |
| 72 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | lock = mutex_alloc(); |
| 75 | if (mutex_init(lock)) { |
| 76 | perror("mutex_init"); |
| 77 | free((void *) lock); |
| 78 | lock = 0; |
| 79 | } |
| 80 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 81 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 84 | void |
| 85 | PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
| 88 | mutex_free(lock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 91 | int |
| 92 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 93 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | int success = FALSE; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
| 97 | if (waitflag) { /* blocking */ |
| 98 | mutex_lock((mutex_t)lock); |
| 99 | success = TRUE; |
| 100 | } else { /* non blocking */ |
| 101 | success = mutex_try_lock((mutex_t)lock); |
| 102 | } |
| 103 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 104 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 107 | void |
| 108 | PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
| 111 | mutex_unlock((mutex_t )lock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 112 | } |