source: mainline/libc/generic/time.c@ ce5bcb4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ce5bcb4 was 51dbadf3, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Added basic klog.
Added ipc tester.
TODO add serializing functions to psthread, so that the lines in klog won't intermix.

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[0b99e40]1/*
2 * Copyright (C) 2006 Ondrej 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.
27 */
28
[f25b73d6]29#include <sys/time.h>
[0b99e40]30#include <unistd.h>
31#include <ipc/ipc.h>
32#include <stdio.h>
33#include <arch/barrier.h>
[44c6d88d]34#include <unistd.h>
35#include <atomic.h>
36#include <futex.h>
[51dbadf3]37#include <ipc/services.h>
[0b99e40]38
39#include <sysinfo.h>
40#include <as.h>
41#include <ddi.h>
42
43/* Pointers to public variables with time */
44struct {
[2d1fde3b]45 volatile sysarg_t seconds1;
[0b99e40]46 volatile sysarg_t useconds;
[2d1fde3b]47 volatile sysarg_t seconds2;
[0b99e40]48} *ktime = NULL;
49
50
51/** POSIX gettimeofday
52 *
53 * The time variables are memory mapped(RO) from kernel, which updates
54 * them periodically. As it is impossible to read 2 values atomically, we
55 * use a trick: First read a seconds, then read microseconds, then
56 * read seconds again. If a second elapsed in the meantime, read
57 * useconds again. This provides assurance, that at least the
58 * sequence of subsequent gettimeofday calls is ordered.
59 */
60int gettimeofday(struct timeval *tv, struct timezone *tz)
61{
62 void *mapping;
[2d1fde3b]63 sysarg_t s1, s2;
[3f695aad]64 sysarg_t rights;
[0b99e40]65 int res;
66
67 if (!ktime) {
[bf9afa07]68 mapping = as_get_mappable_page(PAGE_SIZE);
[0b99e40]69 /* Get the mapping of kernel clock */
70 res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
[51dbadf3]71 mapping, PAGE_SIZE, SERVICE_MEM_REALTIME,
[3f695aad]72 NULL,&rights,NULL);
[0b99e40]73 if (res) {
74 printf("Failed to initialize timeofday memarea\n");
75 _exit(1);
76 }
[90f5d64]77 if (! (rights & AS_AREA_READ)) {
[3f695aad]78 printf("Received bad rights on time area: %X\n",
79 rights);
[bf9afa07]80 as_area_destroy(mapping);
[3f695aad]81 _exit(1);
82 }
[bf9afa07]83 ktime = mapping;
[0b99e40]84 }
[c042bdd]85 if (tz) {
86 tz->tz_minuteswest = 0;
87 tz->tz_dsttime = DST_NONE;
88 }
89
[5bd03eb]90 s2 = ktime->seconds2;
91 read_barrier();
[0b99e40]92 tv->tv_usec = ktime->useconds;
93 read_barrier();
[5bd03eb]94 s1 = ktime->seconds1;
[2d1fde3b]95 if (s1 != s2) {
96 tv->tv_usec = 0;
97 tv->tv_sec = s1 > s2 ? s1 : s2;
98 } else
99 tv->tv_sec = s1;
[0b99e40]100
101 return 0;
102}
[44c6d88d]103
[eaf34f7]104/** Wait unconditionally for specified microseconds */
[44c6d88d]105void usleep(unsigned long usec)
106{
107 atomic_t futex = FUTEX_INITIALIZER;
108
109 futex_initialize(&futex,0);
110 futex_down_timeout(&futex, usec, 0);
111}
Note: See TracBrowser for help on using the repository browser.