smcp_status_t smcp_schedule_timer( smcp_t self, smcp_timer_t timer, smcp_cms_t cms ) { smcp_status_t ret = SMCP_STATUS_FAILURE; SMCP_EMBEDDED_SELF_HOOK; assert(self!=NULL); assert(timer!=NULL); // Make sure we aren't already part of the list. require(!timer->ll.next, bail); require(!timer->ll.prev, bail); require(self->timers != timer, bail); DEBUG_PRINTF("Timer:%p: Scheduling to fire in %dms ...",timer,cms); #if SMCP_DEBUG_TIMERS size_t previousTimerCount = ll_count(self->timers); #endif if (cms < 0) { cms = 0; } timer->fire_date = smcp_plat_cms_to_timestamp(cms); ll_sorted_insert( (void**)&self->timers, timer, &smcp_timer_compare_func, NULL ); ret = SMCP_STATUS_OK; DEBUG_PRINTF("Timer:%p(CTX=%p): Scheduled.",timer,timer->context); DEBUG_PRINTF("%p: Timers in play = %d",self,(int)ll_count(self->timers)); #if SMCP_DEBUG_TIMERS assert((ll_count(self->timers)) == previousTimerCount+1); #endif bail: return ret; }
int bp_timer_add(bp_ttype type, struct timeval t, bp_timer_proc start_p, void *tproc_data) { taskq_el *newtask; int res; struct timeval now; if (type == RELATIVE) { gettimeofday(&now, NULL); t = tv_timeadd(now, t); } /* Build up the task data structure */ newtask = (taskq_el *) malloc(sizeof(taskq_el)); if (newtask == NULL) { fprintf(stderr, "malloc failed in bp_timer_add.\n"); exit(1); } if (pthread_mutex_lock(&taskid_mutex) < 0) { fprintf(stderr, "mutex lock failed in bp_timer_add\n"); exit(1); } newtask->tid = taskid++; if (pthread_mutex_unlock(&taskid_mutex) < 0) { fprintf(stderr, "mutex unlock failed in bp_timer_add\n"); exit(1); } newtask->ttime = t; newtask->tproc = start_p; newtask->tproc_arg = tproc_data; if (pthread_mutex_lock(&taskq_mutex) < 0) { fprintf(stderr, "mutex q lock failed in bp_timer_add\n"); exit(1); } res = ll_sorted_insert(&taskq, (void *) newtask, bp_timevalptr_cmp); taskq_len++; if (pthread_mutex_unlock(&taskq_mutex) < 0) { fprintf(stderr, "mutex q unlock failed in bp_timer_add\n"); exit(1); } if (res != 1) { fprintf(stderr, "ll_sorted_insert failed in bp_timer_add\n"); exit(1); } return 0; }