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 typedef struct alarm_t alarm_t; 22 typedef uint64_t period_ms_t; 23 24 // Prototype for the callback function. 25 typedef void (*alarm_callback_t)(void *data); 26 27 // Creates a new alarm object. The returned object must be freed by calling 28 // |alarm_free|. Returns NULL on failure. 29 alarm_t *alarm_new(void); 30 31 // Frees an alarm object created by |alarm_new|. |alarm| may be NULL. If the 32 // alarm is pending, it will be cancelled. It is not safe to call |alarm_free| 33 // from inside the callback of |alarm|. 34 void alarm_free(alarm_t *alarm); 35 36 // Sets an alarm to fire |cb| after the given |deadline|. Note that |deadline| is the 37 // number of milliseconds relative to the current time. |data| is a context variable 38 // for the callback and may be NULL. |cb| will be called back in the context of an 39 // unspecified thread (i.e. it will not be called back in the same thread as the caller). 40 // |alarm| and |cb| may not be NULL. 41 void alarm_set(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *data); 42 43 // This function cancels the |alarm| if it was previously set. When this call 44 // returns, the caller has a guarantee that the callback is not in progress and 45 // will not be called if it hasn't already been called. This function is idempotent. 46 // |alarm| may not be NULL. 47 void alarm_cancel(alarm_t *alarm); 48