2
* Copyright (c) 2006 Ondrej Palkovsky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
39
#include <arch/barrier.h>
44
#include <ipc/services.h>
52
/* Pointers to public variables with time */
54
volatile sysarg_t seconds1;
55
volatile sysarg_t useconds;
56
volatile sysarg_t seconds2;
59
/** Add microseconds to given timeval.
61
* @param tv Destination timeval.
62
* @param usecs Number of microseconds to add.
64
void tv_add(struct timeval *tv, suseconds_t usecs)
66
tv->tv_sec += usecs / 1000000;
67
tv->tv_usec += usecs % 1000000;
68
if (tv->tv_usec > 1000000) {
70
tv->tv_usec -= 1000000;
74
/** Subtract two timevals.
76
* @param tv1 First timeval.
77
* @param tv2 Second timeval.
79
* @return Return difference between tv1 and tv2 (tv1 - tv2) in
82
suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
86
result = tv1->tv_usec - tv2->tv_usec;
87
result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
92
/** Decide if one timeval is greater than the other.
94
* @param t1 First timeval.
95
* @param t2 Second timeval.
97
* @return Return true tv1 is greater than tv2. Otherwise return
100
int tv_gt(struct timeval *tv1, struct timeval *tv2)
102
if (tv1->tv_sec > tv2->tv_sec)
104
if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
109
/** Decide if one timeval is greater than or equal to the other.
111
* @param tv1 First timeval.
112
* @param tv2 Second timeval.
114
* @return Return true if tv1 is greater than or equal to tv2.
115
* Otherwise return false.
117
int tv_gteq(struct timeval *tv1, struct timeval *tv2)
119
if (tv1->tv_sec > tv2->tv_sec)
121
if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
127
/** POSIX gettimeofday
129
* The time variables are memory mapped(RO) from kernel, which updates
130
* them periodically. As it is impossible to read 2 values atomically, we
131
* use a trick: First read a seconds, then read microseconds, then
132
* read seconds again. If a second elapsed in the meantime, set it to zero.
133
* This provides assurance, that at least the
134
* sequence of subsequent gettimeofday calls is ordered.
136
int gettimeofday(struct timeval *tv, struct timezone *tz)
144
mapping = as_get_mappable_page(PAGE_SIZE);
145
/* Get the mapping of kernel clock */
146
res = ipc_share_in_start_1_1(PHONE_NS, mapping, PAGE_SIZE,
147
SERVICE_MEM_REALTIME, &rights);
149
printf("Failed to initialize timeofday memarea\n");
152
if (!(rights & AS_AREA_READ)) {
153
printf("Received bad rights on time area: %X\n",
155
as_area_destroy(mapping);
161
tz->tz_minuteswest = 0;
162
tz->tz_dsttime = DST_NONE;
165
s2 = ktime->seconds2;
167
tv->tv_usec = ktime->useconds;
169
s1 = ktime->seconds1;
172
tv->tv_sec = s1 > s2 ? s1 : s2;
179
time_t time(time_t *tloc)
183
if (gettimeofday(&tv, NULL))
190
/** Wait unconditionally for specified number of microseconds */
191
int usleep(unsigned long usec)
193
atomic_t futex = FUTEX_INITIALIZER;
195
futex_initialize(&futex, 0);
196
futex_down_timeout(&futex, usec, 0);
200
/** Wait unconditionally for specified number of seconds */
201
unsigned int sleep(unsigned int seconds)
203
atomic_t futex = FUTEX_INITIALIZER;
205
futex_initialize(&futex, 0);
207
/* Sleep in 1000 second steps to support
208
full argument range */
209
while (seconds > 0) {
210
unsigned int period = (seconds > 1000) ? 1000 : seconds;
212
futex_down_timeout(&futex, period * 1000000, 0);