File: ktimer.c

package info (click to toggle)
klic 3.003-1.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,068 kB
  • ctags: 6,333
  • sloc: ansic: 101,584; makefile: 3,395; sh: 1,321; perl: 312; exp: 131; tcl: 111; asm: 102; lisp: 4; sed: 1
file content (184 lines) | stat: -rw-r--r-- 4,711 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* ---------------------------------------------------------- 
%   (C)1995 Institute for New Generation Computer Technology 
%       (Read COPYRIGHT for detailed information.) 
%   (C)1996, 1997, 1998, 1999 Japan Information Processing Development Center
%       (Read COPYRIGHT-JIPDEC for detailed information.)
----------------------------------------------------------- */

#include <klic/basic.h>
#ifdef USETIMER
#include <sys/time.h>
#include <klic/struct.h>
#include <klic/timer.h>
#include <klic/sighndl.h>

extern char *malloc_check();

void klic_timer_interrupt_handler()
{
  declare_globals;
  interrupt_off = 0;
  heaplimit = 0;
  signal_done = 0;
  signal_flags[SIGALRM] = 1;
}

struct timer_reservation_rec {
  struct timeval at;
  struct timeval interval;
  struct timer_reservation_rec *next;
  int (*handler)();
  q data;
};
static struct timer_reservation_rec *timer_reservations;
static struct timer_reservation_rec *timer_rsv_free;

static int process_timed_instantiation(allocp, data)
     q *allocp;
     q data;
{
  declare_globals;
  extern q *do_unify_value();

  heapp = do_unify_value(allocp, data, NILATOM);
  return 0;
}

static int process_simple_timer_handler(allocp, data)
     q *allocp;
     q data;
{
  declare_globals;

  return ((int (*)())((unsigned long)data-ATOMIC))(allocp, SIGALRM);
}

int
process_timer_interrupt(allocp, sig)
     q* allocp;
     long sig;
{
  declare_globals;
  struct timeval now;

  klic_gettod(&now);
  while (timer_reservations != 0 &&
	 NotLaterThan(timer_reservations->at, now)) {
    int result =
      timer_reservations-> handler(allocp, timer_reservations->data);
    if (result != 0) return result;
    allocp = heapp;
    if (TimeIsZero(timer_reservations->interval)) {
      struct timer_reservation_rec *tmp = timer_reservations->next;
      timer_reservations->next = timer_rsv_free;
      timer_rsv_free = timer_reservations;
      timer_reservations = tmp;
    } else {
      struct timer_reservation_rec **tmp, *trec;
      trec = timer_reservations;
      TimeAdd(trec->at, trec->interval, trec->at);
      timer_reservations = timer_reservations->next;
      for (tmp = &timer_reservations;
	   *tmp != 0 && NotLaterThan((*tmp)->at, trec->at);
	   tmp = &(*tmp)->next)
	;
      trec->next = *tmp;
      *tmp = trec;
    }
  }
  if (timer_reservations != 0) {
    call_at_specified_time(timer_reservations->at.tv_sec,
			   timer_reservations->at.tv_usec,
			   klic_timer_interrupt_handler);
  }
  heapp = allocp;
  return 0;
}


static q* gc_timer_data(allocp, ntop, otop, nsize, osize)
     q *allocp, *ntop, *otop;
     unsigned long nsize, osize;
{
  struct timer_reservation_rec *trec;
  extern q *copy_one_term();
  for (trec = timer_reservations;
       trec != 0;
       trec = trec->next) {
    allocp =
      copy_one_term(&trec->data, allocp, ntop, otop, nsize, osize);
  }
  return allocp;
}

void init_klic_timer_handling()
{
  timer_reservations = 0;
  timer_rsv_free = 0;
  register_gc_hook(gc_timer_data);
  add_slit_check_handler(SIGALRM, process_timer_interrupt);
}

static struct timer_reservation_rec *
alloc_timer_reservation_rec()
{
  struct timer_reservation_rec *newrec;
  if (timer_rsv_free == 0) {
    newrec = (struct timer_reservation_rec *)
      malloc_check(sizeof(struct timer_reservation_rec));
  } else {
    newrec = timer_rsv_free;
    timer_rsv_free = timer_rsv_free->next;
  }
  return newrec;
}

q *set_simple_interval_timer_handler(allocp, sec, usec, func)
     q *allocp;
     long sec, usec;
     int (*func)();
{
  declare_globals;
  struct timeval now;
  struct timer_reservation_rec *newrec, **tmp;

  klic_gettod(&now);
  newrec = alloc_timer_reservation_rec();
  newrec->at = now;
  TimeSet(newrec->interval, sec, usec);
  TimeAdd(newrec->at, newrec->interval, newrec->at);
  newrec->handler = process_simple_timer_handler;
  newrec->data = makeatomic(func);
  for (tmp = &timer_reservations;
       *tmp != 0 && NotLaterThan((*tmp)->at, newrec->at);
       tmp = &(*tmp)->next)
    ;
  newrec->next = *tmp;
  *tmp = newrec;
  process_timer_interrupt(allocp, SIGALRM);
  return heapp;
}  

q *reserve_klic_timer_interrupt(allocp, sec, usec, vrbl)
     q *allocp;
     long sec, usec;
     q vrbl;
{
  declare_globals;
  struct timer_reservation_rec *newrec, **tmp;

  newrec = alloc_timer_reservation_rec();
  TimeSet(newrec->at, sec, usec);
  TimeSet(newrec->interval, 0, 0);
  newrec->handler = process_timed_instantiation;
  newrec->data = vrbl;
  for (tmp = &timer_reservations;
       *tmp != 0 && NotLaterThan((*tmp)->at, newrec->at);
       tmp = &(*tmp)->next)
    ;
  newrec->next = *tmp;
  *tmp = newrec;
  process_timer_interrupt(allocp, SIGALRM);
  return heapp;
}
#endif