source: mainline/uspace/lib/posix/source/time.c@ 1165a419

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1165a419 was 9a99aa5, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fibrils need sleep, too.

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Time measurement support.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/time.h"
41
42#include "posix/ctype.h"
43#include "posix/errno.h"
44#include "posix/signal.h"
45#include "posix/assert.h"
46
47#include "libc/fibril_synch.h"
48#include "libc/malloc.h"
49#include "libc/task.h"
50#include "libc/stats.h"
51#include "libc/stdbool.h"
52#include "libc/stddef.h"
53#include "libc/sys/time.h"
54
55// TODO: test everything in this file
56
57/* In some places in this file, phrase "normalized broken-down time" is used.
58 * This means time broken down to components (year, month, day, hour, min, sec),
59 * in which every component is in its proper bounds. Non-normalized time could
60 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
61 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
62 */
63
64int posix_daylight;
65long posix_timezone;
66char *posix_tzname[2];
67
68/**
69 * Set timezone conversion information.
70 */
71void posix_tzset(void)
72{
73 // TODO: read environment
74 posix_tzname[0] = (char *) "GMT";
75 posix_tzname[1] = (char *) "GMT";
76 posix_daylight = 0;
77 posix_timezone = 0;
78}
79
80/**
81 * Get the time in seconds
82 *
83 * @param t If t is non-NULL, the return value is also stored in the memory
84 * pointed to by t.
85 * @return On success, the value of time in seconds since the Epoch
86 * is returned. On error, (time_t)-1 is returned.
87 */
88time_t posix_time(time_t *t)
89{
90 return time(t);
91}
92
93/**
94 * Converts a time value to a broken-down UTC time.
95 *
96 * @param timer Time to convert.
97 * @param result Structure to store the result to.
98 * @return Value of result on success, NULL on overflow.
99 */
100struct tm *posix_gmtime_r(const time_t *restrict timer,
101 struct tm *restrict result)
102{
103 int rc = time_utc2tm(*timer, result);
104 if (rc != EOK) {
105 errno = rc;
106 return NULL;
107 }
108
109 return result;
110}
111
112/**
113 * Converts a time value to a broken-down UTC time.
114 * (non reentrant version)
115 *
116 * @param timep Time to convert
117 * @return Pointer to a statically allocated structure that stores
118 * the result, NULL in case of error.
119 */
120struct tm *posix_gmtime(const time_t *restrict timep)
121{
122 static struct tm result;
123
124 return posix_gmtime_r(timep, &result);
125}
126
127/**
128 * Converts a time value to a broken-down local time.
129 *
130 * @param timer Time to convert.
131 * @param result Structure to store the result to.
132 * @return Value of result on success, NULL on overflow.
133 */
134struct tm *posix_localtime_r(const time_t *restrict timer,
135 struct tm *restrict result)
136{
137 // TODO: deal with timezone
138 // currently assumes system and all times are in GMT
139 return posix_gmtime_r(timer, result);
140}
141
142/**
143 * Converts a time value to a broken-down local time.
144 * (non reentrant version)
145 *
146 * @param timep Time to convert.
147 * @return Pointer to a statically allocated structure that stores
148 * the result, NULL in case of error.
149 */
150struct tm *posix_localtime(const time_t *restrict timep)
151{
152 static struct tm result;
153
154 return posix_localtime_r(timep, &result);
155}
156
157/**
158 * Converts broken-down time to a string in format
159 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
160 *
161 * @param timeptr Broken-down time structure.
162 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
163 * bytes long.
164 * @return Value of buf.
165 */
166char *posix_asctime_r(const struct tm *restrict timeptr,
167 char *restrict buf)
168{
169 time_tm2str(timeptr, buf);
170 return buf;
171}
172
173/**
174 * Convers broken-down time to a string in format
175 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
176 * (non reentrant version)
177 *
178 * @param timeptr Broken-down time structure.
179 * @return Pointer to a statically allocated buffer that stores
180 * the result, NULL in case of error.
181 */
182char *posix_asctime(const struct tm *restrict timeptr)
183{
184 static char buf[ASCTIME_BUF_LEN];
185
186 return posix_asctime_r(timeptr, buf);
187}
188
189/**
190 * Converts the calendar time to a string in format
191 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
192 *
193 * @param timer Time to convert.
194 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
195 * bytes long.
196 * @return Pointer to buf on success, NULL on failure.
197 */
198char *posix_ctime_r(const time_t *timer, char *buf)
199{
200 int r = time_local2str(*timer, buf);
201 if (r != EOK) {
202 errno = r;
203 return NULL;
204 }
205
206 return buf;
207}
208
209/**
210 * Converts the calendar time to a string in format
211 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
212 * (non reentrant version)
213 *
214 * @param timep Time to convert.
215 * @return Pointer to a statically allocated buffer that stores
216 * the result, NULL in case of error.
217 */
218char *posix_ctime(const time_t *timep)
219{
220 static char buf[ASCTIME_BUF_LEN];
221
222 return posix_ctime_r(timep, buf);
223}
224
225/**
226 * Get clock resolution. Only CLOCK_REALTIME is supported.
227 *
228 * @param clock_id Clock ID.
229 * @param res Pointer to the variable where the resolution is to be written.
230 * @return 0 on success, -1 with errno set on failure.
231 */
232int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
233{
234 assert(res != NULL);
235
236 switch (clock_id) {
237 case CLOCK_REALTIME:
238 res->tv_sec = 0;
239 res->tv_nsec = 1000; /* Microsecond resolution. */
240 return 0;
241 default:
242 errno = EINVAL;
243 return -1;
244 }
245}
246
247/**
248 * Get time. Only CLOCK_REALTIME is supported.
249 *
250 * @param clock_id ID of the clock to query.
251 * @param tp Pointer to the variable where the time is to be written.
252 * @return 0 on success, -1 with errno on failure.
253 */
254int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
255{
256 assert(tp != NULL);
257
258 switch (clock_id) {
259 case CLOCK_REALTIME:
260 ;
261 struct timeval tv;
262 gettimeofday(&tv, NULL);
263 tp->tv_sec = tv.tv_sec;
264 tp->tv_nsec = tv.tv_usec * 1000;
265 return 0;
266 default:
267 errno = EINVAL;
268 return -1;
269 }
270}
271
272/**
273 * Set time on a specified clock. As HelenOS doesn't support this yet,
274 * this function always fails.
275 *
276 * @param clock_id ID of the clock to set.
277 * @param tp Time to set.
278 * @return 0 on success, -1 with errno on failure.
279 */
280int posix_clock_settime(posix_clockid_t clock_id,
281 const struct posix_timespec *tp)
282{
283 assert(tp != NULL);
284
285 switch (clock_id) {
286 case CLOCK_REALTIME:
287 // TODO: setting clock
288 // FIXME: HelenOS doesn't actually support hardware
289 // clock yet
290 errno = EPERM;
291 return -1;
292 default:
293 errno = EINVAL;
294 return -1;
295 }
296}
297
298/**
299 * Sleep on a specified clock.
300 *
301 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
302 * @param flags Flags (none supported).
303 * @param rqtp Sleep time.
304 * @param rmtp Remaining time is written here if sleep is interrupted.
305 * @return 0 on success, -1 with errno set on failure.
306 */
307int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
308 const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
309{
310 assert(rqtp != NULL);
311 assert(rmtp != NULL);
312
313 switch (clock_id) {
314 case CLOCK_REALTIME:
315 // TODO: interruptible sleep
316 if (rqtp->tv_sec != 0) {
317 fibril_sleep(rqtp->tv_sec);
318 }
319 if (rqtp->tv_nsec != 0) {
320 fibril_usleep(rqtp->tv_nsec / 1000);
321 }
322 return 0;
323 default:
324 errno = EINVAL;
325 return -1;
326 }
327}
328
329/**
330 * Get CPU time used since the process invocation.
331 *
332 * @return Consumed CPU cycles by this process or -1 if not available.
333 */
334posix_clock_t posix_clock(void)
335{
336 posix_clock_t total_cycles = -1;
337 stats_task_t *task_stats = stats_get_task(task_get_id());
338 if (task_stats) {
339 total_cycles = (posix_clock_t) (task_stats->kcycles +
340 task_stats->ucycles);
341 free(task_stats);
342 task_stats = 0;
343 }
344
345 return total_cycles;
346}
347
348/** @}
349 */
Note: See TracBrowser for help on using the repository browser.