1/** 2 * \file 3 * \brief Barrelfish timer interface. 4 */ 5 6/* 7 * Copyright (c) 2007, 2008, ETH Zurich. 8 * All rights reserved. 9 * 10 * This file is distributed under the terms in the attached LICENSE file. 11 * If you do not find this file, copies can be found by writing to: 12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group. 13 */ 14 15#ifndef BF_TIMER_H 16#define BF_TIMER_H 17 18#include <stdbool.h> 19#include <stdint.h> 20#include <sys/cdefs.h> 21 22__BEGIN_DECLS 23 24struct timer; 25 26/** 27 * Timer callback function prototype. 28 */ 29typedef void (*timer_callback_fn)(struct timer *, void *); 30 31struct timer { 32 struct timer *prev, *next; 33 uint64_t expires; 34 uint64_t duration; 35 bool periodic; 36 timer_callback_fn callback; 37 void *data; 38}; 39 40errval_t timer_init(void); 41struct timer *timer_new(void); 42void timer_start(struct timer *timer); 43void timer_stop(struct timer *timer); 44uint64_t timer_remaining(struct timer *timer); 45void timer_destroy(struct timer *timer); 46bool timer_is_running(struct timer *timer); 47 48/** 49 * \brief Set timer duration. 50 * 51 * \param timer Pointer to timer to set duration for. 52 * \param duration Duration in us to set. 53 * \param periodic true if timer is periodic. 54 */ 55static inline void timer_set_duration(struct timer *timer, uint64_t duration, 56 bool periodic) 57{ 58 timer->duration = duration; 59 timer->periodic = periodic; 60} 61 62/** 63 * \brief Set timeout callback function. 64 * 65 * \param timer Pointer to timer to set callback function for. 66 * \param callback Callback function. 67 * \param data Private data of callback function. 68 */ 69static inline void timer_set_callback(struct timer *timer, 70 timer_callback_fn callback, void *data) 71{ 72 timer->callback = callback; 73 timer->data = data; 74} 75 76static inline struct timer *timer_create(uint64_t duration, bool periodic, 77 timer_callback_fn callback, void *data) 78{ 79 struct timer *newtimer = timer_new(); 80 81 assert(newtimer != NULL); 82 timer_set_duration(newtimer, duration, periodic); 83 timer_set_callback(newtimer, callback, data); 84 85 return newtimer; 86} 87 88__END_DECLS 89 90#endif 91