Changeset c8c43cae in mainline for uspace/lib/c
- Timestamp:
- 2011-12-09T23:30:36Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 77b1c1a
- Parents:
- db8a034 (diff), a52de0e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace/lib/c
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/fibril_synch.c
rdb8a034 rc8c43cae 447 447 } 448 448 449 /** Timer fibril. 450 * 451 * @param arg Timer 452 */ 453 static int fibril_timer_func(void *arg) 454 { 455 fibril_timer_t *timer = (fibril_timer_t *) arg; 456 int rc; 457 458 fibril_mutex_lock(&timer->lock); 459 460 while (true) { 461 while (timer->state != fts_active && 462 timer->state != fts_cleanup) { 463 464 if (timer->state == fts_cleanup) 465 break; 466 467 fibril_condvar_wait(&timer->cv, &timer->lock); 468 } 469 470 if (timer->state == fts_cleanup) 471 break; 472 473 rc = fibril_condvar_wait_timeout(&timer->cv, &timer->lock, 474 timer->delay); 475 if (rc == ETIMEOUT) { 476 timer->state = fts_fired; 477 fibril_mutex_unlock(&timer->lock); 478 timer->fun(timer->arg); 479 fibril_mutex_lock(&timer->lock); 480 } 481 } 482 483 fibril_mutex_unlock(&timer->lock); 484 return 0; 485 } 486 487 /** Create new timer. 488 * 489 * @return New timer on success, @c NULL if out of memory. 490 */ 491 fibril_timer_t *fibril_timer_create(void) 492 { 493 fid_t fid; 494 fibril_timer_t *timer; 495 496 timer = calloc(1, sizeof(fibril_timer_t)); 497 if (timer == NULL) 498 return NULL; 499 500 fid = fibril_create(fibril_timer_func, (void *) timer); 501 if (fid == 0) { 502 free(timer); 503 return NULL; 504 } 505 506 fibril_mutex_initialize(&timer->lock); 507 fibril_condvar_initialize(&timer->cv); 508 509 timer->fibril = fid; 510 timer->state = fts_not_set; 511 512 fibril_add_ready(fid); 513 514 return timer; 515 } 516 517 /** Destroy timer. 518 * 519 * @param timer Timer, must not be active or accessed by other threads. 520 */ 521 void fibril_timer_destroy(fibril_timer_t *timer) 522 { 523 fibril_mutex_lock(&timer->lock); 524 assert(timer->state != fts_active); 525 timer->state = fts_cleanup; 526 fibril_condvar_broadcast(&timer->cv); 527 fibril_mutex_unlock(&timer->lock); 528 } 529 530 /** Set timer. 531 * 532 * Set timer to execute a callback function after the specified 533 * interval. 534 * 535 * @param timer Timer 536 * @param delay Delay in microseconds 537 * @param fun Callback function 538 * @param arg Argument for @a fun 539 */ 540 void fibril_timer_set(fibril_timer_t *timer, suseconds_t delay, 541 fibril_timer_fun_t fun, void *arg) 542 { 543 fibril_mutex_lock(&timer->lock); 544 timer->state = fts_active; 545 timer->delay = delay; 546 timer->fun = fun; 547 timer->arg = arg; 548 fibril_condvar_broadcast(&timer->cv); 549 fibril_mutex_unlock(&timer->lock); 550 } 551 552 /** Clear timer. 553 * 554 * Clears (cancels) timer and returns last state of the timer. 555 * This can be one of: 556 * - fts_not_set If the timer has not been set or has been cleared 557 * - fts_active Timer was set but did not fire 558 * - fts_fired Timer fired 559 * 560 * @param timer Timer 561 * @return Last timer state 562 */ 563 fibril_timer_state_t fibril_timer_clear(fibril_timer_t *timer) 564 { 565 fibril_timer_state_t old_state; 566 567 fibril_mutex_lock(&timer->lock); 568 old_state = timer->state; 569 timer->state = fts_not_set; 570 571 timer->delay = 0; 572 timer->fun = NULL; 573 timer->arg = NULL; 574 fibril_condvar_broadcast(&timer->cv); 575 fibril_mutex_unlock(&timer->lock); 576 577 return old_state; 578 } 579 449 580 /** @} 450 581 */ -
uspace/lib/c/include/bitops.h
rdb8a034 rc8c43cae 40 40 /** Mask with bit @a n set. */ 41 41 #define BIT_V(type, n) \ 42 ((type)1 << ( (n) - 1))42 ((type)1 << (n)) 43 43 44 44 /** Mask with rightmost @a n bits set. */ 45 45 #define BIT_RRANGE(type, n) \ 46 (BIT_V(type, (n) + 1) - 1)46 (BIT_V(type, (n)) - 1) 47 47 48 48 /** Mask with bits @a hi .. @a lo set. @a hi >= @a lo. */ -
uspace/lib/c/include/errno.h
rdb8a034 rc8c43cae 96 96 #define ENOTCONN (-10057) 97 97 98 #define ECONNREFUSED (-10058) 99 100 #define ECONNABORTED (-10059) 101 98 102 /** The requested operation was not performed. Try again later. */ 99 103 #define EAGAIN (-11002) -
uspace/lib/c/include/fibril_synch.h
rdb8a034 rc8c43cae 107 107 fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name) 108 108 109 typedef void (*fibril_timer_fun_t)(void *); 110 111 typedef enum { 112 /** Timer has not been set or has been cleared */ 113 fts_not_set, 114 /** Timer was set but did not fire yet */ 115 fts_active, 116 /** Timer has fired and has not been cleared since */ 117 fts_fired, 118 /** Timer is being destroyed */ 119 fts_cleanup 120 } fibril_timer_state_t; 121 122 /** Fibril timer. 123 * 124 * When a timer is set it executes a callback function (in a separate 125 * fibril) after a specified time interval. The timer can be cleared 126 * (canceled) before that. From the return value of fibril_timer_clear() 127 * one can tell whether the timer fired or not. 128 */ 129 typedef struct { 130 fibril_mutex_t lock; 131 fibril_condvar_t cv; 132 fid_t fibril; 133 fibril_timer_state_t state; 134 135 suseconds_t delay; 136 fibril_timer_fun_t fun; 137 void *arg; 138 } fibril_timer_t; 139 109 140 extern void fibril_mutex_initialize(fibril_mutex_t *); 110 141 extern void fibril_mutex_lock(fibril_mutex_t *); … … 129 160 extern void fibril_condvar_broadcast(fibril_condvar_t *); 130 161 162 extern fibril_timer_t *fibril_timer_create(void); 163 extern void fibril_timer_destroy(fibril_timer_t *); 164 extern void fibril_timer_set(fibril_timer_t *, suseconds_t, fibril_timer_fun_t, 165 void *); 166 extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *); 167 131 168 #endif 132 169
Note:
See TracChangeset
for help on using the changeset viewer.