Subversion Repositories HelenOS

Rev

Rev 2479 | Rev 2564 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2479 Rev 2486
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup libc
29
/** @addtogroup libc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <sys/time.h>
35
#include <sys/time.h>
36
#include <unistd.h>
36
#include <unistd.h>
37
#include <ipc/ipc.h>
37
#include <ipc/ipc.h>
38
#include <stdio.h>
38
#include <stdio.h>
39
#include <arch/barrier.h>
39
#include <arch/barrier.h>
40
#include <unistd.h>
40
#include <unistd.h>
41
#include <atomic.h>
41
#include <atomic.h>
42
#include <futex.h>
42
#include <futex.h>
43
#include <sysinfo.h>
43
#include <sysinfo.h>
44
#include <ipc/services.h>
44
#include <ipc/services.h>
45
 
45
 
46
#include <sysinfo.h>
46
#include <sysinfo.h>
47
#include <as.h>
47
#include <as.h>
48
#include <ddi.h>
48
#include <ddi.h>
49
 
49
 
50
/* Pointers to public variables with time */
50
/* Pointers to public variables with time */
51
struct {
51
struct {
52
    volatile sysarg_t seconds1;
52
    volatile sysarg_t seconds1;
53
    volatile sysarg_t useconds;
53
    volatile sysarg_t useconds;
54
    volatile sysarg_t seconds2;
54
    volatile sysarg_t seconds2;
55
} *ktime = NULL;
55
} *ktime = NULL;
56
 
56
 
-
 
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
}
-
 
71
 
-
 
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
 
57
 
124
 
58
/** POSIX gettimeofday
125
/** POSIX gettimeofday
59
 *
126
 *
60
 * The time variables are memory mapped(RO) from kernel, which updates
127
 * The time variables are memory mapped(RO) from kernel, which updates
61
 * them periodically. As it is impossible to read 2 values atomically, we
128
 * them periodically. As it is impossible to read 2 values atomically, we
62
 * use a trick: First read a seconds, then read microseconds, then
129
 * 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.
130
 * read seconds again. If a second elapsed in the meantime, set it to zero.
64
 * This provides assurance, that at least the
131
 * This provides assurance, that at least the
65
 * sequence of subsequent gettimeofday calls is ordered.
132
 * sequence of subsequent gettimeofday calls is ordered.
66
 */
133
 */
67
int gettimeofday(struct timeval *tv, struct timezone *tz)
134
int gettimeofday(struct timeval *tv, struct timezone *tz)
68
{
135
{
69
    void *mapping;
136
    void *mapping;
70
    sysarg_t s1, s2;
137
    sysarg_t s1, s2;
71
    sysarg_t rights;
138
    sysarg_t rights;
72
    int res;
139
    int res;
73
 
140
 
74
    if (!ktime) {
141
    if (!ktime) {
75
        mapping = as_get_mappable_page(PAGE_SIZE);
142
        mapping = as_get_mappable_page(PAGE_SIZE);
76
        /* Get the mapping of kernel clock */
143
        /* Get the mapping of kernel clock */
77
        res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
144
        res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
78
            (sysarg_t) mapping, PAGE_SIZE, SERVICE_MEM_REALTIME, NULL,
145
            (sysarg_t) mapping, PAGE_SIZE, SERVICE_MEM_REALTIME, NULL,
79
            &rights, NULL);
146
            &rights, NULL);
80
        if (res) {
147
        if (res) {
81
            printf("Failed to initialize timeofday memarea\n");
148
            printf("Failed to initialize timeofday memarea\n");
82
            _exit(1);
149
            _exit(1);
83
        }
150
        }
84
        if (! (rights & AS_AREA_READ)) {
151
        if (! (rights & AS_AREA_READ)) {
85
            printf("Received bad rights on time area: %X\n",
152
            printf("Received bad rights on time area: %X\n",
86
                   rights);
153
                   rights);
87
            as_area_destroy(mapping);
154
            as_area_destroy(mapping);
88
            _exit(1);
155
            _exit(1);
89
        }
156
        }
90
        ktime = mapping;
157
        ktime = mapping;
91
    }
158
    }
92
    if (tz) {
159
    if (tz) {
93
        tz->tz_minuteswest = 0;
160
        tz->tz_minuteswest = 0;
94
        tz->tz_dsttime = DST_NONE;
161
        tz->tz_dsttime = DST_NONE;
95
    }
162
    }
96
 
163
 
97
    s2 = ktime->seconds2;
164
    s2 = ktime->seconds2;
98
    read_barrier();
165
    read_barrier();
99
    tv->tv_usec = ktime->useconds;
166
    tv->tv_usec = ktime->useconds;
100
    read_barrier();
167
    read_barrier();
101
    s1 = ktime->seconds1;
168
    s1 = ktime->seconds1;
102
    if (s1 != s2) {
169
    if (s1 != s2) {
103
        tv->tv_usec = 0;
170
        tv->tv_usec = 0;
104
        tv->tv_sec = s1 > s2 ? s1 : s2;
171
        tv->tv_sec = s1 > s2 ? s1 : s2;
105
    } else
172
    } else
106
        tv->tv_sec = s1;
173
        tv->tv_sec = s1;
107
 
174
 
108
    return 0;
175
    return 0;
109
}
176
}
110
 
177
 
111
/** Wait unconditionally for specified number of microseconds */
178
/** Wait unconditionally for specified number of microseconds */
112
void usleep(unsigned long usec)
179
void usleep(unsigned long usec)
113
{
180
{
114
    atomic_t futex = FUTEX_INITIALIZER;
181
    atomic_t futex = FUTEX_INITIALIZER;
115
 
182
 
116
    futex_initialize(&futex, 0);
183
    futex_initialize(&futex, 0);
117
    futex_down_timeout(&futex, usec, 0);
184
    futex_down_timeout(&futex, usec, 0);
118
}
185
}
119
 
186
 
120
/** Wait unconditionally for specified number of seconds */
187
/** Wait unconditionally for specified number of seconds */
121
unsigned int sleep(unsigned int seconds)
188
unsigned int sleep(unsigned int seconds)
122
{
189
{
123
    atomic_t futex = FUTEX_INITIALIZER;
190
    atomic_t futex = FUTEX_INITIALIZER;
124
 
191
 
125
    futex_initialize(&futex, 0);
192
    futex_initialize(&futex, 0);
126
   
193
   
127
    /* Sleep in 1000 second steps to support
194
    /* Sleep in 1000 second steps to support
128
       full argument range */
195
       full argument range */
129
    while (seconds > 0) {
196
    while (seconds > 0) {
130
        unsigned int period = (seconds > 1000) ? 1000 : seconds;
197
        unsigned int period = (seconds > 1000) ? 1000 : seconds;
131
   
198
   
132
        futex_down_timeout(&futex, period * 1000000, 0);
199
        futex_down_timeout(&futex, period * 1000000, 0);
133
        seconds -= period;
200
        seconds -= period;
134
    }
201
    }
135
}
202
}
136
 
203
 
137
/** @}
204
/** @}
138
 */
205
 */
139
 
206