summaryrefslogtreecommitdiff
path: root/pfinet/timer-emul.c
blob: af1469efbd542559281735f972ad062e01e23fab (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* 
   Copyright (C) 1995 Free Software Foundation, Inc.
   Written by Michael I. Bushnell, p/BSG.

   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 2, 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, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <linux/timer.h>
#include <asm/system.h>
#include <linux/sched.h>
#include <string.h>
#include "pfinet.h"

long long root_jiffies;
volatile struct mapped_time_value *mapped_time;

struct timer_list *timers;
thread_t timer_thread = 0;

static int
timer_function (int this_is_a_pointless_variable_with_a_rather_long_name)
{
  mach_port_t recv;
  int wait = 0;

  recv = mach_reply_port ();
  
  timer_thread = mach_thread_self ();
  
  mutex_lock (&global_lock);
  while (1)
    {
      int jiff = jiffies;
      
      if (!timers)
	wait = -1;
      else if (timers->expires < jiff)
	wait = 0;
      else
	wait = ((timers->expires - jiff) * 1000) / HZ;
	
      mutex_unlock (&global_lock);
      
      mach_msg (NULL, (MACH_RCV_MSG | MACH_RCV_INTERRUPT 
		       | (wait == -1 ? 0 : MACH_RCV_TIMEOUT)),
		0, 0, recv, wait, MACH_PORT_NULL);

      mutex_lock (&global_lock);
      
      while (timers->expires < jiffies)
	{
	  struct timer_list *tp;
	  
	  tp = timers;

	  timers = timers->next;
	  if (timers->next)
	    timers->next->prevp = &timers;
	  
	  tp->next = 0;
	  tp->prevp = 0;
	  
	  (*tp->function) (tp->data);
	}
    }
}  


void
add_timer (struct timer_list *timer)
{
  struct timer_list **tp;
  
  timer->expires += jiffies;

  for (tp = &timers; *tp; tp = &(*tp)->next)
    if ((*tp)->expires > timer->expires)
      {
	timer->next = *tp;
	timer->next->prevp = &timer->next;
	timer->prevp = tp;
	*tp = timer;
	break;
      }
  if (!*tp)
    {
      timer->next = 0;
      timer->prevp = tp;
      *tp = timer;
    }
  
  if (timers == timer)
    {
      /* We have change the first one, so tweak the timer thread
	 to push things up. */
      while (timer_thread == 0)
	swtch_pri (0);
	  
      thread_suspend (timer_thread);
      thread_abort (timer_thread);
      thread_resume (timer_thread);
    }
}

int
del_timer (struct timer_list *timer)
{
  if (timer->prevp)
    {
      *timer->prevp = timer->next;
      if (timer->next)
	timer->next->prevp = timer->prevp;
  
      timer->next = 0;
      timer->prevp = 0;
      return 1;
    }
  else
    return 0;
}

void
init_timer (struct timer_list *timer)
{
  bzero (timer, sizeof (struct timer_list));
}

void
init_time ()
{
  device_t timedev;
  memory_object_t timeobj;
  struct timeval tp;
  
  device_open (master_device, 0, "time", &timedev);
  device_map (timedev, VM_PROT_READ, 0, sizeof (mapped_time_value_t),
	      &timeobj, 0);
  vm_map (mach_task_self (), (vm_address_t *)&mapped_time,
	  sizeof (mapped_time_value_t), 0, 1, timeobj, 0, 0,
	  VM_PROT_READ, VM_PROT_READ, VM_INHERIT_NONE);
  mach_port_deallocate (mach_task_self (), timedev);
  mach_port_deallocate (mach_task_self (), timeobj);

  fill_timeval (&tp);
  
  root_jiffies = (long long) tp.tv_sec * HZ 
    + (long long) tp.tv_usec * HZ / 1000.0;

  cthread_detach (cthread_fork ((cthread_fn_t) timer_function, 0));
}