| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <timer.h> |
|---|
| 20 | #include <llist.h> |
|---|
| 21 | #include <console.h> |
|---|
| 22 | #include <module.h> |
|---|
| 23 | #include <error.h> |
|---|
| 24 | #include <task.h> |
|---|
| 25 | #include <malloc.h> |
|---|
| 26 | |
|---|
| 27 | LIST_HEAD(timerList); |
|---|
| 28 | Spinlock timerLock; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | void TimerAdd(struct Timer* timer) |
|---|
| 43 | { |
|---|
| 44 | SpinLockIrq(&timerLock); |
|---|
| 45 | struct Timer* curr, *curr2; |
|---|
| 46 | |
|---|
| 47 | if (ListEmpty(&timerList)) |
|---|
| 48 | ListAdd(&timer->list, &timerList); |
|---|
| 49 | else{ |
|---|
| 50 | ListForEachEntrySafe(curr, curr2, &timerList,list) |
|---|
| 51 | { |
|---|
| 52 | if (curr->expires >= timer->expires) |
|---|
| 53 | { |
|---|
| 54 | if (curr->list.prev != &timerList) |
|---|
| 55 | timer->expires-=ListEntry(curr->list.prev,struct Timer,list)->expires; |
|---|
| 56 | |
|---|
| 57 | curr->expires -= timer->expires; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | DoListAdd(&timer->list,curr->list.prev,&curr->list); |
|---|
| 61 | goto out; |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | if (curr->list.prev != &timerList) |
|---|
| 67 | timer->expires-=ListEntry(curr->list.prev,struct Timer,list)->expires; |
|---|
| 68 | |
|---|
| 69 | ListAddTail(&timer->list, &timerList); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | out: |
|---|
| 73 | SpinUnlockIrq(&timerLock); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | SYMBOL_EXPORT(TimerAdd); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | int TimerRemove(struct Timer* timer) |
|---|
| 91 | { |
|---|
| 92 | int err=0; |
|---|
| 93 | |
|---|
| 94 | SpinLockIrq(&timerLock); |
|---|
| 95 | |
|---|
| 96 | if (timer->list.next == NULL || timer->list.prev == NULL) |
|---|
| 97 | goto out; |
|---|
| 98 | |
|---|
| 99 | if (ListEmpty(&timerList)) |
|---|
| 100 | err=-EINVAL; |
|---|
| 101 | else{ |
|---|
| 102 | struct Timer* curr, *curr2; |
|---|
| 103 | |
|---|
| 104 | ListForEachEntrySafe(curr, curr2, &timerList, list) |
|---|
| 105 | { |
|---|
| 106 | if (curr == timer) |
|---|
| 107 | { |
|---|
| 108 | ListRemove(&curr->list); |
|---|
| 109 | timer->list.prev = timer->list.next = NULL; |
|---|
| 110 | |
|---|
| 111 | if (curr->expires > 0 && &curr2->list != &timerList ) |
|---|
| 112 | curr2->expires += curr->expires; |
|---|
| 113 | |
|---|
| 114 | goto out; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | out: |
|---|
| 120 | SpinUnlockIrq(&timerLock); |
|---|
| 121 | |
|---|
| 122 | return err; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | SYMBOL_EXPORT(TimerRemove); |
|---|
| 126 | |
|---|
| 127 | void TimerThreadExit(struct Thread* thread) |
|---|
| 128 | { |
|---|
| 129 | struct Timer* curr, *curr2; |
|---|
| 130 | |
|---|
| 131 | ListForEachEntrySafe(curr, curr2, &timerList, list) |
|---|
| 132 | { |
|---|
| 133 | if (curr->owner == thread) |
|---|
| 134 | TimerRemove(curr); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | void UpdateTimers() |
|---|
| 153 | { |
|---|
| 154 | struct Timer* timer; |
|---|
| 155 | |
|---|
| 156 | if (ListEmpty(&timerList)) |
|---|
| 157 | return; |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | timer=ListEntry(timerList.next, struct Timer, list); |
|---|
| 161 | timer->expires-=10; |
|---|
| 162 | |
|---|
| 163 | if (timer->expires <= 0) |
|---|
| 164 | { |
|---|
| 165 | ListRemove(&timer->list); |
|---|
| 166 | timer->list.prev = timer->list.next = NULL; |
|---|
| 167 | |
|---|
| 168 | timer->func(timer->data); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | void SleepWakeup(void* data) |
|---|
| 173 | { |
|---|
| 174 | struct Thread* thread=(struct Thread*)data; |
|---|
| 175 | |
|---|
| 176 | ThrResumeThread(thread); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | SYMBOL_EXPORT(SleepWakeup); |
|---|
| 180 | |
|---|
| 181 | void Sleep(int milliSeconds) |
|---|
| 182 | { |
|---|
| 183 | struct Timer timer; |
|---|
| 184 | |
|---|
| 185 | timer.func = SleepWakeup; |
|---|
| 186 | timer.expires = milliSeconds; |
|---|
| 187 | timer.data = currThread; |
|---|
| 188 | timer.owner = currThread; |
|---|
| 189 | |
|---|
| 190 | TimerAdd(&timer); |
|---|
| 191 | ThrSuspendThread(currThread); |
|---|
| 192 | ThrSchedule(); |
|---|
| 193 | |
|---|
| 194 | TimerRemove(&timer); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | SYMBOL_EXPORT(Sleep); |
|---|