| 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; |
|---|
| 46 | |
|---|
| 47 | if (ListEmpty(&timerList)) |
|---|
| 48 | ListAdd(&timer->list,&timerList); |
|---|
| 49 | else{ |
|---|
| 50 | ListForEachEntry(curr,&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 | SpinLockIrq(&timerLock); |
|---|
| 94 | |
|---|
| 95 | if (ListEmpty(&timerList)) |
|---|
| 96 | err=-EINVAL; |
|---|
| 97 | else{ |
|---|
| 98 | struct Timer* curr,*curr2; |
|---|
| 99 | ListForEachEntrySafe(curr,curr2,&timerList,list) |
|---|
| 100 | { |
|---|
| 101 | if (curr == timer) |
|---|
| 102 | { |
|---|
| 103 | ListRemove(&curr->list); |
|---|
| 104 | |
|---|
| 105 | if (timer->expires > 0) |
|---|
| 106 | curr2->expires+=curr->expires; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | SpinUnlockIrq(&timerLock); |
|---|
| 112 | |
|---|
| 113 | return err; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | SYMBOL_EXPORT(TimerRemove); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | void UpdateTimers() |
|---|
| 133 | { |
|---|
| 134 | struct Timer* timer; |
|---|
| 135 | |
|---|
| 136 | if (ListEmpty(&timerList)) |
|---|
| 137 | return; |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | timer=ListEntry(timerList.next,struct Timer,list); |
|---|
| 141 | timer->expires-=10; |
|---|
| 142 | |
|---|
| 143 | if (timer->expires <= 0) |
|---|
| 144 | { |
|---|
| 145 | ListRemove(&timer->list); |
|---|
| 146 | timer->func(timer->data); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | static void SleepWakeup(void* data) |
|---|
| 151 | { |
|---|
| 152 | struct Thread* thread=(struct Thread*)data; |
|---|
| 153 | ThrStartThread(thread); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | void Sleep(int milliSeconds) |
|---|
| 157 | { |
|---|
| 158 | struct Timer timer; |
|---|
| 159 | |
|---|
| 160 | timer.func=SleepWakeup; |
|---|
| 161 | timer.expires=milliSeconds; |
|---|
| 162 | timer.data=currThread; |
|---|
| 163 | |
|---|
| 164 | TimerAdd(&timer); |
|---|
| 165 | ThrSuspendThread(currThread); |
|---|
| 166 | ThrSchedule(); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | SYMBOL_EXPORT(Sleep); |
|---|