source: mainline/uspace/lib/posix/source/time.c@ c280d7e

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

stdbool.h should not include libarch/types.h

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[2fc5072]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 */
[4cf8ca6]33/** @file Time measurement support.
[2fc5072]34 */
35
[4d10fc8]36#define LIBPOSIX_INTERNAL
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[2fc5072]38
[9b1503e]39#include "internal/common.h"
[a3da2b2]40#include "posix/time.h"
[a6d908c1]41
[a3da2b2]42#include "posix/ctype.h"
43#include "posix/errno.h"
44#include "posix/signal.h"
45#include "posix/assert.h"
[324d46b]46
[9a99aa5]47#include "libc/fibril_synch.h"
[06cb827]48#include "libc/malloc.h"
[a6d908c1]49#include "libc/task.h"
50#include "libc/stats.h"
[582a0b8]51#include "libc/stddef.h"
[3f466c33]52#include "libc/sys/time.h"
[2fc5072]53
[324d46b]54// TODO: test everything in this file
55
[e6165be]56/* In some places in this file, phrase "normalized broken-down time" is used.
57 * This means time broken down to components (year, month, day, hour, min, sec),
58 * in which every component is in its proper bounds. Non-normalized time could
59 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
60 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
61 */
62
[324d46b]63int posix_daylight;
64long posix_timezone;
65char *posix_tzname[2];
66
[55b1efd]67/**
68 * Set timezone conversion information.
[4cf8ca6]69 */
[324d46b]70void posix_tzset(void)
71{
72 // TODO: read environment
73 posix_tzname[0] = (char *) "GMT";
74 posix_tzname[1] = (char *) "GMT";
75 posix_daylight = 0;
76 posix_timezone = 0;
77}
78
[ed29fe4]79/**
80 * Get the time in seconds
81 *
82 * @param t If t is non-NULL, the return value is also stored in the memory
83 * pointed to by t.
84 * @return On success, the value of time in seconds since the Epoch
85 * is returned. On error, (time_t)-1 is returned.
86 */
87time_t posix_time(time_t *t)
88{
89 return time(t);
90}
91
[55b1efd]92/**
93 * Converts a time value to a broken-down UTC time.
[4cf8ca6]94 *
[e6165be]95 * @param timer Time to convert.
96 * @param result Structure to store the result to.
97 * @return Value of result on success, NULL on overflow.
[4cf8ca6]98 */
[f8b6d34c]99struct tm *posix_gmtime_r(const time_t *restrict timer,
100 struct tm *restrict result)
[324d46b]101{
[664fc031]102 int rc = time_utc2tm(*timer, result);
[f7ea5400]103 if (rc != EOK) {
104 errno = rc;
[324d46b]105 return NULL;
106 }
107
108 return result;
[2fc5072]109}
110
[f7ea5400]111/**
112 * Converts a time value to a broken-down UTC time.
113 * (non reentrant version)
114 *
115 * @param timep Time to convert
116 * @return Pointer to a statically allocated structure that stores
117 * the result, NULL in case of error.
118 */
119struct tm *posix_gmtime(const time_t *restrict timep)
120{
121 static struct tm result;
122
123 return posix_gmtime_r(timep, &result);
124}
125
[55b1efd]126/**
127 * Converts a time value to a broken-down local time.
[4cf8ca6]128 *
[e6165be]129 * @param timer Time to convert.
130 * @param result Structure to store the result to.
131 * @return Value of result on success, NULL on overflow.
[4cf8ca6]132 */
[f8b6d34c]133struct tm *posix_localtime_r(const time_t *restrict timer,
134 struct tm *restrict result)
[3f466c33]135{
136 // TODO: deal with timezone
137 // currently assumes system and all times are in GMT
138 return posix_gmtime_r(timer, result);
139}
140
[f7ea5400]141/**
142 * Converts a time value to a broken-down local time.
143 * (non reentrant version)
144 *
145 * @param timep Time to convert.
146 * @return Pointer to a statically allocated structure that stores
147 * the result, NULL in case of error.
148 */
149struct tm *posix_localtime(const time_t *restrict timep)
150{
151 static struct tm result;
152
153 return posix_localtime_r(timep, &result);
154}
155
[55b1efd]156/**
157 * Converts broken-down time to a string in format
158 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
[e6165be]159 *
160 * @param timeptr Broken-down time structure.
161 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
162 * bytes long.
163 * @return Value of buf.
[4cf8ca6]164 */
[f8b6d34c]165char *posix_asctime_r(const struct tm *restrict timeptr,
[324d46b]166 char *restrict buf)
167{
[664fc031]168 time_tm2str(timeptr, buf);
[f7ea5400]169 return buf;
170}
[324d46b]171
[f7ea5400]172/**
173 * Convers broken-down time to a string in format
174 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
175 * (non reentrant version)
176 *
177 * @param timeptr Broken-down time structure.
178 * @return Pointer to a statically allocated buffer that stores
179 * the result, NULL in case of error.
180 */
181char *posix_asctime(const struct tm *restrict timeptr)
182{
183 static char buf[ASCTIME_BUF_LEN];
[324d46b]184
[f7ea5400]185 return posix_asctime_r(timeptr, buf);
[2fc5072]186}
187
[55b1efd]188/**
[f7ea5400]189 * Converts the calendar time to a string in format
190 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
[4cf8ca6]191 *
[e6165be]192 * @param timer Time to convert.
193 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
194 * bytes long.
[f7ea5400]195 * @return Pointer to buf on success, NULL on failure.
[4cf8ca6]196 */
[3f466c33]197char *posix_ctime_r(const time_t *timer, char *buf)
198{
[664fc031]199 int r = time_local2str(*timer, buf);
[f7ea5400]200 if (r != EOK) {
201 errno = r;
[3f466c33]202 return NULL;
203 }
[f7ea5400]204
205 return buf;
206}
207
208/**
209 * Converts the calendar time to a string in format
210 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
211 * (non reentrant version)
212 *
213 * @param timep Time to convert.
214 * @return Pointer to a statically allocated buffer that stores
215 * the result, NULL in case of error.
216 */
217char *posix_ctime(const time_t *timep)
218{
219 static char buf[ASCTIME_BUF_LEN];
220
221 return posix_ctime_r(timep, buf);
[2fc5072]222}
223
[55b1efd]224/**
225 * Get clock resolution. Only CLOCK_REALTIME is supported.
[4cf8ca6]226 *
[e6165be]227 * @param clock_id Clock ID.
228 * @param res Pointer to the variable where the resolution is to be written.
229 * @return 0 on success, -1 with errno set on failure.
[4cf8ca6]230 */
[3f466c33]231int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
232{
233 assert(res != NULL);
234
235 switch (clock_id) {
236 case CLOCK_REALTIME:
237 res->tv_sec = 0;
238 res->tv_nsec = 1000; /* Microsecond resolution. */
239 return 0;
240 default:
241 errno = EINVAL;
242 return -1;
243 }
244}
245
[55b1efd]246/**
247 * Get time. Only CLOCK_REALTIME is supported.
[4cf8ca6]248 *
[e6165be]249 * @param clock_id ID of the clock to query.
250 * @param tp Pointer to the variable where the time is to be written.
[55b1efd]251 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]252 */
[3f466c33]253int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
254{
255 assert(tp != NULL);
256
257 switch (clock_id) {
258 case CLOCK_REALTIME:
259 ;
260 struct timeval tv;
261 gettimeofday(&tv, NULL);
262 tp->tv_sec = tv.tv_sec;
263 tp->tv_nsec = tv.tv_usec * 1000;
264 return 0;
265 default:
266 errno = EINVAL;
267 return -1;
268 }
269}
270
[55b1efd]271/**
272 * Set time on a specified clock. As HelenOS doesn't support this yet,
273 * this function always fails.
[4cf8ca6]274 *
[e6165be]275 * @param clock_id ID of the clock to set.
276 * @param tp Time to set.
277 * @return 0 on success, -1 with errno on failure.
[4cf8ca6]278 */
[3f466c33]279int posix_clock_settime(posix_clockid_t clock_id,
280 const struct posix_timespec *tp)
281{
282 assert(tp != NULL);
283
284 switch (clock_id) {
285 case CLOCK_REALTIME:
286 // TODO: setting clock
287 // FIXME: HelenOS doesn't actually support hardware
288 // clock yet
289 errno = EPERM;
290 return -1;
291 default:
292 errno = EINVAL;
293 return -1;
294 }
295}
296
[55b1efd]297/**
298 * Sleep on a specified clock.
[4cf8ca6]299 *
[e6165be]300 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
301 * @param flags Flags (none supported).
302 * @param rqtp Sleep time.
303 * @param rmtp Remaining time is written here if sleep is interrupted.
304 * @return 0 on success, -1 with errno set on failure.
[4cf8ca6]305 */
[3f466c33]306int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
307 const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
308{
309 assert(rqtp != NULL);
310 assert(rmtp != NULL);
311
312 switch (clock_id) {
313 case CLOCK_REALTIME:
314 // TODO: interruptible sleep
315 if (rqtp->tv_sec != 0) {
[9a99aa5]316 fibril_sleep(rqtp->tv_sec);
[3f466c33]317 }
318 if (rqtp->tv_nsec != 0) {
[9a99aa5]319 fibril_usleep(rqtp->tv_nsec / 1000);
[3f466c33]320 }
321 return 0;
322 default:
323 errno = EINVAL;
324 return -1;
325 }
326}
327
[55b1efd]328/**
329 * Get CPU time used since the process invocation.
[06cb827]330 *
331 * @return Consumed CPU cycles by this process or -1 if not available.
[823a929]332 */
333posix_clock_t posix_clock(void)
334{
[06cb827]335 posix_clock_t total_cycles = -1;
336 stats_task_t *task_stats = stats_get_task(task_get_id());
337 if (task_stats) {
[cb948777]338 total_cycles = (posix_clock_t) (task_stats->kcycles +
339 task_stats->ucycles);
[a12f7f1]340 free(task_stats);
341 task_stats = 0;
[06cb827]342 }
343
344 return total_cycles;
[823a929]345}
346
[2fc5072]347/** @}
348 */
Note: See TracBrowser for help on using the repository browser.