Changeset bd41ac52 in mainline for uspace/lib/c/generic/thread


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/generic/thread
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.