Lines Matching refs:alarm
131 static void alarm_set_internal(alarm_t* alarm, uint64_t period_ms,
134 static void alarm_cancel_internal(alarm_t* alarm);
135 static void remove_pending_alarm(alarm_t* alarm);
136 static void schedule_next_instance(alarm_t* alarm);
185 void alarm_free(alarm_t* alarm) { in alarm_free() argument
186 if (!alarm) return; in alarm_free()
188 alarm_cancel(alarm); in alarm_free()
190 osi_free((void*)alarm->stats.name); in alarm_free()
191 alarm->closure.~CancelableClosureInStruct(); in alarm_free()
192 alarm->callback_mutex.reset(); in alarm_free()
193 osi_free(alarm); in alarm_free()
196 uint64_t alarm_get_remaining_ms(const alarm_t* alarm) { in alarm_get_remaining_ms() argument
197 log::assert_that(alarm != NULL, "assert failed: alarm != NULL"); in alarm_get_remaining_ms()
202 if (alarm->deadline_ms > just_now_ms) in alarm_get_remaining_ms()
203 remaining_ms = alarm->deadline_ms - just_now_ms; in alarm_get_remaining_ms()
208 void alarm_set(alarm_t* alarm, uint64_t interval_ms, alarm_callback_t cb, in alarm_set() argument
210 alarm_set_internal(alarm, interval_ms, cb, data, default_callback_queue, in alarm_set()
214 void alarm_set_on_mloop(alarm_t* alarm, uint64_t interval_ms, in alarm_set_on_mloop() argument
216 alarm_set_internal(alarm, interval_ms, cb, data, NULL, true); in alarm_set_on_mloop()
220 static void alarm_set_internal(alarm_t* alarm, uint64_t period_ms, in alarm_set_internal() argument
224 log::assert_that(alarm != NULL, "assert failed: alarm != NULL"); in alarm_set_internal()
229 alarm->creation_time_ms = now_ms(); in alarm_set_internal()
230 alarm->period_ms = period_ms; in alarm_set_internal()
231 alarm->queue = queue; in alarm_set_internal()
232 alarm->callback = cb; in alarm_set_internal()
233 alarm->data = data; in alarm_set_internal()
234 alarm->for_msg_loop = for_msg_loop; in alarm_set_internal()
236 schedule_next_instance(alarm); in alarm_set_internal()
237 alarm->stats.scheduled_count++; in alarm_set_internal()
240 void alarm_cancel(alarm_t* alarm) { in alarm_cancel() argument
242 if (!alarm) return; in alarm_cancel()
247 local_mutex_ref = alarm->callback_mutex; in alarm_cancel()
248 alarm_cancel_internal(alarm); in alarm_cancel()
257 static void alarm_cancel_internal(alarm_t* alarm) { in alarm_cancel_internal() argument
259 (!list_is_empty(alarms) && list_front(alarms) == alarm); in alarm_cancel_internal()
261 remove_pending_alarm(alarm); in alarm_cancel_internal()
263 alarm->deadline_ms = 0; in alarm_cancel_internal()
264 alarm->prev_deadline_ms = 0; in alarm_cancel_internal()
265 alarm->callback = NULL; in alarm_cancel_internal()
266 alarm->data = NULL; in alarm_cancel_internal()
267 alarm->stats.canceled_count++; in alarm_cancel_internal()
268 alarm->queue = NULL; in alarm_cancel_internal()
273 bool alarm_is_scheduled(const alarm_t* alarm) { in alarm_is_scheduled() argument
274 if ((alarms == NULL) || (alarm == NULL)) return false; in alarm_is_scheduled()
275 return (alarm->callback != NULL); in alarm_is_scheduled()
398 static void remove_pending_alarm(alarm_t* alarm) { in remove_pending_alarm() argument
399 list_remove(alarms, alarm); in remove_pending_alarm()
401 if (alarm->for_msg_loop) { in remove_pending_alarm()
402 alarm->closure.i.Cancel(); in remove_pending_alarm()
404 while (fixed_queue_try_remove_from_queue(alarm->queue, alarm) != NULL) { in remove_pending_alarm()
413 static void schedule_next_instance(alarm_t* alarm) { in schedule_next_instance() argument
417 (!list_is_empty(alarms) && list_front(alarms) == alarm); in schedule_next_instance()
418 if (alarm->callback) remove_pending_alarm(alarm); in schedule_next_instance()
423 if ((alarm->is_periodic) && (alarm->period_ms != 0)) in schedule_next_instance()
425 ((just_now_ms - alarm->creation_time_ms) % alarm->period_ms); in schedule_next_instance()
426 alarm->deadline_ms = just_now_ms + (alarm->period_ms - ms_into_period); in schedule_next_instance()
430 ((alarm_t*)list_front(alarms))->deadline_ms > alarm->deadline_ms) { in schedule_next_instance()
431 list_prepend(alarms, alarm); in schedule_next_instance()
437 ((alarm_t*)list_node(next))->deadline_ms > alarm->deadline_ms) { in schedule_next_instance()
438 list_insert_after(alarms, node, alarm); in schedule_next_instance()
447 (!list_is_empty(alarms) && list_front(alarms) == alarm)) { in schedule_next_instance()
548 static void alarm_ready_generic(alarm_t* alarm, in alarm_ready_generic() argument
550 if (alarm == NULL) { in alarm_ready_generic()
559 if (!alarm->callback) { in alarm_ready_generic()
560 log::fatal("timer callback is NULL! Name={}", alarm->stats.name); in alarm_ready_generic()
562 alarm_callback_t callback = alarm->callback; in alarm_ready_generic()
563 void* data = alarm->data; in alarm_ready_generic()
564 uint64_t deadline_ms = alarm->deadline_ms; in alarm_ready_generic()
565 if (alarm->is_periodic) { in alarm_ready_generic()
568 deadline_ms = alarm->prev_deadline_ms; in alarm_ready_generic()
570 alarm->deadline_ms = 0; in alarm_ready_generic()
571 alarm->callback = NULL; in alarm_ready_generic()
572 alarm->data = NULL; in alarm_ready_generic()
573 alarm->queue = NULL; in alarm_ready_generic()
578 std::shared_ptr<std::recursive_mutex> local_mutex_ref = alarm->callback_mutex; in alarm_ready_generic()
583 update_scheduling_stats(&alarm->stats, now_ms(), deadline_ms); in alarm_ready_generic()
590 static void alarm_ready_mloop(alarm_t* alarm) { in alarm_ready_mloop() argument
592 alarm_ready_generic(alarm, lock); in alarm_ready_mloop()
599 alarm_t* alarm = (alarm_t*)fixed_queue_try_dequeue(queue); in alarm_queue_ready() local
600 alarm_ready_generic(alarm, lock); in alarm_queue_ready()
616 alarm_t* alarm; in callback_dispatch() local
622 (alarm = static_cast<alarm_t*>(list_front(alarms)))->deadline_ms > in callback_dispatch()
628 list_remove(alarms, alarm); in callback_dispatch()
630 if (alarm->is_periodic) { in callback_dispatch()
631 alarm->prev_deadline_ms = alarm->deadline_ms; in callback_dispatch()
632 schedule_next_instance(alarm); in callback_dispatch()
633 alarm->stats.rescheduled_count++; in callback_dispatch()
638 if (alarm->for_msg_loop) { in callback_dispatch()
640 log::error("message loop already NULL. Alarm: {}", alarm->stats.name); in callback_dispatch()
644 alarm->closure.i.Reset(Bind(alarm_ready_mloop, alarm)); in callback_dispatch()
645 get_main_thread()->DoInThread(FROM_HERE, alarm->closure.i.callback()); in callback_dispatch()
647 fixed_queue_enqueue(alarm->queue, alarm); in callback_dispatch()
731 alarm_t* alarm = (alarm_t*)list_node(node); in alarm_debug_dump() local
732 alarm_stats_t* stats = &alarm->stats; in alarm_debug_dump()
735 (alarm->is_periodic) ? "PERIODIC" : "SINGLE"); in alarm_debug_dump()
748 (unsigned long long)(just_now_ms - alarm->creation_time_ms), in alarm_debug_dump()
749 (unsigned long long)alarm->period_ms, in alarm_debug_dump()
750 (long long)(alarm->deadline_ms - just_now_ms)); in alarm_debug_dump()