CS350 COS
COS
Loading...
Searching...
No Matches
ktimer.c
Go to the documentation of this file.
1
2#include <stddef.h>
3#include <stdbool.h>
4#include <stdint.h>
5
6#include <sys/kassert.h>
7#include <sys/queue.h>
8#include <sys/spinlock.h>
9#include <sys/kmem.h>
10#include <sys/mp.h>
11#include <sys/ktime.h>
12#include <sys/ktimer.h>
13
14#define TIMER_WHEEL_LENGTH 256
15
16static int timerHead = 0;
17static uint64_t timerNow = 0;
18static LIST_HEAD(TimerWheelHead, KTimerEvent) timerSlot[TIMER_WHEEL_LENGTH];
19static Spinlock timerLock;
20static Slab timerSlab;
21
22DEFINE_SLAB(KTimerEvent, &timerSlab);
23
24void
26{
27 int i;
28
29 Spinlock_Init(&timerLock, "KTimer Lock", SPINLOCK_TYPE_NORMAL);
30 Slab_Init(&timerSlab, "KTimerEvent Slab", sizeof(KTimerEvent), 16);
31
32 // Initialize wheel
33 timerHead = 0;
35 for (i = 0; i < TIMER_WHEEL_LENGTH; i++) {
36 LIST_INIT(&timerSlot[i]);
37 }
38}
39
41KTimer_Create(uint64_t timeout, KTimerCB cb, void *arg)
42{
43 int slot;
44 KTimerEvent *evt = KTimerEvent_Alloc();
45
46 evt->refCount = 2; // One for the wheel and one for the callee
47 evt->timeout = timerNow + timeout;
48 evt->cb = cb;
49 evt->arg = arg;
50
51 Spinlock_Lock(&timerLock);
52 slot = (timerHead + timeout + TIMER_WHEEL_LENGTH - 1) % TIMER_WHEEL_LENGTH;
53 // XXX: should insert into tail
54 LIST_INSERT_HEAD(&timerSlot[slot], evt, timerQueue);
55 Spinlock_Unlock(&timerLock);
56
57 return evt;
58}
59
60void
62{
63 ASSERT(evt->refCount != 0);
64 __sync_fetch_and_add(&evt->refCount, 1);
65}
66
67void
69{
70 ASSERT(evt->refCount != 0);
71 if (__sync_fetch_and_sub(&evt->refCount, 1) == 1) {
72 KTimerEvent_Free(evt);
73 }
74}
75
76void
78{
79 Spinlock_Lock(&timerLock);
80
81 LIST_REMOVE(evt, timerQueue);
82 KTimer_Release(evt);
83
84 Spinlock_Unlock(&timerLock);
85}
86
87void
89{
90 uint64_t now;
91
92 if (CPU() != 0) {
93 return;
94 }
95
96 now = KTime_GetEpoch();
97
98 Spinlock_Lock(&timerLock);
99
100 while (now > timerNow) {
101 KTimerEvent *it, *tmp;
102
103 // Dispatch pending timer events
104 LIST_FOREACH_SAFE(it, &timerSlot[timerHead], timerQueue, tmp) {
105 if (it->timeout <= now) {
106 (it->cb)(it->arg);
107 LIST_REMOVE(it, timerQueue);
108 KTimer_Release(it);
109 }
110 }
111
112 // Rotate wheel forward
113 timerNow = timerNow + 1;
115 }
116
117 Spinlock_Unlock(&timerLock);
118}
119
#define CPU
Definition: mp.h:7
#define ASSERT(_x)
Definition: kassert.h:8
#define DEFINE_SLAB(_type, _pool)
Definition: kmem.h:69
void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
UnixEpoch KTime_GetEpoch()
Definition: ktime.c:180
KTimerEvent * KTimer_Create(uint64_t timeout, KTimerCB cb, void *arg)
Definition: ktimer.c:41
static uint64_t timerNow
Definition: ktimer.c:17
void KTimer_Release(KTimerEvent *evt)
Definition: ktimer.c:68
static int timerHead
Definition: ktimer.c:16
void KTimer_Process()
Definition: ktimer.c:88
void KTimer_Cancel(KTimerEvent *evt)
Definition: ktimer.c:77
#define TIMER_WHEEL_LENGTH
Definition: ktimer.c:14
void KTimer_Retain(KTimerEvent *evt)
Definition: ktimer.c:61
void(* KTimerCB)(void *)
Definition: ktimer.h:7
void KTimer_Init()
#define LIST_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:420
#define LIST_INIT(head)
Definition: queue.h:430
#define LIST_REMOVE(elm, field)
Definition: queue.h:465
#define LIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:451
#define LIST_HEAD(name, type)
Definition: queue.h:363
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
uint64_t refCount
Definition: ktimer.h:10
KTimerCB cb
Definition: ktimer.h:12
uint64_t timeout
Definition: ktimer.h:11
void * arg
Definition: ktimer.h:13
Definition: kmem.h:46
unsigned long uint64_t
Definition: types.h:13