Mercurial > cpython-lru_cache-weakref
comparison Python/thread_pthread.h @ 4994:9bc652e9f2b4 legacy-trunk
Working semaphore implementation by Sjoerd.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 17 Jan 1997 21:06:41 +0000 |
| parents | 65d2d8e67da3 |
| children | 2c81d7d68ed3 |
comparison
equal
deleted
inserted
replaced
| 4993:b82d847b3dcb | 4994:9bc652e9f2b4 |
|---|---|
| 261 } | 261 } |
| 262 | 262 |
| 263 /* | 263 /* |
| 264 * Semaphore support. | 264 * Semaphore support. |
| 265 */ | 265 */ |
| 266 /* NOTE: 100% non-functional at this time - tim */ | 266 |
| 267 struct semaphore { | |
| 268 pthread_mutex_t mutex; | |
| 269 pthread_cond_t cond; | |
| 270 int value; | |
| 271 }; | |
| 267 | 272 |
| 268 type_sema allocate_sema _P1(value, int value) | 273 type_sema allocate_sema _P1(value, int value) |
| 269 { | 274 { |
| 270 char *sema = 0; | 275 struct semaphore *sema; |
| 276 int status, error = 0; | |
| 277 | |
| 271 dprintf(("allocate_sema called\n")); | 278 dprintf(("allocate_sema called\n")); |
| 272 if (!initialized) | 279 if (!initialized) |
| 273 init_thread(); | 280 init_thread(); |
| 274 | 281 |
| 282 sema = (struct semaphore *) malloc(sizeof(struct semaphore)); | |
| 283 if (sema != NULL) { | |
| 284 sema->value = value; | |
| 285 status = pthread_mutex_init(&sema->mutex, | |
| 286 pthread_mutexattr_default); | |
| 287 CHECK_STATUS("pthread_mutex_init"); | |
| 288 status = pthread_cond_init(&sema->cond, | |
| 289 pthread_condattr_default); | |
| 290 CHECK_STATUS("pthread_cond_init"); | |
| 291 if (error) { | |
| 292 free((void *) sema); | |
| 293 sema = NULL; | |
| 294 } | |
| 295 } | |
| 275 dprintf(("allocate_sema() -> %lx\n", (long) sema)); | 296 dprintf(("allocate_sema() -> %lx\n", (long) sema)); |
| 276 return (type_sema) sema; | 297 return (type_sema) sema; |
| 277 } | 298 } |
| 278 | 299 |
| 279 void free_sema _P1(sema, type_sema sema) | 300 void free_sema _P1(sema, type_sema sema) |
| 280 { | 301 { |
| 302 int status, error = 0; | |
| 303 struct semaphore *thesema = (struct semaphore *) sema; | |
| 304 | |
| 281 dprintf(("free_sema(%lx) called\n", (long) sema)); | 305 dprintf(("free_sema(%lx) called\n", (long) sema)); |
| 306 status = pthread_cond_destroy(&thesema->cond); | |
| 307 CHECK_STATUS("pthread_cond_destroy"); | |
| 308 status = pthread_mutex_destroy(&thesema->mutex); | |
| 309 CHECK_STATUS("pthread_mutex_destroy"); | |
| 310 free((void *) thesema); | |
| 282 } | 311 } |
| 283 | 312 |
| 284 int down_sema _P2(sema, type_sema sema, waitflag, int waitflag) | 313 int down_sema _P2(sema, type_sema sema, waitflag, int waitflag) |
| 285 { | 314 { |
| 315 int status, error = 0, success; | |
| 316 struct semaphore *thesema = (struct semaphore *) sema; | |
| 317 | |
| 286 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag)); | 318 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag)); |
| 319 status = pthread_mutex_lock(&thesema->mutex); | |
| 320 CHECK_STATUS("pthread_mutex_lock"); | |
| 321 if (waitflag) { | |
| 322 while (!error && thesema->value <= 0) { | |
| 323 status = pthread_cond_wait(&thesema->cond, | |
| 324 &thesema->mutex); | |
| 325 CHECK_STATUS("pthread_cond_wait"); | |
| 326 } | |
| 327 } | |
| 328 if (error) | |
| 329 success = 0; | |
| 330 else if (thesema->value > 0) { | |
| 331 thesema->value--; | |
| 332 success = 1; | |
| 333 } | |
| 334 else | |
| 335 success = 0; | |
| 336 status = pthread_mutex_unlock(&thesema->mutex); | |
| 337 CHECK_STATUS("pthread_mutex_unlock"); | |
| 287 dprintf(("down_sema(%lx) return\n", (long) sema)); | 338 dprintf(("down_sema(%lx) return\n", (long) sema)); |
| 288 return -1; | 339 return success; |
| 289 } | 340 } |
| 290 | 341 |
| 291 void up_sema _P1(sema, type_sema sema) | 342 void up_sema _P1(sema, type_sema sema) |
| 292 { | 343 { |
| 344 int status, error = 0; | |
| 345 struct semaphore *thesema = (struct semaphore *) sema; | |
| 346 | |
| 293 dprintf(("up_sema(%lx)\n", (long) sema)); | 347 dprintf(("up_sema(%lx)\n", (long) sema)); |
| 294 } | 348 status = pthread_mutex_lock(&thesema->mutex); |
| 349 CHECK_STATUS("pthread_mutex_lock"); | |
| 350 thesema->value++; | |
| 351 status = pthread_cond_signal(&thesema->cond); | |
| 352 CHECK_STATUS("pthread_cond_signal"); | |
| 353 status = pthread_mutex_unlock(&thesema->mutex); | |
| 354 CHECK_STATUS("pthread_mutex_unlock"); | |
| 355 } |
