1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_time
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Timeout management functions.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <time/timeout.h>
|
---|
39 | #include <typedefs.h>
|
---|
40 | #include <config.h>
|
---|
41 | #include <panic.h>
|
---|
42 | #include <synch/spinlock.h>
|
---|
43 | #include <halt.h>
|
---|
44 | #include <cpu.h>
|
---|
45 | #include <arch/asm.h>
|
---|
46 | #include <arch.h>
|
---|
47 |
|
---|
48 | /** Initialize timeouts
|
---|
49 | *
|
---|
50 | * Initialize kernel timeouts.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | void timeout_init(void)
|
---|
54 | {
|
---|
55 | irq_spinlock_initialize(&CPU->timeoutlock, "cpu.timeoutlock");
|
---|
56 | list_initialize(&CPU->timeout_active_list);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** Initialize timeout
|
---|
60 | *
|
---|
61 | * Initialize all members including the lock.
|
---|
62 | *
|
---|
63 | * @param timeout Timeout to be initialized.
|
---|
64 | *
|
---|
65 | */
|
---|
66 | void timeout_initialize(timeout_t *timeout)
|
---|
67 | {
|
---|
68 | link_initialize(&timeout->link);
|
---|
69 | timeout->cpu = NULL;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Register timeout
|
---|
73 | *
|
---|
74 | * Insert timeout handler f (with argument arg)
|
---|
75 | * to timeout list and make it execute in
|
---|
76 | * time microseconds (or slightly more).
|
---|
77 | *
|
---|
78 | * @param timeout Timeout structure.
|
---|
79 | * @param time Number of usec in the future to execute the handler.
|
---|
80 | * @param handler Timeout handler function.
|
---|
81 | * @param arg Timeout handler argument.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | void timeout_register(timeout_t *timeout, uint64_t time,
|
---|
85 | timeout_handler_t handler, void *arg)
|
---|
86 | {
|
---|
87 | irq_spinlock_lock(&CPU->timeoutlock, true);
|
---|
88 |
|
---|
89 | timeout->cpu = CPU;
|
---|
90 | timeout->deadline = CPU->current_clock_tick + us2ticks(time);
|
---|
91 | timeout->handler = handler;
|
---|
92 | timeout->arg = arg;
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Insert timeout into the active timeouts list according to timeout->deadline.
|
---|
96 | */
|
---|
97 | timeout_t *target = NULL;
|
---|
98 | link_t *cur, *prev;
|
---|
99 | prev = NULL;
|
---|
100 | for (cur = list_first(&CPU->timeout_active_list);
|
---|
101 | cur != NULL; cur = list_next(cur, &CPU->timeout_active_list)) {
|
---|
102 | target = list_get_instance(cur, timeout_t, link);
|
---|
103 |
|
---|
104 | if (timeout->deadline < target->deadline)
|
---|
105 | break;
|
---|
106 |
|
---|
107 | prev = cur;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (prev == NULL)
|
---|
111 | list_prepend(&timeout->link, &CPU->timeout_active_list);
|
---|
112 | else
|
---|
113 | list_insert_after(&timeout->link, prev);
|
---|
114 |
|
---|
115 | irq_spinlock_unlock(&CPU->timeoutlock, true);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Unregister timeout
|
---|
119 | *
|
---|
120 | * Remove timeout from timeout list.
|
---|
121 | *
|
---|
122 | * @param timeout Timeout to unregister.
|
---|
123 | *
|
---|
124 | * @return True on success, false on failure.
|
---|
125 | *
|
---|
126 | */
|
---|
127 | bool timeout_unregister(timeout_t *timeout)
|
---|
128 | {
|
---|
129 | assert(timeout->cpu);
|
---|
130 |
|
---|
131 | irq_spinlock_lock(&timeout->cpu->timeoutlock, true);
|
---|
132 |
|
---|
133 | bool success = link_in_use(&timeout->link);
|
---|
134 | if (success) {
|
---|
135 | list_remove(&timeout->link);
|
---|
136 | }
|
---|
137 |
|
---|
138 | irq_spinlock_unlock(&timeout->cpu->timeoutlock, true);
|
---|
139 | return success;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** @}
|
---|
143 | */
|
---|