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>
50 /* Pointers to public variables with time */
52 volatile sysarg_t seconds1
;
53 volatile sysarg_t useconds
;
54 volatile sysarg_t seconds2
;
58 /** POSIX gettimeofday
60 * The time variables are memory mapped(RO) from kernel, which updates
61 * them periodically. As it is impossible to read 2 values atomically, we
62 * use a trick: First read a seconds, then read microseconds, then
63 * read seconds again. If a second elapsed in the meantime, set it to zero.
64 * This provides assurance, that at least the
65 * sequence of subsequent gettimeofday calls is ordered.
67 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
75 mapping
= as_get_mappable_page(PAGE_SIZE
, (int)
76 sysinfo_value("clock.fcolor"));
77 /* Get the mapping of kernel clock */
78 res
= ipc_call_sync_3(PHONE_NS
, IPC_M_AS_AREA_RECV
, (sysarg_t
)
79 mapping
, PAGE_SIZE
, SERVICE_MEM_REALTIME
, NULL
, &rights
,
82 printf("Failed to initialize timeofday memarea\n");
85 if (! (rights
& AS_AREA_READ
)) {
86 printf("Received bad rights on time area: %X\n",
88 as_area_destroy(mapping
);
94 tz
->tz_minuteswest
= 0;
95 tz
->tz_dsttime
= DST_NONE
;
100 tv
->tv_usec
= ktime
->useconds
;
102 s1
= ktime
->seconds1
;
105 tv
->tv_sec
= s1
> s2
? s1
: s2
;
112 /** Wait unconditionally for specified microseconds */
113 void usleep(unsigned long usec
)
115 atomic_t futex
= FUTEX_INITIALIZER
;
117 futex_initialize(&futex
,0);
118 futex_down_timeout(&futex
, usec
, 0);