[0b99e40] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[0b99e40] | 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.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[a46da63] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[22e6802] | 33 | */
|
---|
[0b99e40] | 34 |
|
---|
[f25b73d6] | 35 | #include <sys/time.h>
|
---|
[6119f24] | 36 | #include <time.h>
|
---|
[2c577e0b] | 37 | #include <bool.h>
|
---|
[c0699467] | 38 | #include <libarch/barrier.h>
|
---|
[2c577e0b] | 39 | #include <macros.h>
|
---|
[6119f24] | 40 | #include <errno.h>
|
---|
| 41 | #include <sysinfo.h>
|
---|
| 42 | #include <as.h>
|
---|
| 43 | #include <ddi.h>
|
---|
[d9ece1cb] | 44 | #include <libc.h>
|
---|
[d4d74dc] | 45 | #include <unistd.h>
|
---|
[c61d34b] | 46 |
|
---|
[2c577e0b] | 47 | /** Pointer to kernel shared variables with time */
|
---|
[0b99e40] | 48 | struct {
|
---|
[2d1fde3b] | 49 | volatile sysarg_t seconds1;
|
---|
[0b99e40] | 50 | volatile sysarg_t useconds;
|
---|
[2d1fde3b] | 51 | volatile sysarg_t seconds2;
|
---|
[0b99e40] | 52 | } *ktime = NULL;
|
---|
| 53 |
|
---|
[daa90e8] | 54 | /** Add microseconds to given timeval.
|
---|
| 55 | *
|
---|
[2c577e0b] | 56 | * @param tv Destination timeval.
|
---|
| 57 | * @param usecs Number of microseconds to add.
|
---|
| 58 | *
|
---|
[daa90e8] | 59 | */
|
---|
| 60 | void tv_add(struct timeval *tv, suseconds_t usecs)
|
---|
| 61 | {
|
---|
| 62 | tv->tv_sec += usecs / 1000000;
|
---|
| 63 | tv->tv_usec += usecs % 1000000;
|
---|
[2c577e0b] | 64 |
|
---|
[daa90e8] | 65 | if (tv->tv_usec > 1000000) {
|
---|
| 66 | tv->tv_sec++;
|
---|
| 67 | tv->tv_usec -= 1000000;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /** Subtract two timevals.
|
---|
| 72 | *
|
---|
[2c577e0b] | 73 | * @param tv1 First timeval.
|
---|
| 74 | * @param tv2 Second timeval.
|
---|
| 75 | *
|
---|
| 76 | * @return Difference between tv1 and tv2 (tv1 - tv2) in
|
---|
| 77 | * microseconds.
|
---|
[daa90e8] | 78 | *
|
---|
| 79 | */
|
---|
| 80 | suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
|
---|
| 81 | {
|
---|
[2c577e0b] | 82 | return (tv1->tv_usec - tv2->tv_usec) +
|
---|
| 83 | ((tv1->tv_sec - tv2->tv_sec) * 1000000);
|
---|
[daa90e8] | 84 | }
|
---|
| 85 |
|
---|
| 86 | /** Decide if one timeval is greater than the other.
|
---|
| 87 | *
|
---|
[2c577e0b] | 88 | * @param t1 First timeval.
|
---|
| 89 | * @param t2 Second timeval.
|
---|
| 90 | *
|
---|
| 91 | * @return True if tv1 is greater than tv2.
|
---|
| 92 | * @return False otherwise.
|
---|
[daa90e8] | 93 | *
|
---|
| 94 | */
|
---|
| 95 | int tv_gt(struct timeval *tv1, struct timeval *tv2)
|
---|
| 96 | {
|
---|
| 97 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
[2c577e0b] | 98 | return true;
|
---|
| 99 |
|
---|
| 100 | if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec > tv2->tv_usec))
|
---|
| 101 | return true;
|
---|
| 102 |
|
---|
| 103 | return false;
|
---|
[daa90e8] | 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Decide if one timeval is greater than or equal to the other.
|
---|
| 107 | *
|
---|
[2c577e0b] | 108 | * @param tv1 First timeval.
|
---|
| 109 | * @param tv2 Second timeval.
|
---|
| 110 | *
|
---|
| 111 | * @return True if tv1 is greater than or equal to tv2.
|
---|
| 112 | * @return False otherwise.
|
---|
[daa90e8] | 113 | *
|
---|
| 114 | */
|
---|
| 115 | int tv_gteq(struct timeval *tv1, struct timeval *tv2)
|
---|
| 116 | {
|
---|
| 117 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
[2c577e0b] | 118 | return true;
|
---|
| 119 |
|
---|
| 120 | if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec >= tv2->tv_usec))
|
---|
| 121 | return true;
|
---|
| 122 |
|
---|
| 123 | return false;
|
---|
[daa90e8] | 124 | }
|
---|
| 125 |
|
---|
[2c577e0b] | 126 | /** Get time of day
|
---|
| 127 | *
|
---|
| 128 | * The time variables are memory mapped (read-only) from kernel which
|
---|
| 129 | * updates them periodically.
|
---|
| 130 | *
|
---|
| 131 | * As it is impossible to read 2 values atomically, we use a trick:
|
---|
| 132 | * First we read the seconds, then we read the microseconds, then we
|
---|
| 133 | * read the seconds again. If a second elapsed in the meantime, set
|
---|
| 134 | * the microseconds to zero.
|
---|
| 135 | *
|
---|
| 136 | * This assures that the values returned by two subsequent calls
|
---|
| 137 | * to gettimeofday() are monotonous.
|
---|
| 138 | *
|
---|
[0b99e40] | 139 | */
|
---|
| 140 | int gettimeofday(struct timeval *tv, struct timezone *tz)
|
---|
| 141 | {
|
---|
[6119f24] | 142 | if (ktime == NULL) {
|
---|
| 143 | uintptr_t faddr;
|
---|
| 144 | int rc = sysinfo_get_value("clock.faddr", &faddr);
|
---|
| 145 | if (rc != EOK) {
|
---|
| 146 | errno = rc;
|
---|
| 147 | return -1;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[fbcdeb8] | 150 | void *addr;
|
---|
| 151 | rc = physmem_map((void *) faddr, 1,
|
---|
| 152 | AS_AREA_READ | AS_AREA_CACHEABLE, &addr);
|
---|
[6119f24] | 153 | if (rc != EOK) {
|
---|
| 154 | as_area_destroy(addr);
|
---|
| 155 | errno = rc;
|
---|
| 156 | return -1;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | ktime = addr;
|
---|
[0b99e40] | 160 | }
|
---|
[2c577e0b] | 161 |
|
---|
[c042bdd] | 162 | if (tz) {
|
---|
| 163 | tz->tz_minuteswest = 0;
|
---|
| 164 | tz->tz_dsttime = DST_NONE;
|
---|
| 165 | }
|
---|
[2c577e0b] | 166 |
|
---|
| 167 | sysarg_t s2 = ktime->seconds2;
|
---|
| 168 |
|
---|
[5bd03eb] | 169 | read_barrier();
|
---|
[0b99e40] | 170 | tv->tv_usec = ktime->useconds;
|
---|
[2c577e0b] | 171 |
|
---|
[0b99e40] | 172 | read_barrier();
|
---|
[2c577e0b] | 173 | sysarg_t s1 = ktime->seconds1;
|
---|
| 174 |
|
---|
[2d1fde3b] | 175 | if (s1 != s2) {
|
---|
[2c577e0b] | 176 | tv->tv_sec = max(s1, s2);
|
---|
[2d1fde3b] | 177 | tv->tv_usec = 0;
|
---|
| 178 | } else
|
---|
| 179 | tv->tv_sec = s1;
|
---|
[2c577e0b] | 180 |
|
---|
[0b99e40] | 181 | return 0;
|
---|
| 182 | }
|
---|
[44c6d88d] | 183 |
|
---|
[813a703] | 184 | time_t time(time_t *tloc)
|
---|
| 185 | {
|
---|
| 186 | struct timeval tv;
|
---|
| 187 | if (gettimeofday(&tv, NULL))
|
---|
| 188 | return (time_t) -1;
|
---|
[2c577e0b] | 189 |
|
---|
[813a703] | 190 | if (tloc)
|
---|
| 191 | *tloc = tv.tv_sec;
|
---|
[2c577e0b] | 192 |
|
---|
[813a703] | 193 | return tv.tv_sec;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[2c577e0b] | 196 | /** Wait unconditionally for specified number of microseconds
|
---|
| 197 | *
|
---|
| 198 | */
|
---|
[22e6802] | 199 | int usleep(useconds_t usec)
|
---|
[44c6d88d] | 200 | {
|
---|
[22e6802] | 201 | (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
|
---|
[0c09f2b] | 202 | return 0;
|
---|
[44c6d88d] | 203 | }
|
---|
[b2951e2] | 204 |
|
---|
[5fd3f2d] | 205 | void udelay(useconds_t time)
|
---|
| 206 | {
|
---|
| 207 | (void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
[2c577e0b] | 211 | /** Wait unconditionally for specified number of seconds
|
---|
| 212 | *
|
---|
| 213 | */
|
---|
[22e6802] | 214 | unsigned int sleep(unsigned int sec)
|
---|
[dd655970] | 215 | {
|
---|
[2c577e0b] | 216 | /*
|
---|
| 217 | * Sleep in 1000 second steps to support
|
---|
| 218 | * full argument range
|
---|
| 219 | */
|
---|
| 220 |
|
---|
[22e6802] | 221 | while (sec > 0) {
|
---|
| 222 | unsigned int period = (sec > 1000) ? 1000 : sec;
|
---|
[2c577e0b] | 223 |
|
---|
[d9ece1cb] | 224 | usleep(period * 1000000);
|
---|
[22e6802] | 225 | sec -= period;
|
---|
[dd655970] | 226 | }
|
---|
[2c577e0b] | 227 |
|
---|
[0c09f2b] | 228 | return 0;
|
---|
[dd655970] | 229 | }
|
---|
| 230 |
|
---|
[a46da63] | 231 | /** @}
|
---|
[b2951e2] | 232 | */
|
---|