Changes in uspace/lib/c/generic/fibril_synch.c [b72efe8:47c9a8c] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/fibril_synch.c
rb72efe8 r47c9a8c 112 112 awaiter_t wdata; 113 113 114 awaiter_initialize(&wdata); 114 115 wdata.fid = fibril_get_id(); 115 wdata.active = false;116 116 wdata.wu_event.inlist = true; 117 link_initialize(&wdata.wu_event.link);118 117 list_append(&wdata.wu_event.link, &fm->waiters); 119 118 check_for_deadlock(&fm->oi); … … 205 204 awaiter_t wdata; 206 205 206 awaiter_initialize(&wdata); 207 207 wdata.fid = (fid_t) f; 208 wdata.active = false;209 208 wdata.wu_event.inlist = true; 210 link_initialize(&wdata.wu_event.link);211 209 f->flags &= ~FIBRIL_WRITER; 212 210 list_append(&wdata.wu_event.link, &frw->waiters); … … 233 231 awaiter_t wdata; 234 232 233 awaiter_initialize(&wdata); 235 234 wdata.fid = (fid_t) f; 236 wdata.active = false;237 235 wdata.wu_event.inlist = true; 238 link_initialize(&wdata.wu_event.link);239 236 f->flags |= FIBRIL_WRITER; 240 237 list_append(&wdata.wu_event.link, &frw->waiters); … … 375 372 return ETIMEOUT; 376 373 374 awaiter_initialize(&wdata); 377 375 wdata.fid = fibril_get_id(); 378 wdata.active = false;379 380 376 wdata.to_event.inlist = timeout > 0; 381 wdata.to_event.occurred = false;382 link_initialize(&wdata.to_event.link);383 384 377 wdata.wu_event.inlist = true; 385 link_initialize(&wdata.wu_event.link);386 378 387 379 futex_down(&async_futex); … … 447 439 } 448 440 441 /** Timer fibril. 442 * 443 * @param arg Timer 444 */ 445 static int fibril_timer_func(void *arg) 446 { 447 fibril_timer_t *timer = (fibril_timer_t *) arg; 448 int rc; 449 450 fibril_mutex_lock(&timer->lock); 451 452 while (true) { 453 while (timer->state != fts_active && 454 timer->state != fts_cleanup) { 455 456 if (timer->state == fts_cleanup) 457 break; 458 459 fibril_condvar_wait(&timer->cv, &timer->lock); 460 } 461 462 if (timer->state == fts_cleanup) 463 break; 464 465 rc = fibril_condvar_wait_timeout(&timer->cv, &timer->lock, 466 timer->delay); 467 if (rc == ETIMEOUT) { 468 timer->state = fts_fired; 469 fibril_mutex_unlock(&timer->lock); 470 timer->fun(timer->arg); 471 fibril_mutex_lock(&timer->lock); 472 } 473 } 474 475 fibril_mutex_unlock(&timer->lock); 476 return 0; 477 } 478 479 /** Create new timer. 480 * 481 * @return New timer on success, @c NULL if out of memory. 482 */ 483 fibril_timer_t *fibril_timer_create(void) 484 { 485 fid_t fid; 486 fibril_timer_t *timer; 487 488 timer = calloc(1, sizeof(fibril_timer_t)); 489 if (timer == NULL) 490 return NULL; 491 492 fid = fibril_create(fibril_timer_func, (void *) timer); 493 if (fid == 0) { 494 free(timer); 495 return NULL; 496 } 497 498 fibril_mutex_initialize(&timer->lock); 499 fibril_condvar_initialize(&timer->cv); 500 501 timer->fibril = fid; 502 timer->state = fts_not_set; 503 504 fibril_add_ready(fid); 505 506 return timer; 507 } 508 509 /** Destroy timer. 510 * 511 * @param timer Timer, must not be active or accessed by other threads. 512 */ 513 void fibril_timer_destroy(fibril_timer_t *timer) 514 { 515 fibril_mutex_lock(&timer->lock); 516 assert(timer->state != fts_active); 517 timer->state = fts_cleanup; 518 fibril_condvar_broadcast(&timer->cv); 519 fibril_mutex_unlock(&timer->lock); 520 } 521 522 /** Set timer. 523 * 524 * Set timer to execute a callback function after the specified 525 * interval. 526 * 527 * @param timer Timer 528 * @param delay Delay in microseconds 529 * @param fun Callback function 530 * @param arg Argument for @a fun 531 */ 532 void fibril_timer_set(fibril_timer_t *timer, suseconds_t delay, 533 fibril_timer_fun_t fun, void *arg) 534 { 535 fibril_mutex_lock(&timer->lock); 536 timer->state = fts_active; 537 timer->delay = delay; 538 timer->fun = fun; 539 timer->arg = arg; 540 fibril_condvar_broadcast(&timer->cv); 541 fibril_mutex_unlock(&timer->lock); 542 } 543 544 /** Clear timer. 545 * 546 * Clears (cancels) timer and returns last state of the timer. 547 * This can be one of: 548 * - fts_not_set If the timer has not been set or has been cleared 549 * - fts_active Timer was set but did not fire 550 * - fts_fired Timer fired 551 * 552 * @param timer Timer 553 * @return Last timer state 554 */ 555 fibril_timer_state_t fibril_timer_clear(fibril_timer_t *timer) 556 { 557 fibril_timer_state_t old_state; 558 559 fibril_mutex_lock(&timer->lock); 560 old_state = timer->state; 561 timer->state = fts_not_set; 562 563 timer->delay = 0; 564 timer->fun = NULL; 565 timer->arg = NULL; 566 fibril_condvar_broadcast(&timer->cv); 567 fibril_mutex_unlock(&timer->lock); 568 569 return old_state; 570 } 571 449 572 /** @} 450 573 */
Note:
See TracChangeset
for help on using the changeset viewer.