Changeset 391 for python/trunk/Python/thread_pth.h
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Python/thread_pth.h
r2 r391 4 4 2000-05-03 Andy Dustman <andy@dustman.net> 5 5 6 Adapted from Posix threads interface 6 Adapted from Posix threads interface 7 7 12 May 1997 -- david arnold <davida@pobox.com> 8 8 */ … … 23 23 24 24 typedef struct { 25 26 27 28 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; 29 29 } pth_lock; 30 30 … … 39 39 static void PyThread__init_thread(void) 40 40 { 41 42 43 44 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); 45 45 } 46 46 … … 52 52 long PyThread_start_new_thread(void (*func)(void *), void *arg) 53 53 { 54 55 56 57 54 pth_t th; 55 dprintf(("PyThread_start_new_thread called\n")); 56 if (!initialized) 57 PyThread_init_thread(); 58 58 59 60 61 62 59 th = pth_spawn(PyThread_attr, 60 (void* (*)(void *))func, 61 (void *)arg 62 ); 63 63 64 64 return th; 65 65 } 66 66 67 67 long PyThread_get_thread_ident(void) 68 68 { 69 volatile pth_t threadid; 70 if (!initialized) 71 PyThread_init_thread(); 72 /* Jump through some hoops for Alpha OSF/1 */ 73 threadid = pth_self(); 74 return (long) *(long *) &threadid; 75 } 76 77 static void do_PyThread_exit_thread(int no_cleanup) 78 { 79 dprintf(("PyThread_exit_thread called\n")); 80 if (!initialized) { 81 if (no_cleanup) 82 _exit(0); 83 else 84 exit(0); 85 } 69 volatile pth_t threadid; 70 if (!initialized) 71 PyThread_init_thread(); 72 /* Jump through some hoops for Alpha OSF/1 */ 73 threadid = pth_self(); 74 return (long) *(long *) &threadid; 86 75 } 87 76 88 77 void PyThread_exit_thread(void) 89 78 { 90 do_PyThread_exit_thread(0); 79 dprintf(("PyThread_exit_thread called\n")); 80 if (!initialized) { 81 exit(0); 82 } 91 83 } 92 93 void PyThread__exit_thread(void)94 {95 do_PyThread_exit_thread(1);96 }97 98 #ifndef NO_EXIT_PROG99 static void do_PyThread_exit_prog(int status, int no_cleanup)100 {101 dprintf(("PyThread_exit_prog(%d) called\n", status));102 if (!initialized)103 if (no_cleanup)104 _exit(status);105 else106 exit(status);107 }108 109 void PyThread_exit_prog(int status)110 {111 do_PyThread_exit_prog(status, 0);112 }113 114 void PyThread__exit_prog(int status)115 {116 do_PyThread_exit_prog(status, 1);117 }118 #endif /* NO_EXIT_PROG */119 84 120 85 /* … … 123 88 PyThread_type_lock PyThread_allocate_lock(void) 124 89 { 125 126 90 pth_lock *lock; 91 int status, error = 0; 127 92 128 129 130 93 dprintf(("PyThread_allocate_lock called\n")); 94 if (!initialized) 95 PyThread_init_thread(); 131 96 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 97 lock = (pth_lock *) malloc(sizeof(pth_lock)); 98 memset((void *)lock, '\0', sizeof(pth_lock)); 99 if (lock) { 100 lock->locked = 0; 101 status = pth_mutex_init(&lock->mut); 102 CHECK_STATUS("pth_mutex_init"); 103 status = pth_cond_init(&lock->lock_released); 104 CHECK_STATUS("pth_cond_init"); 105 if (error) { 106 free((void *)lock); 107 lock = NULL; 108 } 109 } 110 dprintf(("PyThread_allocate_lock() -> %p\n", lock)); 111 return (PyThread_type_lock) lock; 147 112 } 148 113 149 114 void PyThread_free_lock(PyThread_type_lock lock) 150 115 { 151 116 pth_lock *thelock = (pth_lock *)lock; 152 117 153 118 dprintf(("PyThread_free_lock(%p) called\n", lock)); 154 119 155 120 free((void *)thelock); 156 121 } 157 122 158 123 int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) 159 124 { 160 161 162 125 int success; 126 pth_lock *thelock = (pth_lock *)lock; 127 int status, error = 0; 163 128 164 129 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); 165 130 166 status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL); 167 CHECK_STATUS("pth_mutex_acquire[1]"); 168 success = thelock->locked == 0; 169 if (success) thelock->locked = 1; 131 status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL); 132 CHECK_STATUS("pth_mutex_acquire[1]"); 133 success = thelock->locked == 0; 134 if (success) thelock->locked = 1; 135 status = pth_mutex_release( &thelock->mut ); 136 CHECK_STATUS("pth_mutex_release[1]"); 137 138 if ( !success && waitflag ) { 139 /* continue trying until we get the lock */ 140 141 /* mut must be locked by me -- part of the condition 142 * protocol */ 143 status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL ); 144 CHECK_STATUS("pth_mutex_acquire[2]"); 145 while ( thelock->locked ) { 146 status = pth_cond_await(&thelock->lock_released, 147 &thelock->mut, NULL); 148 CHECK_STATUS("pth_cond_await"); 149 } 150 thelock->locked = 1; 170 151 status = pth_mutex_release( &thelock->mut ); 171 CHECK_STATUS("pth_mutex_release[1]"); 172 173 if ( !success && waitflag ) { 174 /* continue trying until we get the lock */ 175 176 /* mut must be locked by me -- part of the condition 177 * protocol */ 178 status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL ); 179 CHECK_STATUS("pth_mutex_acquire[2]"); 180 while ( thelock->locked ) { 181 status = pth_cond_await(&thelock->lock_released, 182 &thelock->mut, NULL); 183 CHECK_STATUS("pth_cond_await"); 184 } 185 thelock->locked = 1; 186 status = pth_mutex_release( &thelock->mut ); 187 CHECK_STATUS("pth_mutex_release[2]"); 188 success = 1; 189 } 190 if (error) success = 0; 191 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); 192 return success; 152 CHECK_STATUS("pth_mutex_release[2]"); 153 success = 1; 154 } 155 if (error) success = 0; 156 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); 157 return success; 193 158 } 194 159 195 160 void PyThread_release_lock(PyThread_type_lock lock) 196 161 { 197 198 162 pth_lock *thelock = (pth_lock *)lock; 163 int status, error = 0; 199 164 200 165 dprintf(("PyThread_release_lock(%p) called\n", lock)); 201 166 202 203 167 status = pth_mutex_acquire( &thelock->mut, 0, NULL ); 168 CHECK_STATUS("pth_mutex_acquire[3]"); 204 169 205 170 thelock->locked = 0; 206 171 207 208 172 status = pth_mutex_release( &thelock->mut ); 173 CHECK_STATUS("pth_mutex_release[3]"); 209 174 210 211 212 175 /* wake up someone (anyone, if any) waiting on the lock */ 176 status = pth_cond_notify( &thelock->lock_released, 0 ); 177 CHECK_STATUS("pth_cond_notify"); 213 178 }
Note:
See TracChangeset
for help on using the changeset viewer.