177343d59SFrançois Tigeot /*- 277343d59SFrançois Tigeot * Copyright (c) 2010 Isilon Systems, Inc. 377343d59SFrançois Tigeot * Copyright (c) 2010 iX Systems, Inc. 477343d59SFrançois Tigeot * Copyright (c) 2010 Panasas, Inc. 5fe87864cSFrançois Tigeot * Copyright (c) 2014 François Tigeot 677343d59SFrançois Tigeot * All rights reserved. 777343d59SFrançois Tigeot * 877343d59SFrançois Tigeot * Redistribution and use in source and binary forms, with or without 977343d59SFrançois Tigeot * modification, are permitted provided that the following conditions 1077343d59SFrançois Tigeot * are met: 1177343d59SFrançois Tigeot * 1. Redistributions of source code must retain the above copyright 1277343d59SFrançois Tigeot * notice unmodified, this list of conditions, and the following 1377343d59SFrançois Tigeot * disclaimer. 1477343d59SFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright 1577343d59SFrançois Tigeot * notice, this list of conditions and the following disclaimer in the 1677343d59SFrançois Tigeot * documentation and/or other materials provided with the distribution. 1777343d59SFrançois Tigeot * 1877343d59SFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1977343d59SFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2077343d59SFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2177343d59SFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2277343d59SFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2377343d59SFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2477343d59SFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2577343d59SFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2677343d59SFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2777343d59SFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2877343d59SFrançois Tigeot */ 2977343d59SFrançois Tigeot #ifndef _LINUX_TIMER_H_ 3077343d59SFrançois Tigeot #define _LINUX_TIMER_H_ 3177343d59SFrançois Tigeot 3277343d59SFrançois Tigeot #include <linux/types.h> 3377343d59SFrançois Tigeot 3477343d59SFrançois Tigeot #include <sys/param.h> 3577343d59SFrançois Tigeot #include <sys/kernel.h> 3677343d59SFrançois Tigeot #include <sys/callout.h> 37fe87864cSFrançois Tigeot #include <sys/thread.h> 3877343d59SFrançois Tigeot 3977343d59SFrançois Tigeot struct timer_list { 4077343d59SFrançois Tigeot struct callout timer_callout; 4177343d59SFrançois Tigeot void (*function)(unsigned long); 4277343d59SFrançois Tigeot unsigned long data; 4377343d59SFrançois Tigeot unsigned long expires; 44fe87864cSFrançois Tigeot struct lwkt_token timer_token; 4577343d59SFrançois Tigeot }; 4677343d59SFrançois Tigeot 4777343d59SFrançois Tigeot static inline void 4877343d59SFrançois Tigeot _timer_fn(void *context) 4977343d59SFrançois Tigeot { 5077343d59SFrançois Tigeot struct timer_list *timer; 5177343d59SFrançois Tigeot 5277343d59SFrançois Tigeot timer = context; 5377343d59SFrançois Tigeot timer->function(timer->data); 5477343d59SFrançois Tigeot } 5577343d59SFrançois Tigeot 5677343d59SFrançois Tigeot #define setup_timer(timer, func, dat) \ 5777343d59SFrançois Tigeot do { \ 5877343d59SFrançois Tigeot (timer)->function = (func); \ 5977343d59SFrançois Tigeot (timer)->data = (dat); \ 60fe87864cSFrançois Tigeot lwkt_token_init(&(timer)->timer_token, "timer token"); \ 61fe87864cSFrançois Tigeot callout_init_mp(&(timer)->timer_callout); \ 6277343d59SFrançois Tigeot } while (0) 6377343d59SFrançois Tigeot 6477343d59SFrançois Tigeot #define init_timer(timer) \ 6577343d59SFrançois Tigeot do { \ 6677343d59SFrançois Tigeot (timer)->function = NULL; \ 6777343d59SFrançois Tigeot (timer)->data = 0; \ 68fe87864cSFrançois Tigeot lwkt_token_init(&(timer)->timer_token, "timer token"); \ 69fe87864cSFrançois Tigeot callout_init_mp(&(timer)->timer_callout); \ 7077343d59SFrançois Tigeot } while (0) 7177343d59SFrançois Tigeot 7277343d59SFrançois Tigeot #define mod_timer(timer, exp) \ 7377343d59SFrançois Tigeot do { \ 7477343d59SFrançois Tigeot (timer)->expires = (exp); \ 75fe87864cSFrançois Tigeot lwkt_gettoken(&(timer)->timer_token); \ 7677343d59SFrançois Tigeot callout_reset(&(timer)->timer_callout, (exp) - jiffies, \ 7777343d59SFrançois Tigeot _timer_fn, (timer)); \ 78fe87864cSFrançois Tigeot lwkt_reltoken(&(timer)->timer_token); \ 7977343d59SFrançois Tigeot } while (0) 8077343d59SFrançois Tigeot 8177343d59SFrançois Tigeot #define add_timer(timer) \ 82fe87864cSFrançois Tigeot lwkt_gettoken(&(timer)->timer_token); \ 8377343d59SFrançois Tigeot callout_reset(&(timer)->timer_callout, \ 84fe87864cSFrançois Tigeot (timer)->expires - jiffies, _timer_fn, (timer)); \ 85fe87864cSFrançois Tigeot lwkt_reltoken(&(timer)->timer_token); 8677343d59SFrançois Tigeot 87fe87864cSFrançois Tigeot static inline void 88fe87864cSFrançois Tigeot del_timer(struct timer_list *timer) 89fe87864cSFrançois Tigeot { 90fe87864cSFrançois Tigeot lwkt_gettoken(&(timer)->timer_token); 91fe87864cSFrançois Tigeot callout_stop(&(timer)->timer_callout); 92fe87864cSFrançois Tigeot lwkt_reltoken(&(timer)->timer_token); 93fe87864cSFrançois Tigeot 94fe87864cSFrançois Tigeot lwkt_token_uninit(&(timer)->timer_token); 95fe87864cSFrançois Tigeot } 96fe87864cSFrançois Tigeot 9777343d59SFrançois Tigeot #define del_timer_sync(timer) callout_drain(&(timer)->timer_callout) 9877343d59SFrançois Tigeot 9977343d59SFrançois Tigeot #define timer_pending(timer) callout_pending(&(timer)->timer_callout) 10077343d59SFrançois Tigeot 10177343d59SFrançois Tigeot static inline unsigned long 10277343d59SFrançois Tigeot round_jiffies(unsigned long j) 10377343d59SFrançois Tigeot { 10477343d59SFrançois Tigeot return roundup(j, hz); 10577343d59SFrançois Tigeot } 10677343d59SFrançois Tigeot 107fe87864cSFrançois Tigeot static inline unsigned long 108fe87864cSFrançois Tigeot round_jiffies_up(unsigned long j) 109fe87864cSFrançois Tigeot { 110fe87864cSFrançois Tigeot return roundup(j, hz); 111fe87864cSFrançois Tigeot } 112fe87864cSFrançois Tigeot 1137f070b26SFrançois Tigeot static inline unsigned long 1147f070b26SFrançois Tigeot round_jiffies_up_relative(unsigned long j) 1157f070b26SFrançois Tigeot { 1167f070b26SFrançois Tigeot return roundup(j, hz); 1177f070b26SFrançois Tigeot } 1187f070b26SFrançois Tigeot 119*4c816622SFrançois Tigeot #define destroy_timer_on_stack(timer) 120*4c816622SFrançois Tigeot 12177343d59SFrançois Tigeot #endif /* _LINUX_TIMER_H_ */ 122