source: mainline/uspace/lib/c/generic/time.c@ d11a181

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d11a181 was fbcdeb8, checked in by Martin Decky <martin@…>, 14 years ago

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

  • SYS_AS_GET_UNMAPPED_AREA was removed
  • dummy implementations of SYS_PHYSMEM_UNMAP and SYS_IOSPACE_DISABLE were added for the sake of symmetry (they do nothing yet)
  • SYS_PHYSMEM_MAP and SYS_DMAMEM_MAP were altered to accept (void *) -1 as address space area base and a lower bound
  • kernel as_area_create() and as_area_share() were altered to accept (void *) -1 as address space area base and a lower bound
  • uspace libraries and programs were altered to reflect the new API
  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[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>
[c61d34b]45
[2c577e0b]46/** Pointer to kernel shared variables with time */
[0b99e40]47struct {
[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 */
59void 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 */
79suseconds_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 */
94int 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 */
114int 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 */
139int 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
[fbcdeb8]149 void *addr;
150 rc = physmem_map((void *) faddr, 1,
151 AS_AREA_READ | AS_AREA_CACHEABLE, &addr);
[6119f24]152 if (rc != EOK) {
153 as_area_destroy(addr);
154 errno = rc;
155 return -1;
156 }
157
158 ktime = addr;
[0b99e40]159 }
[2c577e0b]160
[c042bdd]161 if (tz) {
162 tz->tz_minuteswest = 0;
163 tz->tz_dsttime = DST_NONE;
164 }
[2c577e0b]165
166 sysarg_t s2 = ktime->seconds2;
167
[5bd03eb]168 read_barrier();
[0b99e40]169 tv->tv_usec = ktime->useconds;
[2c577e0b]170
[0b99e40]171 read_barrier();
[2c577e0b]172 sysarg_t s1 = ktime->seconds1;
173
[2d1fde3b]174 if (s1 != s2) {
[2c577e0b]175 tv->tv_sec = max(s1, s2);
[2d1fde3b]176 tv->tv_usec = 0;
177 } else
178 tv->tv_sec = s1;
[2c577e0b]179
[0b99e40]180 return 0;
181}
[44c6d88d]182
[813a703]183time_t time(time_t *tloc)
184{
185 struct timeval tv;
186 if (gettimeofday(&tv, NULL))
187 return (time_t) -1;
[2c577e0b]188
[813a703]189 if (tloc)
190 *tloc = tv.tv_sec;
[2c577e0b]191
[813a703]192 return tv.tv_sec;
193}
194
[2c577e0b]195/** Wait unconditionally for specified number of microseconds
196 *
197 */
[22e6802]198int usleep(useconds_t usec)
[44c6d88d]199{
[22e6802]200 (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
[0c09f2b]201 return 0;
[44c6d88d]202}
[b2951e2]203
[5fd3f2d]204void udelay(useconds_t time)
205{
206 (void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
207}
208
209
[2c577e0b]210/** Wait unconditionally for specified number of seconds
211 *
212 */
[22e6802]213unsigned int sleep(unsigned int sec)
[dd655970]214{
[2c577e0b]215 /*
216 * Sleep in 1000 second steps to support
217 * full argument range
218 */
219
[22e6802]220 while (sec > 0) {
221 unsigned int period = (sec > 1000) ? 1000 : sec;
[2c577e0b]222
[d9ece1cb]223 usleep(period * 1000000);
[22e6802]224 sec -= period;
[dd655970]225 }
[2c577e0b]226
[0c09f2b]227 return 0;
[dd655970]228}
229
[a46da63]230/** @}
[b2951e2]231 */
Note: See TracBrowser for help on using the repository browser.