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