1 /****************************************************************************** 2 * 3 * Copyright (C) 2014 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #pragma once 20 21 #include <stdint.h> 22 23 typedef struct alarm_t alarm_t; 24 typedef uint64_t period_ms_t; 25 26 // Prototype for the callback function. 27 typedef void (*alarm_callback_t)(void *data); 28 29 // Creates a new alarm object. The returned object must be freed by calling 30 // |alarm_free|. Returns NULL on failure. 31 alarm_t *alarm_new(void); 32 33 // Frees an alarm object created by |alarm_new|. |alarm| may be NULL. If the 34 // alarm is pending, it will be cancelled. It is not safe to call |alarm_free| 35 // from inside the callback of |alarm|. 36 void alarm_free(alarm_t *alarm); 37 38 // Sets an alarm to fire |cb| after the given |deadline|. Note that |deadline| is the 39 // number of milliseconds relative to the current time. |data| is a context variable 40 // for the callback and may be NULL. |cb| will be called back in the context of an 41 // unspecified thread (i.e. it will not be called back in the same thread as the caller). 42 // |alarm| and |cb| may not be NULL. 43 void alarm_set(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *data); 44 45 // Like alarm_set, except the alarm repeats every |period| ms until it is cancelled or 46 // freed or |alarm_set| is called to set it non-periodically. 47 void alarm_set_periodic(alarm_t *alarm, period_ms_t period, alarm_callback_t cb, void *data); 48 49 // This function cancels the |alarm| if it was previously set. When this call 50 // returns, the caller has a guarantee that the callback is not in progress and 51 // will not be called if it hasn't already been called. This function is idempotent. 52 // |alarm| may not be NULL. 53 void alarm_cancel(alarm_t *alarm); 54 55 // Figure out how much time until next expiration. 56 // Returns 0 if not armed. |alarm| may not be NULL. 57 // TODO: Remove this function once PM timers can be re-factored 58 period_ms_t alarm_get_remaining_ms(const alarm_t *alarm); 59 60 // Alarm-related state cleanup 61 void alarm_cleanup(void); 62