Changeset bd41ac52 in mainline for uspace/lib/c


Ignore:
Timestamp:
2018-08-25T22:21:25Z (7 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cca80a2
Parents:
e2625b1a
Message:

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C11-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C11), struct timespec (C11), usec_t
(HelenOS) and getuptime / getrealtime (HelenOS).

Also attempt to fix the implementation of clock() to return microseconds
(clocks) rather than processor cycles and move it to libc.

Location:
uspace/lib/c
Files:
1 deleted
22 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/async/client.c

    re2625b1a rbd41ac52  
    110110#include <assert.h>
    111111#include <errno.h>
    112 #include <sys/time.h>
     112#include <time.h>
    113113#include <barrier.h>
    114114#include <stdbool.h>
     
    342342 *
    343343 */
    344 errno_t async_wait_timeout(aid_t amsgid, errno_t *retval, suseconds_t timeout)
     344errno_t async_wait_timeout(aid_t amsgid, errno_t *retval, usec_t timeout)
    345345{
    346346        if (amsgid == 0) {
     
    359359                timeout = 0;
    360360
    361         struct timeval expires;
     361        struct timespec expires;
    362362        getuptime(&expires);
    363         tv_add_diff(&expires, timeout);
     363        ts_add_diff(&expires, USEC2NSEC(timeout));
    364364
    365365        errno_t rc = fibril_wait_timeout(&msg->received, &expires);
  • uspace/lib/c/generic/async/ports.c

    re2625b1a rbd41ac52  
    4141#include <assert.h>
    4242#include <errno.h>
    43 #include <sys/time.h>
     43#include <time.h>
    4444#include <barrier.h>
    4545#include <stdbool.h>
  • uspace/lib/c/generic/async/server.c

    re2625b1a rbd41ac52  
    110110#include <assert.h>
    111111#include <errno.h>
    112 #include <sys/time.h>
     112#include <time.h>
    113113#include <stdbool.h>
    114114#include <stdlib.h>
     
    916916 *
    917917 */
    918 bool async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
     918bool async_get_call_timeout(ipc_call_t *call, usec_t usecs)
    919919{
    920920        assert(call);
    921921        assert(fibril_connection);
    922922
    923         struct timeval tv;
    924         struct timeval *expires = NULL;
     923        struct timespec ts;
     924        struct timespec *expires = NULL;
    925925        if (usecs) {
    926                 getuptime(&tv);
    927                 tv_add_diff(&tv, usecs);
    928                 expires = &tv;
     926                getuptime(&ts);
     927                ts_add_diff(&ts, USEC2NSEC(usecs));
     928                expires = &ts;
    929929        }
    930930
  • uspace/lib/c/generic/io/console.c

    re2625b1a rbd41ac52  
    223223
    224224bool console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
    225     suseconds_t *timeout)
    226 {
    227         struct timeval t0;
    228         gettimeofday(&t0, NULL);
     225    usec_t *timeout)
     226{
     227        struct timespec t0;
     228        getuptime(&t0);
    229229
    230230        if (ctrl->input_aid == 0) {
     
    257257
    258258        /* Update timeout */
    259         struct timeval t1;
    260         gettimeofday(&t1, NULL);
    261         *timeout -= tv_sub_diff(&t1, &t0);
     259        struct timespec t1;
     260        getuptime(&t1);
     261        *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
    262262
    263263        return true;
  • uspace/lib/c/generic/private/async.h

    re2625b1a rbd41ac52  
    4040#include <fibril.h>
    4141#include <fibril_synch.h>
    42 #include <sys/time.h>
     42#include <time.h>
    4343#include <stdbool.h>
    4444
  • uspace/lib/c/generic/private/fibril.h

    re2625b1a rbd41ac52  
    8181
    8282extern void fibril_wait_for(fibril_event_t *);
    83 extern errno_t fibril_wait_timeout(fibril_event_t *, const struct timeval *);
     83extern errno_t fibril_wait_timeout(fibril_event_t *, const struct timespec *);
    8484extern void fibril_notify(fibril_event_t *);
    8585
    86 extern errno_t fibril_ipc_wait(ipc_call_t *, const struct timeval *);
     86extern errno_t fibril_ipc_wait(ipc_call_t *, const struct timespec *);
    8787extern void fibril_ipc_poke(void);
    8888
  • uspace/lib/c/generic/private/futex.h

    re2625b1a rbd41ac52  
    102102 *
    103103 */
    104 static inline errno_t futex_down_composable(futex_t *futex, const struct timeval *expires)
     104static inline errno_t futex_down_composable(futex_t *futex,
     105    const struct timespec *expires)
    105106{
    106107        // TODO: Add tests for this.
     
    109110                return EOK;
    110111
    111         suseconds_t timeout;
     112        usec_t timeout;
    112113
    113114        if (!expires) {
     
    119120                        timeout = 1;
    120121                } else {
    121                         struct timeval tv;
     122                        struct timespec tv;
    122123                        getuptime(&tv);
    123                         timeout = tv_gteq(&tv, expires) ? 1 :
    124                             tv_sub_diff(expires, &tv);
     124                        timeout = ts_gteq(&tv, expires) ? 1 :
     125                            NSEC2USEC(ts_sub_diff(expires, &tv));
    125126                }
    126127
     
    148149}
    149150
    150 static inline errno_t futex_down_timeout(futex_t *futex, const struct timeval *expires)
    151 {
    152         if (expires && expires->tv_sec == 0 && expires->tv_usec == 0) {
     151static inline errno_t futex_down_timeout(futex_t *futex,
     152    const struct timespec *expires)
     153{
     154        if (expires && expires->tv_sec == 0 && expires->tv_nsec == 0) {
    153155                /* Nonblocking down. */
    154156
     
    209211         * trydown.
    210212         */
    211         struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
     213        struct timespec tv = { .tv_sec = 0, .tv_nsec = 0 };
    212214        return futex_down_timeout(futex, &tv) == EOK;
    213215}
  • uspace/lib/c/generic/private/thread.h

    re2625b1a rbd41ac52  
    4949extern void thread_detach(thread_id_t);
    5050extern thread_id_t thread_get_id(void);
    51 extern int thread_usleep(useconds_t);
    52 extern unsigned int thread_sleep(unsigned int);
     51extern void thread_usleep(usec_t);
     52extern void thread_sleep(sec_t);
    5353
    5454#endif
  • uspace/lib/c/generic/rndgen.c

    re2625b1a rbd41ac52  
    5252{
    5353        rndgen_t *rndgen;
    54         struct timeval tv;
     54        struct timespec ts;
    5555
    5656        rndgen = calloc(1, sizeof(rndgen_t));
     
    5959
    6060        /* XXX This is a rather poor way of generating random numbers */
    61         gettimeofday(&tv, NULL);
    62         rndgen->seed = tv.tv_sec ^ tv.tv_usec;
     61        getuptime(&ts);
     62        rndgen->seed = ts.tv_sec ^ ts.tv_nsec;
    6363
    6464        *rrndgen = rndgen;
  • uspace/lib/c/generic/stdlib.c

    re2625b1a rbd41ac52  
    3737#include <fibril_synch.h>
    3838#include <stdlib.h>
     39#include <errno.h>
    3940#include "private/libc.h"
    4041#include "private/scanf.h"
  • uspace/lib/c/generic/thread/fibril.c

    re2625b1a rbd41ac52  
    6060typedef struct {
    6161        link_t link;
    62         struct timeval expires;
     62        struct timespec expires;
    6363        fibril_event_t *event;
    6464} _timeout_t;
     
    142142}
    143143
    144 static inline errno_t _ready_down(const struct timeval *expires)
     144static inline errno_t _ready_down(const struct timespec *expires)
    145145{
    146146        if (multithreaded)
     
    253253}
    254254
    255 static errno_t _ipc_wait(ipc_call_t *call, const struct timeval *expires)
     255static errno_t _ipc_wait(ipc_call_t *call, const struct timespec *expires)
    256256{
    257257        if (!expires)
     
    261261                return ipc_wait(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING);
    262262
    263         struct timeval now;
     263        struct timespec now;
    264264        getuptime(&now);
    265265
    266         if (tv_gteq(&now, expires))
     266        if (ts_gteq(&now, expires))
    267267                return ipc_wait(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING);
    268268
    269         return ipc_wait(call, tv_sub_diff(expires, &now), SYNCH_FLAGS_NONE);
     269        return ipc_wait(call, NSEC2USEC(ts_sub_diff(expires, &now)),
     270            SYNCH_FLAGS_NONE);
    270271}
    271272
     
    275276 * wait after new ready fibrils are added.
    276277 */
    277 static fibril_t *_ready_list_pop(const struct timeval *expires, bool locked)
     278static fibril_t *_ready_list_pop(const struct timespec *expires, bool locked)
    278279{
    279280        if (locked) {
     
    370371static fibril_t *_ready_list_pop_nonblocking(bool locked)
    371372{
    372         struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
     373        struct timespec tv = { .tv_sec = 0, .tv_nsec = 0 };
    373374        return _ready_list_pop(&tv, locked);
    374375}
     
    393394
    394395/* Blocks the current fibril until an IPC call arrives. */
    395 static errno_t _wait_ipc(ipc_call_t *call, const struct timeval *expires)
     396static errno_t _wait_ipc(ipc_call_t *call, const struct timespec *expires)
    396397{
    397398        futex_assert_is_not_locked(&fibril_futex);
     
    430431
    431432/** Fire all timeouts that expired. */
    432 static struct timeval *_handle_expired_timeouts(struct timeval *next_timeout)
    433 {
    434         struct timeval tv;
    435         getuptime(&tv);
     433static struct timespec *_handle_expired_timeouts(struct timespec *next_timeout)
     434{
     435        struct timespec ts;
     436        getuptime(&ts);
    436437
    437438        futex_lock(&fibril_futex);
     
    441442                _timeout_t *to = list_get_instance(cur, _timeout_t, link);
    442443
    443                 if (tv_gt(&to->expires, &tv)) {
     444                if (ts_gt(&to->expires, &ts)) {
    444445                        *next_timeout = to->expires;
    445446                        futex_unlock(&fibril_futex);
     
    535536        (void) arg;
    536537
    537         struct timeval next_timeout;
     538        struct timespec next_timeout;
    538539        while (true) {
    539                 struct timeval *to = _handle_expired_timeouts(&next_timeout);
     540                struct timespec *to = _handle_expired_timeouts(&next_timeout);
    540541                fibril_t *f = _ready_list_pop(to, false);
    541542                if (f) {
     
    615616                _timeout_t *cur = list_get_instance(tmp, _timeout_t, link);
    616617
    617                 if (tv_gteq(&cur->expires, &timeout->expires))
     618                if (ts_gteq(&cur->expires, &timeout->expires))
    618619                        break;
    619620
     
    634635 * @return ETIMEOUT if timed out. EOK otherwise.
    635636 */
    636 errno_t fibril_wait_timeout(fibril_event_t *event, const struct timeval *expires)
     637errno_t fibril_wait_timeout(fibril_event_t *event,
     638    const struct timespec *expires)
    637639{
    638640        assert(fibril_self()->rmutex_locks == 0);
     
    889891}
    890892
    891 void fibril_usleep(suseconds_t timeout)
    892 {
    893         struct timeval expires;
     893void fibril_usleep(usec_t timeout)
     894{
     895        struct timespec expires;
    894896        getuptime(&expires);
    895         tv_add_diff(&expires, timeout);
     897        ts_add_diff(&expires, USEC2NSEC(timeout));
    896898
    897899        fibril_event_t event = FIBRIL_EVENT_INIT;
     
    899901}
    900902
    901 void fibril_sleep(unsigned int sec)
    902 {
    903         struct timeval expires;
     903void fibril_sleep(sec_t sec)
     904{
     905        struct timespec expires;
    904906        getuptime(&expires);
    905907        expires.tv_sec += sec;
     
    916918}
    917919
    918 errno_t fibril_ipc_wait(ipc_call_t *call, const struct timeval *expires)
     920errno_t fibril_ipc_wait(ipc_call_t *call, const struct timespec *expires)
    919921{
    920922        return _wait_ipc(call, expires);
  • uspace/lib/c/generic/thread/fibril_synch.c

    re2625b1a rbd41ac52  
    3737#include <async.h>
    3838#include <adt/list.h>
    39 #include <sys/time.h>
     39#include <time.h>
    4040#include <errno.h>
    4141#include <assert.h>
     
    390390errno_t
    391391fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
    392     suseconds_t timeout)
     392    usec_t timeout)
    393393{
    394394        assert(fibril_mutex_is_locked(fm));
     
    400400        wdata.mutex = fm;
    401401
    402         struct timeval tv;
    403         struct timeval *expires = NULL;
     402        struct timespec ts;
     403        struct timespec *expires = NULL;
    404404        if (timeout) {
    405                 getuptime(&tv);
    406                 tv_add_diff(&tv, timeout);
    407                 expires = &tv;
     405                getuptime(&ts);
     406                ts_add_diff(&ts, USEC2NSEC(timeout));
     407                expires = &ts;
    408408        }
    409409
     
    557557 * @param arg           Argument for @a fun
    558558 */
    559 void fibril_timer_set(fibril_timer_t *timer, suseconds_t delay,
     559void fibril_timer_set(fibril_timer_t *timer, usec_t delay,
    560560    fibril_timer_fun_t fun, void *arg)
    561561{
     
    575575 * @param arg           Argument for @a fun
    576576 */
    577 void fibril_timer_set_locked(fibril_timer_t *timer, suseconds_t delay,
     577void fibril_timer_set_locked(fibril_timer_t *timer, usec_t delay,
    578578    fibril_timer_fun_t fun, void *arg)
    579579{
     
    728728}
    729729
    730 errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *sem, suseconds_t timeout)
     730errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *sem, usec_t timeout)
    731731{
    732732        if (timeout < 0)
     
    751751        futex_unlock(&fibril_synch_futex);
    752752
    753         struct timeval tv;
    754         struct timeval *expires = NULL;
     753        struct timespec ts;
     754        struct timespec *expires = NULL;
    755755        if (timeout) {
    756                 getuptime(&tv);
    757                 tv_add_diff(&tv, timeout);
    758                 expires = &tv;
     756                getuptime(&ts);
     757                ts_add_diff(&ts, USEC2NSEC(timeout));
     758                expires = &ts;
    759759        }
    760760
  • uspace/lib/c/generic/thread/mpsc.c

    re2625b1a rbd41ac52  
    146146 * there is no message left in the queue.
    147147 */
    148 errno_t mpsc_receive(mpsc_t *q, void *b, const struct timeval *expires)
     148errno_t mpsc_receive(mpsc_t *q, void *b, const struct timespec *expires)
    149149{
    150150        mpsc_node_t *n;
  • uspace/lib/c/generic/thread/thread.c

    re2625b1a rbd41ac52  
    176176 *
    177177 */
    178 int thread_usleep(useconds_t usec)
     178void thread_usleep(usec_t usec)
    179179{
    180180        (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
    181         return 0;
    182181}
    183182
     
    185184 *
    186185 */
    187 unsigned int thread_sleep(unsigned int sec)
     186void thread_sleep(sec_t sec)
    188187{
    189188        /*
    190          * Sleep in 1000 second steps to support
    191          * full argument range
     189         * Sleep in 1000 second steps to support full argument range
    192190         */
    193 
    194191        while (sec > 0) {
    195192                unsigned int period = (sec > 1000) ? 1000 : sec;
    196193
    197                 thread_usleep(period * 1000000);
     194                thread_usleep(SEC2USEC(period));
    198195                sec -= period;
    199196        }
    200 
    201         return 0;
    202197}
    203198
  • uspace/lib/c/generic/time.c

    re2625b1a rbd41ac52  
    3535 */
    3636
    37 #include <sys/time.h>
    3837#include <time.h>
    3938#include <stdbool.h>
     
    5150#include <loc.h>
    5251#include <device/clock_dev.h>
     52#include <stats.h>
    5353
    5454#define ASCTIME_BUF_LEN  26
     
    5757#define MINS_PER_HOUR  60
    5858#define SECS_PER_MIN   60
    59 #define USECS_PER_SEC  1000000
     59#define NSECS_PER_SEC  1000000000ll
    6060#define MINS_PER_DAY   (MINS_PER_HOUR * HOURS_PER_DAY)
    6161#define SECS_PER_HOUR  (SECS_PER_MIN * MINS_PER_HOUR)
     
    7171static async_sess_t *clock_conn = NULL;
    7272
     73/**
     74 * Get CPU time used since the process invocation.
     75 *
     76 * @return Consumed microseconds by this process or -1 if not available.
     77 */
     78clock_t clock(void)
     79{
     80        static_assert(CLOCKS_PER_SEC == 1000000);
     81
     82        size_t count;
     83        stats_cpu_t *cpu_stats = stats_get_cpus(&count);
     84        if (!cpu_stats)
     85                return (clock_t) -1;
     86        if (!cpu_stats->frequency_mhz) {
     87                free(cpu_stats);
     88                return (clock_t) -1;
     89        }
     90
     91        clock_t total_usecs = -1;
     92        if (cpu_stats) {
     93                stats_task_t *task_stats = stats_get_task(task_get_id());
     94                if (task_stats) {
     95                        total_usecs = (clock_t) (task_stats->kcycles +
     96                            task_stats->ucycles) / cpu_stats->frequency_mhz;
     97                        free(task_stats);
     98                }
     99                free(cpu_stats);
     100        }
     101
     102        return total_usecs;
     103}
     104
    73105/** Check whether the year is a leap year.
    74106 *
     
    252284 *
    253285 * @param tm Broken-down time to normalize.
    254  * @param tv Timeval to add.
     286 * @param ts Timespec to add.
    255287 *
    256288 * @return 0 on success, -1 on overflow
    257289 *
    258290 */
    259 static int normalize_tm_tv(struct tm *tm, const struct timeval *tv)
     291static int normalize_tm_ts(struct tm *tm, const struct timespec *ts)
    260292{
    261293        // TODO: DST correction
    262294
    263295        /* Set initial values. */
    264         time_t usec = tm->tm_usec + tv->tv_usec;
    265         time_t sec = tm->tm_sec + tv->tv_sec;
     296        time_t nsec = tm->tm_nsec + ts->tv_nsec;
     297        time_t sec = tm->tm_sec + ts->tv_sec;
    266298        time_t min = tm->tm_min;
    267299        time_t hour = tm->tm_hour;
     
    271303
    272304        /* Adjust time. */
    273         sec += floor_div(usec, USECS_PER_SEC);
    274         usec = floor_mod(usec, USECS_PER_SEC);
     305        sec += floor_div(nsec, NSECS_PER_SEC);
     306        nsec = floor_mod(nsec, NSECS_PER_SEC);
    275307        min += floor_div(sec, SECS_PER_MIN);
    276308        sec = floor_mod(sec, SECS_PER_MIN);
     
    321353
    322354        /* And put the values back to the struct. */
    323         tm->tm_usec = (int) usec;
     355        tm->tm_nsec = (int) nsec;
    324356        tm->tm_sec = (int) sec;
    325357        tm->tm_min = (int) min;
     
    340372static int normalize_tm_time(struct tm *tm, time_t time)
    341373{
    342         struct timeval tv = {
     374        struct timespec ts = {
    343375                .tv_sec = time,
    344                 .tv_usec = 0
     376                .tv_nsec = 0
    345377        };
    346378
    347         return normalize_tm_tv(tm, &tv);
     379        return normalize_tm_ts(tm, &ts);
    348380}
    349381
     
    456488}
    457489
    458 static void tv_normalize(struct timeval *tv)
    459 {
    460         while (tv->tv_usec > USECS_PER_SEC) {
    461                 tv->tv_sec++;
    462                 tv->tv_usec -= USECS_PER_SEC;
    463         }
    464         while (tv->tv_usec < 0) {
    465                 tv->tv_sec--;
    466                 tv->tv_usec += USECS_PER_SEC;
    467         }
    468 }
    469 
    470 /** Add microseconds to given timeval.
    471  *
    472  * @param tv    Destination timeval.
    473  * @param usecs Number of microseconds to add.
    474  *
    475  */
    476 void tv_add_diff(struct timeval *tv, suseconds_t usecs)
    477 {
    478         tv->tv_sec += usecs / USECS_PER_SEC;
    479         tv->tv_usec += usecs % USECS_PER_SEC;
    480         tv_normalize(tv);
    481 }
    482 
    483 /** Add two timevals.
    484  *
    485  * @param tv1 First timeval.
    486  * @param tv2 Second timeval.
    487  */
    488 void tv_add(struct timeval *tv1, const struct timeval *tv2)
    489 {
    490         tv1->tv_sec += tv2->tv_sec;
    491         tv1->tv_usec += tv2->tv_usec;
    492         tv_normalize(tv1);
    493 }
    494 
    495 /** Subtract two timevals.
    496  *
    497  * @param tv1 First timeval.
    498  * @param tv2 Second timeval.
    499  *
    500  * @return Difference between tv1 and tv2 (tv1 - tv2) in
    501  *         microseconds.
    502  *
    503  */
    504 suseconds_t tv_sub_diff(const struct timeval *tv1, const struct timeval *tv2)
    505 {
    506         return (tv1->tv_usec - tv2->tv_usec) +
    507             ((tv1->tv_sec - tv2->tv_sec) * USECS_PER_SEC);
    508 }
    509 
    510 /** Subtract two timevals.
    511  *
    512  * @param tv1 First timeval.
    513  * @param tv2 Second timeval.
    514  *
    515  */
    516 void tv_sub(struct timeval *tv1, const struct timeval *tv2)
    517 {
    518         tv1->tv_sec -= tv2->tv_sec;
    519         tv1->tv_usec -= tv2->tv_usec;
    520         tv_normalize(tv1);
    521 }
    522 
    523 /** Decide if one timeval is greater than the other.
    524  *
    525  * @param t1 First timeval.
    526  * @param t2 Second timeval.
    527  *
    528  * @return True if tv1 is greater than tv2.
    529  * @return False otherwise.
    530  *
    531  */
    532 int tv_gt(const struct timeval *tv1, const struct timeval *tv2)
    533 {
    534         if (tv1->tv_sec > tv2->tv_sec)
     490static void ts_normalize(struct timespec *ts)
     491{
     492        while (ts->tv_nsec >= NSECS_PER_SEC) {
     493                ts->tv_sec++;
     494                ts->tv_nsec -= NSECS_PER_SEC;
     495        }
     496        while (ts->tv_nsec < 0) {
     497                ts->tv_sec--;
     498                ts->tv_nsec += NSECS_PER_SEC;
     499        }
     500}
     501
     502/** Add nanoseconds to given timespec.
     503 *
     504 * @param ts     Destination timespec.
     505 * @param nsecs  Number of nanoseconds to add.
     506 *
     507 */
     508void ts_add_diff(struct timespec *ts, nsec_t nsecs)
     509{
     510        ts->tv_sec += nsecs / NSECS_PER_SEC;
     511        ts->tv_nsec += nsecs % NSECS_PER_SEC;
     512        ts_normalize(ts);
     513}
     514
     515/** Add two timespecs.
     516 *
     517 * @param ts1  First timespec.
     518 * @param ts2  Second timespec.
     519 */
     520void ts_add(struct timespec *ts1, const struct timespec *ts2)
     521{
     522        ts1->tv_sec += ts2->tv_sec;
     523        ts1->tv_nsec += ts2->tv_nsec;
     524        ts_normalize(ts1);
     525}
     526
     527/** Subtract two timespecs.
     528 *
     529 * @param ts1  First timespec.
     530 * @param ts2  Second timespec.
     531 *
     532 * @return  Difference between ts1 and ts2 (ts1 - ts2) in nanoseconds.
     533 *
     534 */
     535nsec_t ts_sub_diff(const struct timespec *ts1, const struct timespec *ts2)
     536{
     537        return (nsec_t) (ts1->tv_nsec - ts2->tv_nsec) +
     538            SEC2NSEC((ts1->tv_sec - ts2->tv_sec));
     539}
     540
     541/** Subtract two timespecs.
     542 *
     543 * @param ts1  First timespec.
     544 * @param ts2  Second timespec.
     545 *
     546 */
     547void ts_sub(struct timespec *ts1, const struct timespec *ts2)
     548{
     549        ts1->tv_sec -= ts2->tv_sec;
     550        ts1->tv_nsec -= ts2->tv_nsec;
     551        ts_normalize(ts1);
     552}
     553
     554/** Decide if one timespec is greater than the other.
     555 *
     556 * @param ts1  First timespec.
     557 * @param ts2  Second timespec.
     558 *
     559 * @return  True if ts1 is greater than ts2.
     560 * @return  False otherwise.
     561 *
     562 */
     563bool ts_gt(const struct timespec *ts1, const struct timespec *ts2)
     564{
     565        if (ts1->tv_sec > ts2->tv_sec)
    535566                return true;
    536567
    537         if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec > tv2->tv_usec))
     568        if ((ts1->tv_sec == ts2->tv_sec) && (ts1->tv_nsec > ts2->tv_nsec))
    538569                return true;
    539570
     
    541572}
    542573
    543 /** Decide if one timeval is greater than or equal to the other.
    544  *
    545  * @param tv1 First timeval.
    546  * @param tv2 Second timeval.
    547  *
    548  * @return True if tv1 is greater than or equal to tv2.
    549  * @return False otherwise.
    550  *
    551  */
    552 int tv_gteq(const struct timeval *tv1, const struct timeval *tv2)
    553 {
    554         if (tv1->tv_sec > tv2->tv_sec)
     574/** Decide if one timespec is greater than or equal to the other.
     575 *
     576 * @param ts1  First timespec.
     577 * @param ts2  Second timespec.
     578 *
     579 * @return  True if ts1 is greater than or equal to ts2.
     580 * @return  False otherwise.
     581 *
     582 */
     583bool ts_gteq(const struct timespec *ts1, const struct timespec *ts2)
     584{
     585        if (ts1->tv_sec > ts2->tv_sec)
    555586                return true;
    556587
    557         if ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec >= tv2->tv_usec))
     588        if ((ts1->tv_sec == ts2->tv_sec) && (ts1->tv_nsec >= ts2->tv_nsec))
    558589                return true;
    559590
     
    561592}
    562593
    563 /** Get time of day.
    564  *
    565  * The time variables are memory mapped (read-only) from kernel which
    566  * updates them periodically.
    567  *
    568  * As it is impossible to read 2 values atomically, we use a trick:
    569  * First we read the seconds, then we read the microseconds, then we
    570  * read the seconds again. If a second elapsed in the meantime, set
    571  * the microseconds to zero.
    572  *
    573  * This assures that the values returned by two subsequent calls
    574  * to gettimeofday() are monotonous.
    575  *
    576  */
    577 void gettimeofday(struct timeval *tv, struct timezone *tz)
    578 {
    579         if (tz) {
    580                 tz->tz_minuteswest = 0;
    581                 tz->tz_dsttime = DST_NONE;
    582         }
    583 
     594/** Get real time from a RTC service.
     595 *
     596 * @param[out] ts  Timespec to hold time read from the RTC service (if
     597 *                 available). If no such service exists, the returned time
     598 *                 corresponds to system uptime.
     599 */
     600void getrealtime(struct timespec *ts)
     601{
    584602        if (clock_conn == NULL) {
    585603                category_id_t cat_id;
     
    620638                goto fallback;
    621639
    622         tv->tv_usec = time.tm_usec;
    623         tv->tv_sec = mktime(&time);
     640        ts->tv_nsec = time.tm_nsec;
     641        ts->tv_sec = mktime(&time);
    624642
    625643        return;
    626644
    627645fallback:
    628         getuptime(tv);
    629 }
    630 
    631 void getuptime(struct timeval *tv)
     646        getuptime(ts);
     647}
     648
     649/** Get system uptime.
     650 *
     651 * @param[out] ts  Timespec to hold time current uptime.
     652 *
     653 * The time variables are memory mapped (read-only) from kernel which
     654 * updates them periodically.
     655 *
     656 * As it is impossible to read 2 values atomically, we use a trick:
     657 * First we read the seconds, then we read the microseconds, then we
     658 * read the seconds again. If a second elapsed in the meantime, set
     659 * the microseconds to zero.
     660 *
     661 * This assures that the values returned by two subsequent calls
     662 * to getuptime() are monotonous.
     663 *
     664 */
     665void getuptime(struct timespec *ts)
    632666{
    633667        if (ktime == NULL) {
     
    654688
    655689        read_barrier();
    656         tv->tv_usec = ktime->useconds;
     690        ts->tv_nsec = USEC2NSEC(ktime->useconds);
    657691
    658692        read_barrier();
     
    660694
    661695        if (s1 != s2) {
    662                 tv->tv_sec = max(s1, s2);
    663                 tv->tv_usec = 0;
     696                ts->tv_sec = max(s1, s2);
     697                ts->tv_nsec = 0;
    664698        } else
    665                 tv->tv_sec = s1;
     699                ts->tv_sec = s1;
    666700
    667701        return;
    668702
    669703fallback:
    670         tv->tv_sec = 0;
    671         tv->tv_usec = 0;
     704        ts->tv_sec = 0;
     705        ts->tv_nsec = 0;
    672706}
    673707
    674708time_t time(time_t *tloc)
    675709{
    676         struct timeval tv;
    677         gettimeofday(&tv, NULL);
     710        struct timespec ts;
     711        getrealtime(&ts);
    678712
    679713        if (tloc)
    680                 *tloc = tv.tv_sec;
    681 
    682         return tv.tv_sec;
    683 }
    684 
    685 void udelay(useconds_t time)
     714                *tloc = ts.tv_sec;
     715
     716        return ts.tv_sec;
     717}
     718
     719void udelay(sysarg_t time)
    686720{
    687721        (void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
     
    884918                        break;
    885919                case 's':
    886                         APPEND("%ld", secs_since_epoch(tm));
     920                        APPEND("%lld", secs_since_epoch(tm));
    887921                        break;
    888922                case 'S':
     
    961995
    962996        /* Set result to epoch. */
    963         result->tm_usec = 0;
     997        result->tm_nsec = 0;
    964998        result->tm_sec = 0;
    965999        result->tm_min = 0;
     
    10381072 *
    10391073 */
    1040 errno_t time_tv2tm(const struct timeval *tv, struct tm *restrict result)
     1074errno_t time_ts2tm(const struct timespec *ts, struct tm *restrict result)
    10411075{
    10421076        // TODO: Deal with timezones.
     
    10441078
    10451079        /* Set result to epoch. */
    1046         result->tm_usec = 0;
     1080        result->tm_nsec = 0;
    10471081        result->tm_sec = 0;
    10481082        result->tm_min = 0;
     
    10521086        result->tm_year = 70; /* 1970 */
    10531087
    1054         if (normalize_tm_tv(result, tv) == -1)
     1088        if (normalize_tm_ts(result, ts) == -1)
    10551089                return EOVERFLOW;
    10561090
     
    10701104errno_t time_local2tm(const time_t time, struct tm *restrict result)
    10711105{
    1072         struct timeval tv = {
     1106        struct timespec ts = {
    10731107                .tv_sec = time,
    1074                 .tv_usec = 0
     1108                .tv_nsec = 0
    10751109        };
    10761110
    1077         return time_tv2tm(&tv, result);
     1111        return time_ts2tm(&ts, result);
    10781112}
    10791113
  • uspace/lib/c/include/async.h

    re2625b1a rbd41ac52  
    4242#include <ipc/common.h>
    4343#include <fibril.h>
    44 #include <sys/time.h>
     44#include <time.h>
    4545#include <stdbool.h>
    4646#include <abi/proc/task.h>
     
    112112        async_get_call_timeout(data, 0)
    113113
    114 extern bool async_get_call_timeout(ipc_call_t *, suseconds_t);
     114extern bool async_get_call_timeout(ipc_call_t *, usec_t);
    115115
    116116/*
     
    140140
    141141extern void async_wait_for(aid_t, errno_t *);
    142 extern errno_t async_wait_timeout(aid_t, errno_t *, suseconds_t);
     142extern errno_t async_wait_timeout(aid_t, errno_t *, usec_t);
    143143extern void async_forget(aid_t);
    144144
  • uspace/lib/c/include/ddi.h

    re2625b1a rbd41ac52  
    3939#include <stddef.h>
    4040#include <stdint.h>
    41 #include <sys/time.h>
     41#include <time.h>
    4242#include <byteorder.h>
    4343#include <abi/ddi/irq.h>
     
    138138
    139139static inline uint8_t pio_change_8(ioport8_t *reg, uint8_t val, uint8_t mask,
    140     useconds_t delay)
     140    usec_t delay)
    141141{
    142142        uint8_t v = pio_read_8(reg);
     
    147147
    148148static inline uint16_t pio_change_16(ioport16_t *reg, uint16_t val,
    149     uint16_t mask, useconds_t delay)
     149    uint16_t mask, usec_t delay)
    150150{
    151151        uint16_t v = pio_read_16(reg);
     
    156156
    157157static inline uint32_t pio_change_32(ioport32_t *reg, uint32_t val,
    158     uint32_t mask, useconds_t delay)
     158    uint32_t mask, usec_t delay)
    159159{
    160160        uint32_t v = pio_read_32(reg);
     
    165165
    166166static inline uint64_t pio_change_64(ioport64_t *reg, uint64_t val,
    167     uint64_t mask, useconds_t delay)
     167    uint64_t mask, usec_t delay)
    168168{
    169169        uint64_t v = pio_read_64(reg);
     
    173173}
    174174
    175 static inline uint8_t pio_set_8(ioport8_t *r, uint8_t v, useconds_t d)
     175static inline uint8_t pio_set_8(ioport8_t *r, uint8_t v, usec_t d)
    176176{
    177177        return pio_change_8(r, v, 0, d);
    178178}
    179 static inline uint16_t pio_set_16(ioport16_t *r, uint16_t v, useconds_t d)
     179static inline uint16_t pio_set_16(ioport16_t *r, uint16_t v, usec_t d)
    180180{
    181181        return pio_change_16(r, v, 0, d);
    182182}
    183 static inline uint32_t pio_set_32(ioport32_t *r, uint32_t v, useconds_t d)
     183static inline uint32_t pio_set_32(ioport32_t *r, uint32_t v, usec_t d)
    184184{
    185185        return pio_change_32(r, v, 0, d);
    186186}
    187 static inline uint64_t pio_set_64(ioport64_t *r, uint64_t v, useconds_t d)
     187static inline uint64_t pio_set_64(ioport64_t *r, uint64_t v, usec_t d)
    188188{
    189189        return pio_change_64(r, v, 0, d);
    190190}
    191191
    192 static inline uint8_t pio_clear_8(ioport8_t *r, uint8_t v, useconds_t d)
     192static inline uint8_t pio_clear_8(ioport8_t *r, uint8_t v, usec_t d)
    193193{
    194194        return pio_change_8(r, 0, v, d);
    195195}
    196 static inline uint16_t pio_clear_16(ioport16_t *r, uint16_t v, useconds_t d)
     196static inline uint16_t pio_clear_16(ioport16_t *r, uint16_t v, usec_t d)
    197197{
    198198        return pio_change_16(r, 0, v, d);
    199199}
    200 static inline uint32_t pio_clear_32(ioport32_t *r, uint32_t v, useconds_t d)
     200static inline uint32_t pio_clear_32(ioport32_t *r, uint32_t v, usec_t d)
    201201{
    202202        return pio_change_32(r, 0, v, d);
    203203}
    204 static inline uint64_t pio_clear_64(ioport64_t *r, uint64_t v, useconds_t d)
     204static inline uint64_t pio_clear_64(ioport64_t *r, uint64_t v, usec_t d)
    205205{
    206206        return pio_change_64(r, 0, v, d);
  • uspace/lib/c/include/fibril.h

    re2625b1a rbd41ac52  
    6060extern void fibril_yield(void);
    6161
    62 extern void fibril_usleep(suseconds_t);
    63 extern void fibril_sleep(unsigned int);
     62extern void fibril_usleep(usec_t);
     63extern void fibril_sleep(sec_t);
    6464
    6565extern void fibril_enable_multithreaded(void);
  • uspace/lib/c/include/fibril_synch.h

    re2625b1a rbd41ac52  
    3939#include <adt/list.h>
    4040#include <tls.h>
    41 #include <sys/time.h>
     41#include <time.h>
    4242#include <stdbool.h>
    4343
     
    138138        fid_t handler_fid;
    139139
    140         suseconds_t delay;
     140        usec_t delay;
    141141        fibril_timer_fun_t fun;
    142142        void *arg;
     
    181181extern void fibril_condvar_initialize(fibril_condvar_t *);
    182182extern errno_t fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
    183     suseconds_t);
     183    usec_t);
    184184extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
    185185extern void fibril_condvar_signal(fibril_condvar_t *);
     
    188188extern fibril_timer_t *fibril_timer_create(fibril_mutex_t *);
    189189extern void fibril_timer_destroy(fibril_timer_t *);
    190 extern void fibril_timer_set(fibril_timer_t *, suseconds_t, fibril_timer_fun_t,
     190extern void fibril_timer_set(fibril_timer_t *, usec_t, fibril_timer_fun_t,
    191191    void *);
    192 extern void fibril_timer_set_locked(fibril_timer_t *, suseconds_t,
     192extern void fibril_timer_set_locked(fibril_timer_t *, usec_t,
    193193    fibril_timer_fun_t, void *);
    194194extern fibril_timer_state_t fibril_timer_clear(fibril_timer_t *);
     
    198198extern void fibril_semaphore_up(fibril_semaphore_t *);
    199199extern void fibril_semaphore_down(fibril_semaphore_t *);
    200 extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, suseconds_t);
     200extern errno_t fibril_semaphore_down_timeout(fibril_semaphore_t *, usec_t);
    201201extern void fibril_semaphore_close(fibril_semaphore_t *);
    202202
     
    205205extern void mpsc_destroy(mpsc_t *);
    206206extern errno_t mpsc_send(mpsc_t *, const void *);
    207 extern errno_t mpsc_receive(mpsc_t *, void *, const struct timeval *);
     207extern errno_t mpsc_receive(mpsc_t *, void *, const struct timespec *);
    208208extern void mpsc_close(mpsc_t *);
    209209
  • uspace/lib/c/include/io/con_srv.h

    re2625b1a rbd41ac52  
    4545#include <io/style.h>
    4646#include <stdbool.h>
    47 #include <sys/time.h>
     47#include <time.h>
    4848#include <stddef.h>
    4949
     
    5555        void *sarg;
    5656        /** Period to check for abort */
    57         suseconds_t abort_timeout;
     57        usec_t abort_timeout;
    5858        bool aborted;
    5959} con_srvs_t;
  • uspace/lib/c/include/io/console.h

    re2625b1a rbd41ac52  
    3636#define LIBC_IO_CONSOLE_H_
    3737
    38 #include <sys/time.h>
     38#include <time.h>
    3939#include <io/concaps.h>
    4040#include <io/kbd_event.h>
     
    8585extern bool console_get_event(console_ctrl_t *, cons_event_t *);
    8686extern bool console_get_event_timeout(console_ctrl_t *, cons_event_t *,
    87     suseconds_t *);
     87    usec_t *);
    8888
    8989#endif
  • uspace/lib/c/include/time.h

    re2625b1a rbd41ac52  
    11/*
    2  * Copyright (c) 2007 Jakub Jermar
     2 * Copyright (c) 2018 Jakub Jermar
    33 * All rights reserved.
    44 *
     
    4040#endif
    4141
    42 #include <sys/time.h>
    4342
     43/* ISO/IEC 9899:2011 7.27.1 (2) */
     44
     45#include <_bits/NULL.h>
     46
     47#define CLOCKS_PER_SEC  ((clock_t) 1000000)
     48
     49#define TIME_UTC        1
     50
     51
     52/* ISO/IEC 9899:2011 7.27.1 (3) */
     53
     54#include <_bits/size_t.h>
     55
     56/* ISO/IEC 9899:2011 7.27.1 (3), (4) */
     57
     58typedef long long time_t;
     59typedef long long clock_t;
     60
     61struct timespec {
     62        time_t tv_sec;
     63        long tv_nsec;
     64};
     65
     66struct tm {
     67        int tm_sec;
     68        int tm_nsec;
     69        int tm_min;
     70        int tm_hour;
     71        int tm_mday;
     72        int tm_mon;
     73        int tm_year;
     74        int tm_wday;
     75        int tm_yday;
     76        int tm_isdst;
     77};
     78
     79/* ISO/IEC 9899:2011 7.27.2.1 (1) */
     80extern clock_t clock(void);
     81
     82/* ISO/IEC 9899:2011 7.27.2.2 (1) */
     83extern double difftime(time_t, time_t);
     84
     85/* ISO/IEC 9899:2011 7.27.2.3 (1) */
     86extern time_t mktime(struct tm *);
     87
     88/* ISO/IEC 9899:2011 7.27.2.4 (1) */
    4489extern time_t time(time_t *);
     90
     91/* ISO/IEC 9899:2011 7.27.2.5 (1) */
     92extern int timespec_get(struct timespec *, int);
     93
     94/* ISO/IEC 9899:2011 7.27.3.1 (1) */
     95extern char *asctime(const struct tm *);
     96
     97/* ISO/IEC 9899:2011 7.27.3.2 (1) */
     98extern char *ctime(const time_t *);
     99
     100/* ISO/IEC 9899:2011 7.27.3.3 (1) */
     101extern struct tm *gmtime(const time_t *);
     102
     103/* ISO/IEC 9899:2011 7.27.3.4 (1) */
     104extern struct tm *localtime(const time_t *);
     105
     106/* ISO/IEC 9899:2011 7.27.3.5 (1) */
     107extern size_t strftime(char *, size_t, const char *, const struct tm *);
     108
     109/*
     110 * HelenOS specific extensions
     111 */
     112
     113#include <stdbool.h>
     114#include <_bits/errno.h>
     115
     116typedef long long sec_t;
     117typedef long long msec_t;
     118typedef long long usec_t;
     119typedef long long nsec_t;       /* good for +/- 292 years */
     120
     121#define SEC2MSEC(s)     ((s) * 1000ll)
     122#define SEC2USEC(s)     ((s) * 1000000ll)
     123#define SEC2NSEC(s)     ((s) * 1000000000ll)
     124
     125#define MSEC2SEC(ms)    ((ms) / 1000ll)
     126#define MSEC2USEC(ms)   ((ms) * 1000ll)
     127#define MSEC2NSEC(ms)   ((ms) * 1000000ll)
     128
     129#define USEC2SEC(us)    ((us) / 1000000ll)
     130#define USEC2MSEC(us)   ((us) / 1000ll)
     131#define USEC2NSEC(us)   ((us) * 1000ll)
     132
     133#define NSEC2SEC(ns)    ((ns) / 1000000000ll)
     134#define NSEC2MSEC(ns)   ((ns) / 1000000ll)
     135#define NSEC2USEC(ns)   ((ns) / 1000ll)
     136
     137extern void getuptime(struct timespec *);
     138extern void getrealtime(struct timespec *);
     139
     140extern void ts_add_diff(struct timespec *, nsec_t);
     141extern void ts_add(struct timespec *, const struct timespec *);
     142extern void ts_sub(struct timespec *, const struct timespec *);
     143extern nsec_t ts_sub_diff(const struct timespec *, const struct timespec *);
     144extern bool ts_gt(const struct timespec *, const struct timespec *);
     145extern bool ts_gteq(const struct timespec *, const struct timespec *);
     146
     147extern errno_t time_utc2tm(const time_t, struct tm *);
     148extern errno_t time_utc2str(const time_t, char *);
     149extern void time_tm2str(const struct tm *, char *);
     150extern errno_t time_ts2tm(const struct timespec *, struct tm *);
     151extern errno_t time_local2tm(const time_t, struct tm *);
     152extern errno_t time_local2str(const time_t, char *);
     153
     154extern void udelay(sysarg_t);
    45155
    46156#ifdef __cplusplus
Note: See TracChangeset for help on using the changeset viewer.