The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linuxkpi/common/src/linux_hrtimer.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice unmodified, this list of conditions, and the following
    9  *    disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   24  */
   25 
   26 #include <sys/cdefs.h>
   27 __FBSDID("$FreeBSD$");
   28 
   29 #include <sys/param.h>
   30 #include <sys/systm.h>
   31 #include <sys/lock.h>
   32 #include <sys/mutex.h>
   33 #include <sys/time.h>
   34 
   35 #include <machine/cpu.h>
   36 
   37 #include <linux/hrtimer.h>
   38 
   39 static void
   40 hrtimer_call_handler(void *arg)
   41 {
   42         struct hrtimer *hrtimer;
   43         enum hrtimer_restart ret;
   44 
   45         hrtimer = arg;
   46         ret = hrtimer->function(hrtimer);
   47 
   48         if (ret == HRTIMER_RESTART) {
   49                 callout_schedule_sbt(&hrtimer->callout,
   50                     nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0);
   51         } else {
   52                 callout_deactivate(&hrtimer->callout);
   53         }
   54 }
   55 
   56 bool
   57 linux_hrtimer_active(struct hrtimer *hrtimer)
   58 {
   59         bool ret;
   60 
   61         mtx_lock(&hrtimer->mtx);
   62         ret = callout_active(&hrtimer->callout);
   63         mtx_unlock(&hrtimer->mtx);
   64 
   65         return (ret);
   66 }
   67 
   68 /*
   69  * Try to cancel active hrtimer.
   70  * Return 1 if timer was active and cancellation succeeded, 0 if timer was
   71  * inactive, or -1 if the timer is being serviced and can't be cancelled.
   72  */
   73 int
   74 linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer)
   75 {
   76         int ret;
   77 
   78         mtx_lock(&hrtimer->mtx);
   79         ret = callout_stop(&hrtimer->callout);
   80         mtx_unlock(&hrtimer->mtx);
   81         if (ret > 0) {
   82                 return (1);
   83         } else if (ret < 0) {
   84                 return (0);
   85         } else {
   86                 return (-1);
   87         }
   88 }
   89 
   90 /*
   91  * Cancel active hrtimer.
   92  * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
   93  */
   94 int
   95 linux_hrtimer_cancel(struct hrtimer *hrtimer)
   96 {
   97 
   98         return (callout_drain(&hrtimer->callout) > 0);
   99 }
  100 
  101 void
  102 linux_hrtimer_init(struct hrtimer *hrtimer)
  103 {
  104 
  105         memset(hrtimer, 0, sizeof(*hrtimer));
  106         mtx_init(&hrtimer->mtx, "hrtimer", NULL,
  107             MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
  108         callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
  109 }
  110 
  111 void
  112 linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)
  113 {
  114         hrtimer->expires = ktime_to_ns(time);
  115 }
  116 
  117 void
  118 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
  119 {
  120 
  121         linux_hrtimer_start_range_ns(hrtimer, time, 0);
  122 }
  123 
  124 void
  125 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,
  126     int64_t nsec)
  127 {
  128 
  129         mtx_lock(&hrtimer->mtx);
  130         hrtimer->precision = nsec;
  131         callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),
  132             nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);
  133         mtx_unlock(&hrtimer->mtx);
  134 }
  135 
  136 void
  137 linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)
  138 {
  139 
  140         mtx_lock(&hrtimer->mtx);
  141         callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),
  142             nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);
  143         mtx_unlock(&hrtimer->mtx);
  144 }

Cache object: dcde9353418b4f971b1fb458fd95bea3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.