source: mainline/uspace/lib/c/generic/time.c@ 64d2b10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 64d2b10 was 2c577e0b, checked in by Martin Decky <martin@…>, 14 years ago
  • implement service_realtime_share_in()
  • cstyle changes in time.c
  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
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.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#include <sys/time.h>
36#include <unistd.h>
37#include <bool.h>
38#include <ipc/ns.h>
39#include <arch/barrier.h>
40#include <macros.h>
41#include <libc.h>
42#include <time.h>
43
44/** Pointer to kernel shared variables with time */
45struct {
46 volatile sysarg_t seconds1;
47 volatile sysarg_t useconds;
48 volatile sysarg_t seconds2;
49} *ktime = NULL;
50
51/** Add microseconds to given timeval.
52 *
53 * @param tv Destination timeval.
54 * @param usecs Number of microseconds to add.
55 *
56 */
57void tv_add(struct timeval *tv, suseconds_t usecs)
58{
59 tv->tv_sec += usecs / 1000000;
60 tv->tv_usec += usecs % 1000000;
61
62 if (tv->tv_usec > 1000000) {
63 tv->tv_sec++;
64 tv->tv_usec -= 1000000;
65 }
66}
67
68/** Subtract two timevals.
69 *
70 * @param tv1 First timeval.
71 * @param tv2 Second timeval.
72 *
73 * @return Difference between tv1 and tv2 (tv1 - tv2) in
74 * microseconds.
75 *
76 */
77suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
78{
79 return (tv1->tv_usec - tv2->tv_usec) +
80 ((tv1->tv_sec - tv2->tv_sec) * 1000000);
81}
82
83/** Decide if one timeval is greater than the other.
84 *
85 * @param t1 First timeval.
86 * @param t2 Second timeval.
87 *
88 * @return True if tv1 is greater than tv2.
89 * @return False otherwise.
90 *
91 */
92int tv_gt(struct timeval *tv1, struct timeval *tv2)
93{
94 if (tv1->tv_sec > tv2->tv_sec)
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;
101}
102
103/** Decide if one timeval is greater than or equal to the other.
104 *
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.
110 *
111 */
112int tv_gteq(struct timeval *tv1, struct timeval *tv2)
113{
114 if (tv1->tv_sec > tv2->tv_sec)
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;
121}
122
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 *
136 */
137int gettimeofday(struct timeval *tv, struct timezone *tz)
138{
139 if (!ktime) {
140 ktime = service_realtime_share_in();
141 if (!ktime)
142 return -1;
143 }
144
145 if (tz) {
146 tz->tz_minuteswest = 0;
147 tz->tz_dsttime = DST_NONE;
148 }
149
150 sysarg_t s2 = ktime->seconds2;
151
152 read_barrier();
153 tv->tv_usec = ktime->useconds;
154
155 read_barrier();
156 sysarg_t s1 = ktime->seconds1;
157
158 if (s1 != s2) {
159 tv->tv_sec = max(s1, s2);
160 tv->tv_usec = 0;
161 } else
162 tv->tv_sec = s1;
163
164 return 0;
165}
166
167time_t time(time_t *tloc)
168{
169 struct timeval tv;
170 if (gettimeofday(&tv, NULL))
171 return (time_t) -1;
172
173 if (tloc)
174 *tloc = tv.tv_sec;
175
176 return tv.tv_sec;
177}
178
179/** Wait unconditionally for specified number of microseconds
180 *
181 */
182int usleep(useconds_t usec)
183{
184 (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
185 return 0;
186}
187
188/** Wait unconditionally for specified number of seconds
189 *
190 */
191unsigned int sleep(unsigned int sec)
192{
193 /*
194 * Sleep in 1000 second steps to support
195 * full argument range
196 */
197
198 while (sec > 0) {
199 unsigned int period = (sec > 1000) ? 1000 : sec;
200
201 usleep(period * 1000000);
202 sec -= period;
203 }
204
205 return 0;
206}
207
208/** @}
209 */
Note: See TracBrowser for help on using the repository browser.