/* * Schedule the next rwmain phase. This will run on the scheduler in the * next interation. * * @param rwmain - rwmain instance * @param next - next phase function * @param frequency - times per second to execute timer, 0 to run only once * @param ctx - if defined, context passed to the next phase, otherwise the * rwmain instance is passed. */ static void schedule_next( struct rwmain_gi * rwmain, rwsched_CFRunLoopTimerCallBack next, uint16_t frequency, void * ctx) { rwsched_CFRunLoopTimerRef cftimer; rwsched_CFRunLoopTimerContext cf_context; bzero(&cf_context, sizeof(rwsched_CFRunLoopTimerContext)); cf_context.info = ctx ? ctx : rwmain; cftimer = rwsched_tasklet_CFRunLoopTimerCreate( rwmain->rwvx->rwsched_tasklet, kCFAllocatorDefault, CFAbsoluteTimeGetCurrent(), frequency ? 1.0 / (double)frequency : 0, 0, 0, next, &cf_context); rwsched_tasklet_CFRunLoopAddTimer( rwmain->rwvx->rwsched_tasklet, rwsched_tasklet_CFRunLoopGetCurrent(rwmain->rwvx->rwsched_tasklet), cftimer, rwmain->rwvx->rwsched->main_cfrunloop_mode); }
uint64_t rwmsg_toyRtimer_add(rwmsg_toytask_t *toy, int ms, toytimer_cb_t cb, void *ud) { rwsched_instance_ptr_t instance; rwmsg_toytask_t *toytask = toy; struct rwmsg_toytimer_s *toytimer; //uint64_t nsec; // Validate input parameters RW_CF_TYPE_VALIDATE(toytask, rwtoytask_tasklet_ptr_t); // Get the rwsched instance from the toytask instance = toytask->toysched->rwsched_instance; RW_CF_TYPE_VALIDATE(instance, rwsched_instance_ptr_t); // Allocate a toytimer container type and track it toytimer = RW_CF_TYPE_MALLOC0(sizeof(*toytimer), rwmsg_toytimer_ptr_t); RW_CF_TYPE_VALIDATE(toytimer, rwmsg_toytimer_ptr_t); // Create a CF runloop timer CFRunLoopTimerContext cf_context = { 0, NULL, NULL, NULL, NULL }; double timer_interval = ms * .001; rwsched_CFRunLoopRef runloop = rwsched_tasklet_CFRunLoopGetCurrent(toytask->rwsched_tasklet_info); rwsched_CFRunLoopTimerRef cftimer; // Create a CFRunLoopTimer as a runloop source for the toytimer cf_context.info = toytimer; cftimer = rwsched_tasklet_CFRunLoopTimerCreate(toytask->rwsched_tasklet_info, kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + timer_interval, timer_interval, 0, 0, rwmsg_toysched_Rtimer_callback, &cf_context); RW_CF_TYPE_VALIDATE(cftimer, rwsched_CFRunLoopTimerRef); rwsched_tasklet_CFRunLoopAddTimer(toytask->rwsched_tasklet_info, runloop, cftimer, instance->main_cfrunloop_mode); // Fill in the toytimer structure toytimer->id = cftimer->index; toytimer->cb = cb; toytimer->ud = ud; // Fill in the toytimer context structure toytimer->context.toytask = toy; toytimer->context.source.cftimer = cftimer; toytimer->context.u.toytimer.toytimer = toytimer; // Return the callback id return (uint64_t) toytimer->id; }
rwsched_CFRunLoopTimerRef rwsched_tasklet_CFRunLoopTimer(rwsched_tasklet_t *sched_tasklet, CFAbsoluteTime fireDate, CFTimeInterval interval, rwsched_CFRunLoopTimerCallBack callback, gpointer user_data) { rwsched_CFRunLoopTimerContext cf_context = { 0, NULL, NULL, NULL, NULL }; cf_context.info = user_data; return rwsched_tasklet_CFRunLoopTimerCreate(sched_tasklet, kCFAllocatorDefault, fireDate, interval, 0, 0, callback, &cf_context); }