static switch_status_t timer_check(switch_timer_t *timer, switch_bool_t step) { timer_private_t *private_info = timer->private_info; switch_status_t status = SWITCH_STATUS_SUCCESS; if (globals.RUNNING != 1 || !private_info->ready) { return SWITCH_STATUS_SUCCESS; } check_roll(); timer->tick = TIMER_MATRIX[timer->interval].tick; if (timer->tick < private_info->reference) { timer->diff = private_info->reference - timer->tick; } else { timer->diff = 0; } if (timer->diff) { status = SWITCH_STATUS_FALSE; } else if (step) { timer_step(timer); } return status; }
/* gather a single roll input */ char gather_roll()/*#{{{*/ { char result = '\0'; do { printf(">> "); result = getchar(); input_flush() if(result == 'x') { result = toupper(result); } /* end if */ }while(check_roll(result)); /* end do-while */ return result; } /* end gater_roll #}}} */
static switch_status_t timer_next(switch_timer_t *timer) { timer_private_t *private_info = timer->private_info; #ifdef DISABLE_1MS_COND int cond_index = timer->interval; #else int cond_index = 1; #endif int delta = (int) (private_info->reference - TIMER_MATRIX[timer->interval].tick); /* sync up timer if it's not been called for a while otherwise it will return instantly several times until it catches up */ if (delta < -1) { private_info->reference = timer->tick = TIMER_MATRIX[timer->interval].tick; } timer_step(timer); if (!MATRIX) { do_sleep(1000 * timer->interval); goto end; } while (globals.RUNNING == 1 && private_info->ready && TIMER_MATRIX[timer->interval].tick < private_info->reference) { check_roll(); if (globals.timer_count >= runtime.tipping_point) { os_yield(); globals.use_cond_yield = 0; } else { if (globals.use_cond_yield == 1) { switch_mutex_lock(TIMER_MATRIX[cond_index].mutex); if (TIMER_MATRIX[timer->interval].tick < private_info->reference) { switch_thread_cond_wait(TIMER_MATRIX[cond_index].cond, TIMER_MATRIX[cond_index].mutex); } switch_mutex_unlock(TIMER_MATRIX[cond_index].mutex); } else { do_sleep(1000); } } } end: return globals.RUNNING == 1 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; }
static switch_status_t timer_step(switch_timer_t *timer) { timer_private_t *private_info = timer->private_info; uint64_t samples; if (globals.RUNNING != 1 || private_info->ready == 0) { return SWITCH_STATUS_FALSE; } check_roll(); samples = timer->samples * (private_info->reference - private_info->start); if (samples > UINT32_MAX) { private_info->start = private_info->reference; samples = timer->samples; } timer->samplecount = (uint32_t) samples; private_info->reference++; return SWITCH_STATUS_SUCCESS; }