summaryrefslogtreecommitdiff
path: root/viengoos/timer.c
blob: 1e2e976c9709bb0644160e6f709f3abf49952786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* timer.c - Architecture independent timer implemenation.
   Copyright (C) 2009 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include "timer.h"
#include "timer-arch.h"
#include "bottom-half.h"

volatile struct vg_time time_data __attribute__ ((aligned (PAGESIZE)));

void
timer_bootstrap (void)
{
  timer_arch_bootstrap ();
}

/* Sorted by TIMER->EXPIRE.  */
struct timer *timers;

/* Whether a bottom half handler has been registered to run a
   timer.  */
bool timer_bottom_half_pending;

static void
timer_bottom_half_callback (struct bottom_half *bh)
{
  timer_bottom_half_pending = false;

  while (timers && unlikely (timers->expire <= time_data.ns_since_boot))
    {
      struct timer *t = timers;
      timers = t->next;
      t->next = 0;

      t->callback (t);
    }
}

void
timer_register (struct timer *timer)
{
  assert (! timer->next);

  struct timer *t;
  struct timer **prevp;
  for (t = timers, prevp = &timers; t; prevp = &t->next, t = t->next)
    if (t->expire > timer->expire)
      break;

  timer->next = *prevp;
  *prevp = timer;

  if (t)
    {
      timer->prev = t->prev;
      t->prev = timer;
    }
  else
    timer->prev = 0;
}

void
timer_cancel (struct timer *timer)
{
  if (! timer->prev)
    assert (timer == timers);
  else
    assert (timer->prev->next == timer);

  if (timer->next)
    assert (timer->next->prev == timer);

  if (timer->next)
    timer->next->prev = timer->prev;

  if (timer->prev)
    timer->prev = timer->next;
  else
    timers = timer->next;

#ifndef NDEBUG
  timer->next = timer->prev = 0;
#endif
}

void
timer_check_hard (void)
{
  if (timers && unlikely (timers->expire <= time_data.ns_since_boot)
      && ! timer_bottom_half_pending)
    {
      static struct bottom_half timer_bottom_half
	= { .callback = timer_bottom_half_callback };

      timer_bottom_half_pending = true;
      bottom_half_register (&timer_bottom_half);
    }
}