Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 1 | |
| 2 | /* GNU pth threads interface |
| 3 | http://www.gnu.org/software/pth |
| 4 | 2000-05-03 Andy Dustman <andy@dustman.net> |
| 5 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6 | Adapted from Posix threads interface |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 7 | 12 May 1997 -- david arnold <davida@pobox.com> |
| 8 | */ |
| 9 | |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <pth.h> |
| 13 | |
| 14 | /* A pth mutex isn't sufficient to model the Python lock type |
| 15 | * because pth mutexes can be acquired multiple times by the |
| 16 | * same thread. |
| 17 | * |
| 18 | * The pth_lock struct implements a Python lock as a "locked?" bit |
| 19 | * and a <condition, mutex> pair. In general, if the bit can be acquired |
| 20 | * instantly, it is, else the pair is used to block the thread until the |
| 21 | * bit is cleared. |
| 22 | */ |
| 23 | |
| 24 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | char locked; /* 0=unlocked, 1=locked */ |
| 26 | /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
| 27 | pth_cond_t lock_released; |
| 28 | pth_mutex_t mut; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 29 | } pth_lock; |
| 30 | |
| 31 | #define CHECK_STATUS(name) if (status == -1) { printf("%d ", status); perror(name); error = 1; } |
| 32 | |
Martin v. Löwis | 70849f8 | 2003-09-20 11:13:36 +0000 | [diff] [blame] | 33 | pth_attr_t PyThread_attr; |
| 34 | |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Initialization. |
| 37 | */ |
| 38 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 39 | static void PyThread__init_thread(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 40 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | pth_init(); |
| 42 | PyThread_attr = pth_attr_new(); |
| 43 | pth_attr_set(PyThread_attr, PTH_ATTR_STACK_SIZE, 1<<18); |
| 44 | pth_attr_set(PyThread_attr, PTH_ATTR_JOINABLE, FALSE); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Thread support. |
| 49 | */ |
| 50 | |
| 51 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 52 | long PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 53 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | pth_t th; |
| 55 | dprintf(("PyThread_start_new_thread called\n")); |
| 56 | if (!initialized) |
| 57 | PyThread_init_thread(); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 58 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | th = pth_spawn(PyThread_attr, |
| 60 | (void* (*)(void *))func, |
| 61 | (void *)arg |
| 62 | ); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 63 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | return th; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 67 | long PyThread_get_thread_ident(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 68 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | volatile pth_t threadid; |
| 70 | if (!initialized) |
| 71 | PyThread_init_thread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | threadid = pth_self(); |
Jesus Cea | 736e7fc | 2011-03-14 17:36:54 +0100 | [diff] [blame] | 73 | return (long) threadid; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Amaury Forgeot d'Arc | 72aee3d | 2010-02-24 00:10:48 +0000 | [diff] [blame] | 76 | void PyThread_exit_thread(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 77 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | dprintf(("PyThread_exit_thread called\n")); |
| 79 | if (!initialized) { |
| 80 | exit(0); |
| 81 | } |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 84 | /* |
| 85 | * Lock support. |
| 86 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 87 | PyThread_type_lock PyThread_allocate_lock(void) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 88 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | pth_lock *lock; |
| 90 | int status, error = 0; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | dprintf(("PyThread_allocate_lock called\n")); |
| 93 | if (!initialized) |
| 94 | PyThread_init_thread(); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | lock = (pth_lock *) malloc(sizeof(pth_lock)); |
| 97 | memset((void *)lock, '\0', sizeof(pth_lock)); |
| 98 | if (lock) { |
| 99 | lock->locked = 0; |
| 100 | status = pth_mutex_init(&lock->mut); |
| 101 | CHECK_STATUS("pth_mutex_init"); |
| 102 | status = pth_cond_init(&lock->lock_released); |
| 103 | CHECK_STATUS("pth_cond_init"); |
| 104 | if (error) { |
| 105 | free((void *)lock); |
| 106 | lock = NULL; |
| 107 | } |
| 108 | } |
| 109 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 110 | return (PyThread_type_lock) lock; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 113 | void PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | pth_lock *thelock = (pth_lock *)lock; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | free((void *)thelock); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 122 | int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | int success; |
| 125 | pth_lock *thelock = (pth_lock *)lock; |
| 126 | int status, error = 0; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL); |
| 131 | CHECK_STATUS("pth_mutex_acquire[1]"); |
| 132 | success = thelock->locked == 0; |
| 133 | if (success) thelock->locked = 1; |
| 134 | status = pth_mutex_release( &thelock->mut ); |
| 135 | CHECK_STATUS("pth_mutex_release[1]"); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | if ( !success && waitflag ) { |
| 138 | /* continue trying until we get the lock */ |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | /* mut must be locked by me -- part of the condition |
| 141 | * protocol */ |
| 142 | status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL ); |
| 143 | CHECK_STATUS("pth_mutex_acquire[2]"); |
| 144 | while ( thelock->locked ) { |
| 145 | status = pth_cond_await(&thelock->lock_released, |
| 146 | &thelock->mut, NULL); |
| 147 | CHECK_STATUS("pth_cond_await"); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 148 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | thelock->locked = 1; |
| 150 | status = pth_mutex_release( &thelock->mut ); |
| 151 | CHECK_STATUS("pth_mutex_release[2]"); |
| 152 | success = 1; |
| 153 | } |
| 154 | if (error) success = 0; |
| 155 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 156 | return success; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 159 | void PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | pth_lock *thelock = (pth_lock *)lock; |
| 162 | int status, error = 0; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | status = pth_mutex_acquire( &thelock->mut, 0, NULL ); |
| 167 | CHECK_STATUS("pth_mutex_acquire[3]"); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | thelock->locked = 0; |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | status = pth_mutex_release( &thelock->mut ); |
| 172 | CHECK_STATUS("pth_mutex_release[3]"); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | /* wake up someone (anyone, if any) waiting on the lock */ |
| 175 | status = pth_cond_notify( &thelock->lock_released, 0 ); |
| 176 | CHECK_STATUS("pth_cond_notify"); |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 177 | } |