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