Lines Matching refs:alarm
85 static void alarm_set_internal(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *dat…
86 static void schedule_next_instance(alarm_t *alarm, bool force_reschedule);
128 void alarm_free(alarm_t *alarm) { in alarm_free() argument
129 if (!alarm) in alarm_free()
132 alarm_cancel(alarm); in alarm_free()
133 pthread_mutex_destroy(&alarm->callback_lock); in alarm_free()
134 osi_free(alarm); in alarm_free()
137 period_ms_t alarm_get_remaining_ms(const alarm_t *alarm) { in alarm_get_remaining_ms() argument
138 assert(alarm != NULL); in alarm_get_remaining_ms()
142 if (alarm->deadline) in alarm_get_remaining_ms()
143 remaining_ms = alarm->deadline - now(); in alarm_get_remaining_ms()
149 void alarm_set(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *data) { in alarm_set() argument
150 alarm_set_internal(alarm, deadline, cb, data, false); in alarm_set()
153 void alarm_set_periodic(alarm_t *alarm, period_ms_t period, alarm_callback_t cb, void *data) { in alarm_set_periodic() argument
154 alarm_set_internal(alarm, period, cb, data, true); in alarm_set_periodic()
158 static void alarm_set_internal(alarm_t *alarm, period_ms_t period, alarm_callback_t cb, void *data,… in alarm_set_internal() argument
160 assert(alarm != NULL); in alarm_set_internal()
165 alarm->created = now(); in alarm_set_internal()
166 alarm->is_periodic = is_periodic; in alarm_set_internal()
167 alarm->period = period; in alarm_set_internal()
168 alarm->callback = cb; in alarm_set_internal()
169 alarm->data = data; in alarm_set_internal()
171 schedule_next_instance(alarm, false); in alarm_set_internal()
176 void alarm_cancel(alarm_t *alarm) { in alarm_cancel() argument
178 assert(alarm != NULL); in alarm_cancel()
182 bool needs_reschedule = (!list_is_empty(alarms) && list_front(alarms) == alarm); in alarm_cancel()
184 list_remove(alarms, alarm); in alarm_cancel()
185 alarm->deadline = 0; in alarm_cancel()
186 alarm->callback = NULL; in alarm_cancel()
187 alarm->data = NULL; in alarm_cancel()
195 pthread_mutex_lock(&alarm->callback_lock); in alarm_cancel()
196 pthread_mutex_unlock(&alarm->callback_lock); in alarm_cancel()
269 static void schedule_next_instance(alarm_t *alarm, bool force_reschedule) { in schedule_next_instance() argument
272 bool needs_reschedule = (!list_is_empty(alarms) && list_front(alarms) == alarm); in schedule_next_instance()
273 if (alarm->callback) in schedule_next_instance()
274 list_remove(alarms, alarm); in schedule_next_instance()
278 …period_ms_t ms_into_period = alarm->is_periodic ? ((just_now - alarm->created) % alarm->period) : … in schedule_next_instance()
279 alarm->deadline = just_now + (alarm->period - ms_into_period); in schedule_next_instance()
282 if (list_is_empty(alarms) || ((alarm_t *)list_front(alarms))->deadline >= alarm->deadline) in schedule_next_instance()
283 list_prepend(alarms, alarm); in schedule_next_instance()
287 if (next == list_end(alarms) || ((alarm_t *)list_node(next))->deadline >= alarm->deadline) { in schedule_next_instance()
288 list_insert_after(alarms, node, alarm); in schedule_next_instance()
294 …f (force_reschedule || needs_reschedule || (!list_is_empty(alarms) && list_front(alarms) == alarm)) in schedule_next_instance()
370 alarm_t *alarm; in callback_dispatch() local
376 if (list_is_empty(alarms) || (alarm = list_front(alarms))->deadline > now()) { in callback_dispatch()
382 list_remove(alarms, alarm); in callback_dispatch()
384 alarm_callback_t callback = alarm->callback; in callback_dispatch()
385 void *data = alarm->data; in callback_dispatch()
387 if (alarm->is_periodic) { in callback_dispatch()
388 schedule_next_instance(alarm, true); in callback_dispatch()
392 alarm->deadline = 0; in callback_dispatch()
393 alarm->callback = NULL; in callback_dispatch()
394 alarm->data = NULL; in callback_dispatch()
398 pthread_mutex_lock(&alarm->callback_lock); in callback_dispatch()
403 pthread_mutex_unlock(&alarm->callback_lock); in callback_dispatch()