Subversion Repositories HelenOS-historic

Rev

Rev 1501 | Rev 1596 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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