comparison Python/thread_pthread.h @ 9679:877d23bd5bdc legacy-trunk

Thanks to Chris Herborth, the thread primitives now have proper Py* names in the source code (they already had those for the linker, through some smart macros; but the source still had the old, un-Py names).
author Guido van Rossum <guido@python.org>
date Mon, 21 Dec 1998 19:32:43 +0000
parents 2340f615da8f
children 94ad90fd2712
comparison
equal deleted inserted replaced
9678:f39a19a1229a 9679:877d23bd5bdc
123 #ifdef _HAVE_BSDI 123 #ifdef _HAVE_BSDI
124 static void _noop() 124 static void _noop()
125 { 125 {
126 } 126 }
127 127
128 static void _init_thread _P0() 128 static void PyThread__init_thread _P0()
129 { 129 {
130 /* DO AN INIT BY STARTING THE THREAD */ 130 /* DO AN INIT BY STARTING THE THREAD */
131 static int dummy = 0; 131 static int dummy = 0;
132 pthread_t thread1; 132 pthread_t thread1;
133 pthread_create(&thread1, NULL, (void *) _noop, &dummy); 133 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
134 pthread_join(thread1, NULL); 134 pthread_join(thread1, NULL);
135 } 135 }
136 136
137 #else /* !_HAVE_BSDI */ 137 #else /* !_HAVE_BSDI */
138 138
139 static void _init_thread _P0() 139 static void PyThread__init_thread _P0()
140 { 140 {
141 #if defined(_AIX) && defined(__GNUC__) 141 #if defined(_AIX) && defined(__GNUC__)
142 pthread_init(); 142 pthread_init();
143 #endif 143 #endif
144 } 144 }
148 /* 148 /*
149 * Thread support. 149 * Thread support.
150 */ 150 */
151 151
152 152
153 int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) 153 int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
154 { 154 {
155 pthread_t th; 155 pthread_t th;
156 int success; 156 int success;
157 dprintf(("start_new_thread called\n")); 157 dprintf(("PyThread_start_new_thread called\n"));
158 if (!initialized) 158 if (!initialized)
159 init_thread(); 159 PyThread_init_thread();
160 160
161 success = pthread_create(&th, 161 success = pthread_create(&th,
162 #if defined(PY_PTHREAD_D4) 162 #if defined(PY_PTHREAD_D4)
163 pthread_attr_default, 163 pthread_attr_default,
164 (pthread_startroutine_t)func, 164 (pthread_startroutine_t)func,
186 #endif 186 #endif
187 } 187 }
188 return success < 0 ? 0 : 1; 188 return success < 0 ? 0 : 1;
189 } 189 }
190 190
191 long get_thread_ident _P0() 191 long PyThread_get_thread_ident _P0()
192 { 192 {
193 volatile pthread_t threadid; 193 volatile pthread_t threadid;
194 if (!initialized) 194 if (!initialized)
195 init_thread(); 195 PyThread_init_thread();
196 /* Jump through some hoops for Alpha OSF/1 */ 196 /* Jump through some hoops for Alpha OSF/1 */
197 threadid = pthread_self(); 197 threadid = pthread_self();
198 return (long) *(long *) &threadid; 198 return (long) *(long *) &threadid;
199 } 199 }
200 200
201 static void do_exit_thread _P1(no_cleanup, int no_cleanup) 201 static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
202 { 202 {
203 dprintf(("exit_thread called\n")); 203 dprintf(("PyThread_exit_thread called\n"));
204 if (!initialized) { 204 if (!initialized) {
205 if (no_cleanup) 205 if (no_cleanup)
206 _exit(0); 206 _exit(0);
207 else 207 else
208 exit(0); 208 exit(0);
209 } 209 }
210 } 210 }
211 211
212 void exit_thread _P0() 212 void PyThread_exit_thread _P0()
213 { 213 {
214 do_exit_thread(0); 214 do_PyThread_exit_thread(0);
215 } 215 }
216 216
217 void _exit_thread _P0() 217 void PyThread__exit_thread _P0()
218 { 218 {
219 do_exit_thread(1); 219 do_PyThread_exit_thread(1);
220 } 220 }
221 221
222 #ifndef NO_EXIT_PROG 222 #ifndef NO_EXIT_PROG
223 static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) 223 static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
224 { 224 {
225 dprintf(("exit_prog(%d) called\n", status)); 225 dprintf(("PyThread_exit_prog(%d) called\n", status));
226 if (!initialized) 226 if (!initialized)
227 if (no_cleanup) 227 if (no_cleanup)
228 _exit(status); 228 _exit(status);
229 else 229 else
230 exit(status); 230 exit(status);
231 } 231 }
232 232
233 void exit_prog _P1(status, int status) 233 void PyThread_exit_prog _P1(status, int status)
234 { 234 {
235 do_exit_prog(status, 0); 235 do_PyThread_exit_prog(status, 0);
236 } 236 }
237 237
238 void _exit_prog _P1(status, int status) 238 void PyThread__exit_prog _P1(status, int status)
239 { 239 {
240 do_exit_prog(status, 1); 240 do_PyThread_exit_prog(status, 1);
241 } 241 }
242 #endif /* NO_EXIT_PROG */ 242 #endif /* NO_EXIT_PROG */
243 243
244 /* 244 /*
245 * Lock support. 245 * Lock support.
246 */ 246 */
247 type_lock allocate_lock _P0() 247 PyThread_type_lock PyThread_allocate_lock _P0()
248 { 248 {
249 pthread_lock *lock; 249 pthread_lock *lock;
250 int status, error = 0; 250 int status, error = 0;
251 251
252 dprintf(("allocate_lock called\n")); 252 dprintf(("PyThread_allocate_lock called\n"));
253 if (!initialized) 253 if (!initialized)
254 init_thread(); 254 PyThread_init_thread();
255 255
256 lock = (pthread_lock *) malloc(sizeof(pthread_lock)); 256 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
257 memset((void *)lock, '\0', sizeof(pthread_lock)); 257 memset((void *)lock, '\0', sizeof(pthread_lock));
258 if (lock) { 258 if (lock) {
259 lock->locked = 0; 259 lock->locked = 0;
270 free((void *)lock); 270 free((void *)lock);
271 lock = 0; 271 lock = 0;
272 } 272 }
273 } 273 }
274 274
275 dprintf(("allocate_lock() -> %lx\n", (long)lock)); 275 dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock));
276 return (type_lock) lock; 276 return (PyThread_type_lock) lock;
277 } 277 }
278 278
279 void free_lock _P1(lock, type_lock lock) 279 void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
280 { 280 {
281 pthread_lock *thelock = (pthread_lock *)lock; 281 pthread_lock *thelock = (pthread_lock *)lock;
282 int status, error = 0; 282 int status, error = 0;
283 283
284 dprintf(("free_lock(%lx) called\n", (long)lock)); 284 dprintf(("PyThread_free_lock(%lx) called\n", (long)lock));
285 285
286 status = pthread_mutex_destroy( &thelock->mut ); 286 status = pthread_mutex_destroy( &thelock->mut );
287 CHECK_STATUS("pthread_mutex_destroy"); 287 CHECK_STATUS("pthread_mutex_destroy");
288 288
289 status = pthread_cond_destroy( &thelock->lock_released ); 289 status = pthread_cond_destroy( &thelock->lock_released );
290 CHECK_STATUS("pthread_cond_destroy"); 290 CHECK_STATUS("pthread_cond_destroy");
291 291
292 free((void *)thelock); 292 free((void *)thelock);
293 } 293 }
294 294
295 int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag) 295 int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
296 { 296 {
297 int success; 297 int success;
298 pthread_lock *thelock = (pthread_lock *)lock; 298 pthread_lock *thelock = (pthread_lock *)lock;
299 int status, error = 0; 299 int status, error = 0;
300 300
301 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag)); 301 dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
302 302
303 status = pthread_mutex_lock( &thelock->mut ); 303 status = pthread_mutex_lock( &thelock->mut );
304 CHECK_STATUS("pthread_mutex_lock[1]"); 304 CHECK_STATUS("pthread_mutex_lock[1]");
305 success = thelock->locked == 0; 305 success = thelock->locked == 0;
306 if (success) thelock->locked = 1; 306 if (success) thelock->locked = 1;
323 status = pthread_mutex_unlock( &thelock->mut ); 323 status = pthread_mutex_unlock( &thelock->mut );
324 CHECK_STATUS("pthread_mutex_unlock[2]"); 324 CHECK_STATUS("pthread_mutex_unlock[2]");
325 success = 1; 325 success = 1;
326 } 326 }
327 if (error) success = 0; 327 if (error) success = 0;
328 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success)); 328 dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
329 return success; 329 return success;
330 } 330 }
331 331
332 void release_lock _P1(lock, type_lock lock) 332 void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
333 { 333 {
334 pthread_lock *thelock = (pthread_lock *)lock; 334 pthread_lock *thelock = (pthread_lock *)lock;
335 int status, error = 0; 335 int status, error = 0;
336 336
337 dprintf(("release_lock(%lx) called\n", (long)lock)); 337 dprintf(("PyThread_release_lock(%lx) called\n", (long)lock));
338 338
339 status = pthread_mutex_lock( &thelock->mut ); 339 status = pthread_mutex_lock( &thelock->mut );
340 CHECK_STATUS("pthread_mutex_lock[3]"); 340 CHECK_STATUS("pthread_mutex_lock[3]");
341 341
342 thelock->locked = 0; 342 thelock->locked = 0;
357 pthread_mutex_t mutex; 357 pthread_mutex_t mutex;
358 pthread_cond_t cond; 358 pthread_cond_t cond;
359 int value; 359 int value;
360 }; 360 };
361 361
362 type_sema allocate_sema _P1(value, int value) 362 PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
363 { 363 {
364 struct semaphore *sema; 364 struct semaphore *sema;
365 int status, error = 0; 365 int status, error = 0;
366 366
367 dprintf(("allocate_sema called\n")); 367 dprintf(("PyThread_allocate_sema called\n"));
368 if (!initialized) 368 if (!initialized)
369 init_thread(); 369 PyThread_init_thread();
370 370
371 sema = (struct semaphore *) malloc(sizeof(struct semaphore)); 371 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
372 if (sema != NULL) { 372 if (sema != NULL) {
373 sema->value = value; 373 sema->value = value;
374 status = pthread_mutex_init(&sema->mutex, 374 status = pthread_mutex_init(&sema->mutex,
380 if (error) { 380 if (error) {
381 free((void *) sema); 381 free((void *) sema);
382 sema = NULL; 382 sema = NULL;
383 } 383 }
384 } 384 }
385 dprintf(("allocate_sema() -> %lx\n", (long) sema)); 385 dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema));
386 return (type_sema) sema; 386 return (PyThread_type_sema) sema;
387 } 387 }
388 388
389 void free_sema _P1(sema, type_sema sema) 389 void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
390 { 390 {
391 int status, error = 0; 391 int status, error = 0;
392 struct semaphore *thesema = (struct semaphore *) sema; 392 struct semaphore *thesema = (struct semaphore *) sema;
393 393
394 dprintf(("free_sema(%lx) called\n", (long) sema)); 394 dprintf(("PyThread_free_sema(%lx) called\n", (long) sema));
395 status = pthread_cond_destroy(&thesema->cond); 395 status = pthread_cond_destroy(&thesema->cond);
396 CHECK_STATUS("pthread_cond_destroy"); 396 CHECK_STATUS("pthread_cond_destroy");
397 status = pthread_mutex_destroy(&thesema->mutex); 397 status = pthread_mutex_destroy(&thesema->mutex);
398 CHECK_STATUS("pthread_mutex_destroy"); 398 CHECK_STATUS("pthread_mutex_destroy");
399 free((void *) thesema); 399 free((void *) thesema);
400 } 400 }
401 401
402 int down_sema _P2(sema, type_sema sema, waitflag, int waitflag) 402 int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
403 { 403 {
404 int status, error = 0, success; 404 int status, error = 0, success;
405 struct semaphore *thesema = (struct semaphore *) sema; 405 struct semaphore *thesema = (struct semaphore *) sema;
406 406
407 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag)); 407 dprintf(("PyThread_down_sema(%lx, %d) called\n", (long) sema, waitflag));
408 status = pthread_mutex_lock(&thesema->mutex); 408 status = pthread_mutex_lock(&thesema->mutex);
409 CHECK_STATUS("pthread_mutex_lock"); 409 CHECK_STATUS("pthread_mutex_lock");
410 if (waitflag) { 410 if (waitflag) {
411 while (!error && thesema->value <= 0) { 411 while (!error && thesema->value <= 0) {
412 status = pthread_cond_wait(&thesema->cond, 412 status = pthread_cond_wait(&thesema->cond,
422 } 422 }
423 else 423 else
424 success = 0; 424 success = 0;
425 status = pthread_mutex_unlock(&thesema->mutex); 425 status = pthread_mutex_unlock(&thesema->mutex);
426 CHECK_STATUS("pthread_mutex_unlock"); 426 CHECK_STATUS("pthread_mutex_unlock");
427 dprintf(("down_sema(%lx) return\n", (long) sema)); 427 dprintf(("PyThread_down_sema(%lx) return\n", (long) sema));
428 return success; 428 return success;
429 } 429 }
430 430
431 void up_sema _P1(sema, type_sema sema) 431 void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
432 { 432 {
433 int status, error = 0; 433 int status, error = 0;
434 struct semaphore *thesema = (struct semaphore *) sema; 434 struct semaphore *thesema = (struct semaphore *) sema;
435 435
436 dprintf(("up_sema(%lx)\n", (long) sema)); 436 dprintf(("PyThread_up_sema(%lx)\n", (long) sema));
437 status = pthread_mutex_lock(&thesema->mutex); 437 status = pthread_mutex_lock(&thesema->mutex);
438 CHECK_STATUS("pthread_mutex_lock"); 438 CHECK_STATUS("pthread_mutex_lock");
439 thesema->value++; 439 thesema->value++;
440 status = pthread_cond_signal(&thesema->cond); 440 status = pthread_cond_signal(&thesema->cond);
441 CHECK_STATUS("pthread_cond_signal"); 441 CHECK_STATUS("pthread_cond_signal");