Changeset 111b9b9 in mainline for kernel/generic/src/synch/condvar.c


Ignore:
Timestamp:
2023-02-11T19:13:44Z (18 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4777e02
Parents:
76e17d7c
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2022-08-15 17:46:39)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-11 19:13:44)
Message:

Reimplement waitq using thread_wait/wakeup

This adds a few functions to the thread API which can be
summarized as "stop running until woken up by others".
The ordering and context-switching concerns are thus yeeted
to this abstraction and waitq only deals with maintaining
the queues. Overall, this makes the control flow in waitq
much easier to navigate.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/synch/condvar.c

    r76e17d7c r111b9b9  
    5858void condvar_signal(condvar_t *cv)
    5959{
    60         waitq_wakeup(&cv->wq, WAKEUP_FIRST);
     60        waitq_signal(&cv->wq);
    6161}
    6262
     
    6868void condvar_broadcast(condvar_t *cv)
    6969{
    70         waitq_wakeup(&cv->wq, WAKEUP_ALL);
     70        waitq_wake_all(&cv->wq);
    7171}
    7272
     
    8181errno_t condvar_wait_timeout(condvar_t *cv, mutex_t *mtx, uint32_t usec)
    8282{
    83         errno_t rc;
    84         ipl_t ipl;
    85         bool blocked;
     83        wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
    8684
    87         ipl = waitq_sleep_prepare(&cv->wq);
    8885        /* Unlock only after the waitq is locked so we don't miss a wakeup. */
    8986        mutex_unlock(mtx);
    9087
    91         cv->wq.missed_wakeups = 0;      /* Enforce blocking. */
    92         rc = waitq_sleep_timeout_unsafe(&cv->wq, usec, SYNCH_FLAGS_NON_BLOCKING, &blocked);
    93         assert(blocked || rc != EOK);
     88        errno_t rc = waitq_sleep_timeout_unsafe(&cv->wq, usec, SYNCH_FLAGS_NON_BLOCKING, guard);
    9489
    95         waitq_sleep_finish(&cv->wq, blocked, ipl);
    96         /* Lock only after releasing the waitq to avoid a possible deadlock. */
    9790        mutex_lock(mtx);
    98 
    9991        return rc;
    10092}
     
    10294errno_t condvar_wait(condvar_t *cv, mutex_t *mtx)
    10395{
    104         errno_t rc;
    105         ipl_t ipl;
    106         bool blocked;
     96        wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
    10797
    108         ipl = waitq_sleep_prepare(&cv->wq);
    10998        /* Unlock only after the waitq is locked so we don't miss a wakeup. */
    11099        mutex_unlock(mtx);
    111100
    112         cv->wq.missed_wakeups = 0;      /* Enforce blocking. */
    113         rc = waitq_sleep_unsafe(&cv->wq, &blocked);
    114         assert(blocked || rc != EOK);
     101        errno_t rc = waitq_sleep_unsafe(&cv->wq, guard);
    115102
    116         waitq_sleep_finish(&cv->wq, blocked, ipl);
    117         /* Lock only after releasing the waitq to avoid a possible deadlock. */
    118103        mutex_lock(mtx);
    119 
    120104        return rc;
    121105}
     
    142126    uint32_t usec, int flags)
    143127{
    144         errno_t rc;
    145         ipl_t ipl;
    146         bool blocked;
    147 
    148         ipl = waitq_sleep_prepare(&cv->wq);
     128        wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
    149129
    150130        /* Unlock only after the waitq is locked so we don't miss a wakeup. */
    151131        spinlock_unlock(lock);
    152132
    153         cv->wq.missed_wakeups = 0;      /* Enforce blocking. */
    154         rc = waitq_sleep_timeout_unsafe(&cv->wq, usec, flags, &blocked);
    155         assert(blocked || rc != EOK);
     133        errno_t rc = waitq_sleep_timeout_unsafe(&cv->wq, usec, flags, guard);
    156134
    157         waitq_sleep_finish(&cv->wq, blocked, ipl);
    158         /* Lock only after releasing the waitq to avoid a possible deadlock. */
    159135        spinlock_lock(lock);
    160 
    161136        return rc;
    162137}
Note: See TracChangeset for help on using the changeset viewer.