~kris/9p

9hist

ref: 0ed7376734c237eeed1c7d91d094c06ebbd2a9cb 9hist/pc/cycintr.c -rw-r--r-- 1.2 KiB
0ed73767 — David du Colombier Plan 9 from Bell Labs 2002-04-10 24 years ago
                                                                                
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
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"


static struct
{
	Lock;
	Timer	*ci;
}timers;

/*
 * called by clockintrsched()
 */
vlong
timernext(void)
{
	Timer *ci;
	vlong when;

	ilock(&timers);
	when = 0;
	ci = timers.ci;
	if(ci != nil)
		when = ci->when;
	iunlock(&timers);
	return when;
}

vlong
checktimer(Ureg *u, void*)
{
	Timer *ci;
	vlong when;

	ilock(&timers);
	while(ci = timers.ci){
		when = ci->when;
		if(when > fastticks(nil)){
			iunlock(&timers);
			return when;
		}
		timers.ci = ci->next;
		iunlock(&timers);
		(*ci->f)(u, ci);
		ilock(&timers);
	}
	iunlock(&timers);
	return 0;
}

void
timeradd(Timer *nci)
{
	Timer *ci, **last;

	ilock(&timers);
	last = &timers.ci;
	while(ci = *last){
		if(ci == nci){
			*last = ci->next;
			break;
		}
		last = &ci->next;
	}

	last = &timers.ci;
	while(ci = *last){
		if(ci->when > nci->when)
			break;
		last = &ci->next;
	}
	nci->next = *last;
	*last = nci;
	iunlock(&timers);
}

void
timerdel(Timer *dci)
{
	Timer *ci, **last;

	ilock(&timers);
	last = &timers.ci;
	while(ci = *last){
		if(ci == dci){
			*last = ci->next;
			break;
		}
		last = &ci->next;
	}
	iunlock(&timers);
}