1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #ifndef CRAS_TM_H_ 7 #define CRAS_TM_H_ 8 9 /* cras_timer provides an interface to register a function to be called at a 10 * later time. This interface should be used from the main thread only, it is 11 * not thread safe. 12 */ 13 14 struct cras_tm; /* timer manager */ 15 struct cras_timer; 16 17 /* Creates a timer. Must later be removed with cras_rm_cancel_timer. 18 * Args: 19 * tm - Timer manager. 20 * ms - Call 'cb' in ms milliseconds. 21 * cb - The callback to call at timeout. 22 * cb_data - Passed to the callback when it is run. 23 * Returns: 24 * Pointer to a newly allocated timer, passed timer to cras_tm_cancel_timer 25 * to cancel before it fires. 26 */ 27 struct cras_timer *cras_tm_create_timer(struct cras_tm *tm, unsigned int ms, 28 void (*cb)(struct cras_timer *t, 29 void *data), 30 void *cb_data); 31 32 /* Deletes a timer returned from cras_tm_create_timer. */ 33 void cras_tm_cancel_timer(struct cras_tm *tm, struct cras_timer *t); 34 35 /* Interface for system to create the timer manager. */ 36 struct cras_tm *cras_tm_init(); 37 38 /* Interface for system to destroy the timer manager. */ 39 void cras_tm_deinit(struct cras_tm *tm); 40 41 /* Get the amount of time before the next timer expires. ts is set to an 42 * the amount of time before the next timer expires (0 if already past due). 43 * Args: 44 * tm - Timer manager. 45 * ts - Filled with time before next event. 46 * Returns: 47 * 0 if no timers are active, 1 if they are. 48 */ 49 int cras_tm_get_next_timeout(const struct cras_tm *tm, struct timespec *ts); 50 51 /* Calls any expired timers. */ 52 void cras_tm_call_callbacks(struct cras_tm *tm); 53 54 #endif /* CRAS_TM_H_ */ 55