Rev 2678 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1435 | palkovsky | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Ondrej Palkovsky |
1435 | palkovsky | 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. |
||
1653 | cejka | 27 | */ |
28 | |||
1719 | decky | 29 | /** @addtogroup libc |
1653 | cejka | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
1435 | palkovsky | 33 | */ |
34 | |||
1449 | palkovsky | 35 | #include <sys/time.h> |
1435 | palkovsky | 36 | #include <unistd.h> |
37 | #include <ipc/ipc.h> |
||
38 | #include <stdio.h> |
||
39 | #include <arch/barrier.h> |
||
1452 | palkovsky | 40 | #include <unistd.h> |
41 | #include <atomic.h> |
||
42 | #include <futex.h> |
||
2015 | jermar | 43 | #include <sysinfo.h> |
1596 | palkovsky | 44 | #include <ipc/services.h> |
1435 | palkovsky | 45 | |
46 | #include <sysinfo.h> |
||
47 | #include <as.h> |
||
48 | #include <ddi.h> |
||
49 | |||
50 | /* Pointers to public variables with time */ |
||
51 | struct { |
||
1439 | palkovsky | 52 | volatile sysarg_t seconds1; |
1435 | palkovsky | 53 | volatile sysarg_t useconds; |
1439 | palkovsky | 54 | volatile sysarg_t seconds2; |
1435 | palkovsky | 55 | } *ktime = NULL; |
56 | |||
2486 | jermar | 57 | /** Add microseconds to given timeval. |
58 | * |
||
59 | * @param tv Destination timeval. |
||
60 | * @param usecs Number of microseconds to add. |
||
61 | */ |
||
62 | void tv_add(struct timeval *tv, suseconds_t usecs) |
||
63 | { |
||
64 | tv->tv_sec += usecs / 1000000; |
||
65 | tv->tv_usec += usecs % 1000000; |
||
66 | if (tv->tv_usec > 1000000) { |
||
67 | tv->tv_sec++; |
||
68 | tv->tv_usec -= 1000000; |
||
69 | } |
||
70 | } |
||
1435 | palkovsky | 71 | |
2486 | jermar | 72 | /** Subtract two timevals. |
73 | * |
||
74 | * @param tv1 First timeval. |
||
75 | * @param tv2 Second timeval. |
||
76 | * |
||
77 | * @return Return difference between tv1 and tv2 (tv1 - tv2) in |
||
78 | * microseconds. |
||
79 | */ |
||
80 | suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2) |
||
81 | { |
||
82 | suseconds_t result; |
||
83 | |||
84 | result = tv1->tv_usec - tv2->tv_usec; |
||
85 | result += (tv1->tv_sec - tv2->tv_sec) * 1000000; |
||
86 | |||
87 | return result; |
||
88 | } |
||
89 | |||
90 | /** Decide if one timeval is greater than the other. |
||
91 | * |
||
92 | * @param t1 First timeval. |
||
93 | * @param t2 Second timeval. |
||
94 | * |
||
95 | * @return Return true tv1 is greater than tv2. Otherwise return |
||
96 | * false. |
||
97 | */ |
||
98 | int tv_gt(struct timeval *tv1, struct timeval *tv2) |
||
99 | { |
||
100 | if (tv1->tv_sec > tv2->tv_sec) |
||
101 | return 1; |
||
102 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec) |
||
103 | return 1; |
||
104 | return 0; |
||
105 | } |
||
106 | |||
107 | /** Decide if one timeval is greater than or equal to the other. |
||
108 | * |
||
109 | * @param tv1 First timeval. |
||
110 | * @param tv2 Second timeval. |
||
111 | * |
||
112 | * @return Return true if tv1 is greater than or equal to tv2. |
||
113 | * Otherwise return false. |
||
114 | */ |
||
115 | int tv_gteq(struct timeval *tv1, struct timeval *tv2) |
||
116 | { |
||
117 | if (tv1->tv_sec > tv2->tv_sec) |
||
118 | return 1; |
||
119 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec) |
||
120 | return 1; |
||
121 | return 0; |
||
122 | } |
||
123 | |||
124 | |||
1435 | palkovsky | 125 | /** POSIX gettimeofday |
126 | * |
||
127 | * The time variables are memory mapped(RO) from kernel, which updates |
||
128 | * them periodically. As it is impossible to read 2 values atomically, we |
||
129 | * use a trick: First read a seconds, then read microseconds, then |
||
1701 | palkovsky | 130 | * read seconds again. If a second elapsed in the meantime, set it to zero. |
131 | * This provides assurance, that at least the |
||
1435 | palkovsky | 132 | * sequence of subsequent gettimeofday calls is ordered. |
133 | */ |
||
134 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
||
135 | { |
||
136 | void *mapping; |
||
1439 | palkovsky | 137 | sysarg_t s1, s2; |
2677 | jermar | 138 | int rights; |
1435 | palkovsky | 139 | int res; |
140 | |||
141 | if (!ktime) { |
||
2141 | jermar | 142 | mapping = as_get_mappable_page(PAGE_SIZE); |
1435 | palkovsky | 143 | /* Get the mapping of kernel clock */ |
2678 | jermar | 144 | res = ipc_share_in_start_1_1(PHONE_NS, mapping, PAGE_SIZE, |
2677 | jermar | 145 | SERVICE_MEM_REALTIME, &rights); |
1435 | palkovsky | 146 | if (res) { |
147 | printf("Failed to initialize timeofday memarea\n"); |
||
148 | _exit(1); |
||
149 | } |
||
2564 | jermar | 150 | if (!(rights & AS_AREA_READ)) { |
1462 | palkovsky | 151 | printf("Received bad rights on time area: %X\n", |
2564 | jermar | 152 | rights); |
1501 | palkovsky | 153 | as_area_destroy(mapping); |
1462 | palkovsky | 154 | _exit(1); |
155 | } |
||
1501 | palkovsky | 156 | ktime = mapping; |
1435 | palkovsky | 157 | } |
1441 | palkovsky | 158 | if (tz) { |
159 | tz->tz_minuteswest = 0; |
||
160 | tz->tz_dsttime = DST_NONE; |
||
161 | } |
||
162 | |||
1442 | palkovsky | 163 | s2 = ktime->seconds2; |
164 | read_barrier(); |
||
1435 | palkovsky | 165 | tv->tv_usec = ktime->useconds; |
166 | read_barrier(); |
||
1442 | palkovsky | 167 | s1 = ktime->seconds1; |
1439 | palkovsky | 168 | if (s1 != s2) { |
169 | tv->tv_usec = 0; |
||
170 | tv->tv_sec = s1 > s2 ? s1 : s2; |
||
171 | } else |
||
172 | tv->tv_sec = s1; |
||
1435 | palkovsky | 173 | |
174 | return 0; |
||
175 | } |
||
1452 | palkovsky | 176 | |
2564 | jermar | 177 | time_t time(time_t *tloc) |
178 | { |
||
179 | struct timeval tv; |
||
180 | |||
181 | if (gettimeofday(&tv, NULL)) |
||
182 | return (time_t) -1; |
||
183 | if (tloc) |
||
184 | *tloc = tv.tv_sec; |
||
185 | return tv.tv_sec; |
||
186 | } |
||
187 | |||
2188 | decky | 188 | /** Wait unconditionally for specified number of microseconds */ |
2616 | jermar | 189 | int usleep(unsigned long usec) |
1452 | palkovsky | 190 | { |
191 | atomic_t futex = FUTEX_INITIALIZER; |
||
192 | |||
2188 | decky | 193 | futex_initialize(&futex, 0); |
1452 | palkovsky | 194 | futex_down_timeout(&futex, usec, 0); |
2616 | jermar | 195 | return 0; |
1452 | palkovsky | 196 | } |
1653 | cejka | 197 | |
2188 | decky | 198 | /** Wait unconditionally for specified number of seconds */ |
199 | unsigned int sleep(unsigned int seconds) |
||
200 | { |
||
201 | atomic_t futex = FUTEX_INITIALIZER; |
||
202 | |||
203 | futex_initialize(&futex, 0); |
||
204 | |||
205 | /* Sleep in 1000 second steps to support |
||
206 | full argument range */ |
||
207 | while (seconds > 0) { |
||
208 | unsigned int period = (seconds > 1000) ? 1000 : seconds; |
||
209 | |||
210 | futex_down_timeout(&futex, period * 1000000, 0); |
||
211 | seconds -= period; |
||
212 | } |
||
2616 | jermar | 213 | return 0; |
2188 | decky | 214 | } |
215 | |||
1719 | decky | 216 | /** @} |
1653 | cejka | 217 | */ |