Home | History | Annotate | Line # | Download | only in linux
timer.h revision 1.10
      1 /*	$NetBSD: timer.h,v 1.10 2021/12/19 10:38:05 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Notes on porting:
     34  *
     35  * - Linux does not have teardown_timer.  You must add it yourself in
     36  *   the appropriate place.
     37  */
     38 
     39 #ifndef _LINUX_TIMER_H_
     40 #define _LINUX_TIMER_H_
     41 
     42 #include <sys/callout.h>
     43 
     44 #include <linux/jiffies.h>
     45 #include <linux/ktime.h>
     46 
     47 struct timer_list {
     48 	struct callout tl_callout;
     49 };
     50 
     51 #define	from_timer(V, T, F)	container_of(T, __typeof__(*(V)), F)
     52 
     53 static inline void
     54 timer_setup(struct timer_list *timer, void (*fn)(struct timer_list *),
     55     uintptr_t flags)
     56 {
     57 
     58 	callout_init(&timer->tl_callout, 0);
     59 
     60 	/* XXX Super-sketchy casts!  */
     61 	callout_setfunc(&timer->tl_callout, (void (*)(void *))(void *)fn,
     62 	    (void *)flags);
     63 }
     64 
     65 static inline void
     66 teardown_timer(struct timer_list *timer)
     67 {
     68 
     69 	callout_destroy(&timer->tl_callout);
     70 }
     71 
     72 static inline void
     73 mod_timer(struct timer_list *timer, unsigned long then)
     74 {
     75 	const unsigned long now = jiffies;
     76 
     77 	callout_schedule(&timer->tl_callout, (now < then? (then - now) : 0));
     78 }
     79 
     80 static inline void
     81 mod_timer_pinned(struct timer_list *timer, unsigned long then)
     82 {
     83 
     84 	/* XXX Stay on the same CPU it was originally on...  */
     85 	mod_timer(timer, then);
     86 }
     87 
     88 static inline void
     89 del_timer(struct timer_list *timer)
     90 {
     91 
     92 	callout_stop(&timer->tl_callout);
     93 }
     94 
     95 static inline bool
     96 del_timer_sync(struct timer_list *timer)
     97 {
     98 
     99 	/* XXX return values? */
    100 	return callout_halt(&timer->tl_callout, NULL);
    101 }
    102 
    103 static inline bool
    104 timer_pending(struct timer_list *timer)
    105 {
    106 
    107 	return callout_pending(&timer->tl_callout);
    108 }
    109 
    110 /*
    111  * XXX This is bogus -- the Linux version does various machinations to
    112  * give some jitter so that stuff doesn't wake up all at once.
    113  */
    114 
    115 static inline unsigned long
    116 round_jiffies_up(unsigned long j)
    117 {
    118 	return roundup(j, hz);
    119 }
    120 
    121 static inline unsigned long
    122 round_jiffies_up_relative(unsigned long j)
    123 {
    124 	return roundup(j, hz);
    125 }
    126 
    127 #endif  /* _LINUX_TIMER_H_ */
    128