Skip to content

Commit

Permalink
aarch64: make synch_thread/synch_port work with weak memory model
Browse files Browse the repository at this point in the history
This patch changes bsd/porting/synch.cc to change _awake variable to an atomic
for similar reasons as the t variable in the waiter. In this case, the synch_port::wakeup*()
methods use "void thread::wake_with(Action action)" and we need to make sure the _awake variable
is visible between CPUs.

Signed-off-by: Waldemar Kozaczuk <[email protected]>
  • Loading branch information
wkozaczuk committed May 28, 2021
1 parent 9054d34 commit 7272f35
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions bsd/porting/synch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TRACEPOINT(trace_synch_wakeup_one_waking, "chan=%p thread=%p", void *, void *);

struct synch_thread {
sched::thread* _thread;
bool _awake;
std::atomic<bool> _awake;
};

class synch_port {
Expand Down Expand Up @@ -69,7 +69,7 @@ int synch_port::_msleep(void *chan, struct mtx *mtx,
// Init the wait
synch_thread wait;
wait._thread = sched::thread::current();
wait._awake = false;
wait._awake.store(false, std::memory_order_release);

if (mtx) {
wait_lock = &mtx->_mutex;
Expand Down Expand Up @@ -100,7 +100,8 @@ int synch_port::_msleep(void *chan, struct mtx *mtx,
{
sched::thread::wait_until_interruptible([&] {
return ( (timo_hz && t.expired()) ||
(wait._awake) );
(wait._awake.load(std::memory_order_acquire)) );

});
}
catch (int e)
Expand All @@ -113,7 +114,7 @@ int synch_port::_msleep(void *chan, struct mtx *mtx,
mutex_lock(wait_lock);
}
// msleep timeout
if (!wait._awake) {
if (!wait._awake.load(std::memory_order_acquire)) {
trace_synch_msleep_expired(chan);
if (chan) {
// A pointer to the local "wait" may still be on the list -
Expand Down Expand Up @@ -146,7 +147,7 @@ void synch_port::wakeup(void* chan)
for (auto it=ppp.first; it!=ppp.second; ++it) {
synch_thread* wait = (*it).second;
trace_synch_wakeup_waking(chan, wait->_thread);
wait->_thread->wake_with([&] { wait->_awake = true; });
wait->_thread->wake_with([&] { wait->_awake.store(true, std::memory_order_release); });
}
_evlist.erase(ppp.first, ppp.second);
mutex_unlock(&_lock);
Expand All @@ -163,7 +164,7 @@ void synch_port::wakeup_one(void* chan)
synch_thread* wait = (*it).second;
_evlist.erase(it);
trace_synch_wakeup_one_waking(chan, wait->_thread);
wait->_thread->wake_with([&] { wait->_awake = true; });
wait->_thread->wake_with([&] { wait->_awake.store(true, std::memory_order_release); });
}
mutex_unlock(&_lock);
}
Expand Down

0 comments on commit 7272f35

Please sign in to comment.