]> git.buserror.net Git - polintos/scott/priv.git/blob - lib/kernel/time/timer.cc
b495e310218c1a4ff22d8962193bd771d1209432
[polintos/scott/priv.git] / lib / kernel / time / timer.cc
1 // lib/kernel/time/timer.cc -- Generic timer multiplexing
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following conditions:
11 // 
12 //     * Redistributions of source code must retain the above copyright notice,
13 //       this list of conditions and the following disclaimers.
14 // 
15 //     * Redistributions in binary form must reproduce the above copyright notice,
16 //       this list of conditions and the following disclaimers in the
17 //       documentation and/or other materials provided with the distribution.
18 // 
19 //     * The names of the Software's authors and/or contributors
20 //       may not be used to endorse or promote products derived from
21 //       this Software without specific prior written permission.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
29 // SOFTWARE.
30
31 #include <kernel/time.h>
32 #include <lowlevel/clock.h>
33
34 namespace Time {
35         void TimerMux::arm(TimerEntry *entry, Time expiry)
36         {
37                 AutoSpinLockRecIRQ autolock(lock);
38                 
39                 bool was_empty = heap.empty();
40                 Time oldexpiry;
41
42                 if (!was_empty) {
43                         oldexpiry = heap.get_top()->expiry;
44                 } else {
45                         // Shut up the compiler
46                         oldexpiry.seconds = 0;
47                         oldexpiry.nanos = 0;
48                 }
49
50                 entry->expiry = expiry;
51                 
52                 if (entry->is_armed())
53                         heap.requeue(entry);
54                 else
55                         heap.add(entry);
56
57                 Time newexpiry = heap.get_top()->expiry;
58                 if (was_empty || oldexpiry != newexpiry)
59                         parent.arm(newexpiry);
60         }
61
62         void TimerMux::disarm(TimerEntry *entry)
63         {
64                 AutoSpinLockRecIRQ autolock(lock);
65                 
66                 if (entry->is_armed()) {
67                         bool was_top = entry == heap.get_top();
68
69                         heap.del(entry);
70
71                         if (was_top)
72                                 parent.arm(heap.get_top()->expiry);
73                 }
74         }
75         
76         void TimerMux::run()
77         {
78                 DroppableAutoSpinLockRecIRQ autolock(lock);
79                 
80                 while (true) {
81                         TimerEntry *top = heap.get_top();
82                         Time now;
83                         clock.get_time(&now);
84
85                         if (now < top->expiry)
86                                 break;
87
88                         heap.del(top);
89
90                         autolock.unlock();
91                         top->action();
92                         autolock.lock();
93                 }
94                 
95                 if (!heap.empty())
96                         parent.arm(heap.get_top()->expiry);
97         }
98         
99         void TimerEntry::set_action(System::Events::Event *event)
100         {
101                 // FIXME
102         }
103
104         void TimerEntry::action()
105         {
106                 // FIXME
107         }
108 }
109
110 #include _KERNEL_SERVER(time/timer/footer.cc)