2020-11-29 23:05:27 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-11-29 23:05:27 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-29 23:05:27 +00:00
|
|
|
*/
|
|
|
|
|
2021-12-19 21:46:55 +00:00
|
|
|
#include <AK/BuiltinWrappers.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 11:39:11 +00:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-11-29 23:05:27 +00:00
|
|
|
#include <Kernel/Net/Socket.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Scheduler.h>
|
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-02-27 22:56:16 +00:00
|
|
|
Thread::BlockTimeout::BlockTimeout(bool is_absolute, const Time* time, const Time* start_time, clockid_t clock_id)
|
|
|
|
: m_clock_id(clock_id)
|
|
|
|
, m_infinite(!time)
|
|
|
|
{
|
2021-04-26 15:21:15 +00:00
|
|
|
if (m_infinite)
|
|
|
|
return;
|
2021-08-24 10:38:56 +00:00
|
|
|
if (*time > Time::zero())
|
2021-04-26 15:21:15 +00:00
|
|
|
m_time = *time;
|
2021-05-05 16:51:06 +00:00
|
|
|
m_start_time = start_time ? *start_time : TimeManagement::the().current_time(clock_id);
|
2021-04-26 15:21:15 +00:00
|
|
|
if (!is_absolute)
|
|
|
|
m_time += m_start_time;
|
2021-02-27 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
bool Thread::Blocker::add_to_blocker_set(Thread::BlockerSet& blocker_set, void* data)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-22 13:59:47 +00:00
|
|
|
VERIFY(!m_blocker_set);
|
|
|
|
if (blocker_set.add_blocker(*this, data)) {
|
|
|
|
m_blocker_set = &blocker_set;
|
2020-11-29 23:05:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread::Blocker::~Blocker()
|
|
|
|
{
|
2021-08-22 23:45:05 +00:00
|
|
|
VERIFY(!m_lock.is_locked());
|
2021-08-22 13:59:47 +00:00
|
|
|
if (m_blocker_set)
|
2021-08-23 22:34:11 +00:00
|
|
|
m_blocker_set->remove_blocker(*this);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 10:14:14 +00:00
|
|
|
bool Thread::Blocker::setup_blocker()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
void Thread::Blocker::begin_blocking(Badge<Thread>)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_is_blocking);
|
2020-11-29 23:05:27 +00:00
|
|
|
m_is_blocking = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Thread::Blocker::end_blocking(Badge<Thread>, bool did_timeout) -> BlockResult
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
// if m_is_blocking is false here, some thread forced to
|
|
|
|
// unblock us when we get here. This is only called from the
|
|
|
|
// thread that was blocked.
|
2021-08-23 23:01:50 +00:00
|
|
|
VERIFY(Thread::current() == m_thread);
|
2020-11-29 23:05:27 +00:00
|
|
|
m_is_blocking = false;
|
|
|
|
|
|
|
|
was_unblocked(did_timeout);
|
|
|
|
return block_result();
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
Thread::JoinBlocker::JoinBlocker(Thread& joinee, ErrorOr<void>& try_join_result, void*& joinee_exit_value)
|
2020-11-29 23:05:27 +00:00
|
|
|
: m_joinee(joinee)
|
|
|
|
, m_joinee_exit_value(joinee_exit_value)
|
2021-08-24 10:14:14 +00:00
|
|
|
, m_try_join_result(try_join_result)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-24 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::JoinBlocker::setup_blocker()
|
|
|
|
{
|
|
|
|
// We need to hold our lock to avoid a race where try_join succeeds
|
|
|
|
// but the joinee is joining immediately
|
|
|
|
SpinlockLocker lock(m_lock);
|
2021-08-24 11:11:58 +00:00
|
|
|
bool should_block = true;
|
2021-08-24 10:14:14 +00:00
|
|
|
m_try_join_result = m_joinee->try_join([&]() {
|
|
|
|
if (!add_to_blocker_set(m_joinee->m_join_blocker_set))
|
2021-08-24 11:11:58 +00:00
|
|
|
should_block = false;
|
2021-08-24 10:14:14 +00:00
|
|
|
});
|
2021-08-24 11:11:58 +00:00
|
|
|
if (m_try_join_result.is_error())
|
|
|
|
return false;
|
|
|
|
return should_block;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 00:09:08 +00:00
|
|
|
void Thread::JoinBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
// If we should have blocked but got here it must have been that the
|
2021-08-22 13:59:47 +00:00
|
|
|
// timeout was already in the past. So we need to ask the BlockerSet
|
2020-11-29 23:05:27 +00:00
|
|
|
// to supply us the information. We cannot hold the lock as unblock
|
2021-08-22 13:59:47 +00:00
|
|
|
// could be called by the BlockerSet at any time!
|
2021-08-24 11:11:58 +00:00
|
|
|
if (reason == UnblockImmediatelyReason::TimeoutInThePast) {
|
|
|
|
m_joinee->m_join_blocker_set.try_unblock(*this);
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::JoinBlocker::unblock(void* value, bool from_add_blocker)
|
|
|
|
{
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
m_did_unblock = true;
|
|
|
|
m_joinee_exit_value = value;
|
|
|
|
do_set_interrupted_by_death();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!from_add_blocker)
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-22 22:10:33 +00:00
|
|
|
Thread::WaitQueueBlocker::WaitQueueBlocker(WaitQueue& wait_queue, StringView block_reason)
|
2021-08-24 10:14:14 +00:00
|
|
|
: m_wait_queue(wait_queue)
|
|
|
|
, m_block_reason(block_reason)
|
2020-12-08 04:29:41 +00:00
|
|
|
{
|
2021-08-24 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::WaitQueueBlocker::setup_blocker()
|
|
|
|
{
|
2021-08-24 11:11:58 +00:00
|
|
|
return add_to_blocker_set(m_wait_queue);
|
2020-12-08 04:29:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 22:10:33 +00:00
|
|
|
Thread::WaitQueueBlocker::~WaitQueueBlocker()
|
2020-12-08 04:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-22 22:10:33 +00:00
|
|
|
bool Thread::WaitQueueBlocker::unblock()
|
2020-12-08 04:29:41 +00:00
|
|
|
{
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-12-08 04:29:41 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
m_did_unblock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
Thread::FutexBlocker::FutexBlocker(FutexQueue& futex_queue, u32 bitset)
|
2021-08-24 10:14:14 +00:00
|
|
|
: m_futex_queue(futex_queue)
|
|
|
|
, m_bitset(bitset)
|
2020-12-22 06:21:58 +00:00
|
|
|
{
|
2021-08-24 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::FutexBlocker::setup_blocker()
|
|
|
|
{
|
2021-08-24 11:11:58 +00:00
|
|
|
return add_to_blocker_set(m_futex_queue);
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread::FutexBlocker::~FutexBlocker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::FutexBlocker::finish_requeue(FutexQueue& futex_queue)
|
|
|
|
{
|
2021-08-29 18:10:24 +00:00
|
|
|
VERIFY(m_lock.is_locked_by_current_processor());
|
2021-08-22 13:59:47 +00:00
|
|
|
set_blocker_set_raw_locked(&futex_queue);
|
2021-01-22 16:42:36 +00:00
|
|
|
// We can now release the lock
|
2020-12-22 06:21:58 +00:00
|
|
|
m_lock.unlock(m_relock_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::FutexBlocker::unblock_bitset(u32 bitset)
|
|
|
|
{
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-12-22 06:21:58 +00:00
|
|
|
if (m_did_unblock || (bitset != FUTEX_BITSET_MATCH_ANY && (m_bitset & bitset) == 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_did_unblock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::FutexBlocker::unblock(bool force)
|
|
|
|
{
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-12-22 06:21:58 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return force;
|
|
|
|
m_did_unblock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
Thread::OpenFileDescriptionBlocker::OpenFileDescriptionBlocker(OpenFileDescription& description, BlockFlags flags, BlockFlags& unblocked_flags)
|
2020-11-29 23:05:27 +00:00
|
|
|
: m_blocked_description(description)
|
|
|
|
, m_flags(flags)
|
|
|
|
, m_unblocked_flags(unblocked_flags)
|
2021-08-24 10:14:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
bool Thread::OpenFileDescriptionBlocker::setup_blocker()
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
m_unblocked_flags = BlockFlags::None;
|
2021-08-24 11:11:58 +00:00
|
|
|
return add_to_blocker_set(m_blocked_description->blocker_set());
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
bool Thread::OpenFileDescriptionBlocker::unblock_if_conditions_are_met(bool from_add_blocker, void*)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
auto unblock_flags = m_blocked_description->should_unblock(m_flags);
|
|
|
|
if (unblock_flags == BlockFlags::None)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
m_did_unblock = true;
|
|
|
|
m_unblocked_flags = unblock_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!from_add_blocker)
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
void Thread::OpenFileDescriptionBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-24 11:11:58 +00:00
|
|
|
if (reason == UnblockImmediatelyReason::UnblockConditionAlreadyMet)
|
2020-11-29 23:05:27 +00:00
|
|
|
return;
|
2021-08-24 11:11:58 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
// If we should have blocked but got here it must have been that the
|
2021-08-22 13:59:47 +00:00
|
|
|
// timeout was already in the past. So we need to ask the BlockerSet
|
2020-11-29 23:05:27 +00:00
|
|
|
// to supply us the information. We cannot hold the lock as unblock
|
2021-08-22 13:59:47 +00:00
|
|
|
// could be called by the BlockerSet at any time!
|
2021-08-23 00:09:08 +00:00
|
|
|
VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast);
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2021-09-04 22:12:49 +00:00
|
|
|
// Just call unblock_if_conditions_are_met here because we will query the file description
|
2021-08-22 13:59:47 +00:00
|
|
|
// for the data and don't need any input from the FileBlockerSet.
|
|
|
|
// However, it's possible that if timeout_in_past is true then FileBlockerSet
|
2020-11-29 23:05:27 +00:00
|
|
|
// may call us at any given time, so our call to unblock here may fail.
|
|
|
|
// Either way, unblock will be called at least once, which provides
|
|
|
|
// all the data we need.
|
2021-09-04 22:12:49 +00:00
|
|
|
unblock_if_conditions_are_met(false, nullptr);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
const OpenFileDescription& Thread::OpenFileDescriptionBlocker::blocked_description() const
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
return m_blocked_description;
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
Thread::AcceptBlocker::AcceptBlocker(OpenFileDescription& description, BlockFlags& unblocked_flags)
|
|
|
|
: OpenFileDescriptionBlocker(description, BlockFlags::Accept | BlockFlags::Exception, unblocked_flags)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
Thread::ConnectBlocker::ConnectBlocker(OpenFileDescription& description, BlockFlags& unblocked_flags)
|
|
|
|
: OpenFileDescriptionBlocker(description, BlockFlags::Connect | BlockFlags::Exception, unblocked_flags)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
Thread::WriteBlocker::WriteBlocker(OpenFileDescription& description, BlockFlags& unblocked_flags)
|
|
|
|
: OpenFileDescriptionBlocker(description, BlockFlags::Write | BlockFlags::Exception, unblocked_flags)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Thread::WriteBlocker::override_timeout(const BlockTimeout& timeout) -> const BlockTimeout&
|
|
|
|
{
|
2021-12-29 00:01:27 +00:00
|
|
|
auto const& description = blocked_description();
|
2020-11-29 23:05:27 +00:00
|
|
|
if (description.is_socket()) {
|
2021-12-29 00:01:27 +00:00
|
|
|
auto const& socket = *description.socket();
|
2020-11-29 23:05:27 +00:00
|
|
|
if (socket.has_send_timeout()) {
|
2021-02-28 01:48:45 +00:00
|
|
|
Time send_timeout = socket.send_timeout();
|
2021-02-27 22:56:16 +00:00
|
|
|
m_timeout = BlockTimeout(false, &send_timeout, timeout.start_time(), timeout.clock_id());
|
2020-11-29 23:05:27 +00:00
|
|
|
if (timeout.is_infinite() || (!m_timeout.is_infinite() && m_timeout.absolute_time() < timeout.absolute_time()))
|
|
|
|
return m_timeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
Thread::ReadBlocker::ReadBlocker(OpenFileDescription& description, BlockFlags& unblocked_flags)
|
|
|
|
: OpenFileDescriptionBlocker(description, BlockFlags::Read | BlockFlags::Exception, unblocked_flags)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Thread::ReadBlocker::override_timeout(const BlockTimeout& timeout) -> const BlockTimeout&
|
|
|
|
{
|
2021-12-29 00:01:27 +00:00
|
|
|
auto const& description = blocked_description();
|
2020-11-29 23:05:27 +00:00
|
|
|
if (description.is_socket()) {
|
2021-12-29 00:01:27 +00:00
|
|
|
auto const& socket = *description.socket();
|
2020-11-29 23:05:27 +00:00
|
|
|
if (socket.has_receive_timeout()) {
|
2021-02-28 01:48:45 +00:00
|
|
|
Time receive_timeout = socket.receive_timeout();
|
2021-02-27 22:56:16 +00:00
|
|
|
m_timeout = BlockTimeout(false, &receive_timeout, timeout.start_time(), timeout.clock_id());
|
2020-11-29 23:05:27 +00:00
|
|
|
if (timeout.is_infinite() || (!m_timeout.is_infinite() && m_timeout.absolute_time() < timeout.absolute_time()))
|
|
|
|
return m_timeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2021-02-27 22:56:16 +00:00
|
|
|
Thread::SleepBlocker::SleepBlocker(const BlockTimeout& deadline, Time* remaining)
|
2020-11-29 23:05:27 +00:00
|
|
|
: m_deadline(deadline)
|
|
|
|
, m_remaining(remaining)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Thread::SleepBlocker::override_timeout(const BlockTimeout& timeout) -> const BlockTimeout&
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(timeout.is_infinite()); // A timeout should not be provided
|
2020-11-29 23:05:27 +00:00
|
|
|
// To simplify things only use the sleep deadline.
|
|
|
|
return m_deadline;
|
|
|
|
}
|
|
|
|
|
2021-08-23 00:09:08 +00:00
|
|
|
void Thread::SleepBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
// SleepBlocker::should_block should always return true, so timeout
|
|
|
|
// in the past is the only valid case when this function is called
|
2021-08-23 00:09:08 +00:00
|
|
|
VERIFY(reason == UnblockImmediatelyReason::TimeoutInThePast);
|
2020-11-29 23:05:27 +00:00
|
|
|
calculate_remaining();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::SleepBlocker::was_unblocked(bool did_timeout)
|
|
|
|
{
|
|
|
|
Blocker::was_unblocked(did_timeout);
|
|
|
|
|
|
|
|
calculate_remaining();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::SleepBlocker::calculate_remaining()
|
|
|
|
{
|
|
|
|
if (!m_remaining)
|
|
|
|
return;
|
2021-05-05 16:51:06 +00:00
|
|
|
auto time_now = TimeManagement::the().current_time(m_deadline.clock_id());
|
2020-11-29 23:05:27 +00:00
|
|
|
if (time_now < m_deadline.absolute_time())
|
2021-02-27 22:56:16 +00:00
|
|
|
*m_remaining = m_deadline.absolute_time() - time_now;
|
2020-11-29 23:05:27 +00:00
|
|
|
else
|
|
|
|
*m_remaining = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread::BlockResult Thread::SleepBlocker::block_result()
|
|
|
|
{
|
|
|
|
auto result = Blocker::block_result();
|
|
|
|
if (result == Thread::BlockResult::InterruptedByTimeout)
|
|
|
|
return Thread::BlockResult::WokeNormally;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread::SelectBlocker::SelectBlocker(FDVector& fds)
|
|
|
|
: m_fds(fds)
|
2021-08-24 10:14:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::SelectBlocker::setup_blocker()
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-24 11:11:58 +00:00
|
|
|
bool should_block = true;
|
2020-11-29 23:05:27 +00:00
|
|
|
for (auto& fd_entry : m_fds) {
|
|
|
|
fd_entry.unblocked_flags = FileBlocker::BlockFlags::None;
|
|
|
|
|
2021-08-24 11:11:58 +00:00
|
|
|
if (!should_block)
|
2020-11-29 23:05:27 +00:00
|
|
|
continue;
|
2021-08-22 13:59:47 +00:00
|
|
|
if (!fd_entry.description->blocker_set().add_blocker(*this, &fd_entry))
|
2021-08-24 11:11:58 +00:00
|
|
|
should_block = false;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
2021-08-24 11:11:58 +00:00
|
|
|
return should_block;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread::SelectBlocker::~SelectBlocker()
|
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
for (auto& fd_entry : m_fds)
|
2021-08-23 22:34:11 +00:00
|
|
|
fd_entry.description->blocker_set().remove_blocker(*this);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 00:09:08 +00:00
|
|
|
void Thread::SelectBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason reason)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-08-24 11:11:58 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return;
|
|
|
|
m_did_unblock = true;
|
|
|
|
if (reason == UnblockImmediatelyReason::UnblockConditionAlreadyMet) {
|
|
|
|
auto count = collect_unblocked_flags();
|
|
|
|
VERIFY(count > 0);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 22:12:49 +00:00
|
|
|
bool Thread::SelectBlocker::unblock_if_conditions_are_met(bool from_add_blocker, void* data)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(data); // data is a pointer to an entry in the m_fds vector
|
2020-11-29 23:05:27 +00:00
|
|
|
auto& fd_info = *static_cast<FDInfo*>(data);
|
|
|
|
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto unblock_flags = fd_info.description->should_unblock(fd_info.block_flags);
|
|
|
|
if (unblock_flags == BlockFlags::None)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_did_unblock = true;
|
|
|
|
|
|
|
|
// We need to store unblock_flags here, otherwise someone else
|
|
|
|
// affecting this file descriptor could change the information
|
|
|
|
// between now and when was_unblocked is called!
|
|
|
|
fd_info.unblocked_flags = unblock_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only do this once for the first one
|
|
|
|
if (!from_add_blocker)
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Thread::SelectBlocker::collect_unblocked_flags()
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
for (auto& fd_entry : m_fds) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd_entry.block_flags != FileBlocker::BlockFlags::None);
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
// unblock will have set at least the first descriptor's unblock
|
|
|
|
// flags that triggered the unblock. Make sure we don't discard that
|
|
|
|
// information as it may have changed by now!
|
|
|
|
if (fd_entry.unblocked_flags == FileBlocker::BlockFlags::None)
|
|
|
|
fd_entry.unblocked_flags = fd_entry.description->should_unblock(fd_entry.block_flags);
|
|
|
|
|
|
|
|
if (fd_entry.unblocked_flags != FileBlocker::BlockFlags::None)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::SelectBlocker::was_unblocked(bool did_timeout)
|
|
|
|
{
|
|
|
|
Blocker::was_unblocked(did_timeout);
|
|
|
|
if (!did_timeout && !was_interrupted()) {
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_did_unblock);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
size_t count = collect_unblocked_flags();
|
|
|
|
// If we were blocked and didn't time out, we should have at least one unblocked fd!
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(count > 0);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:01:42 +00:00
|
|
|
Thread::SignalBlocker::SignalBlocker(sigset_t pending_set, siginfo_t& result)
|
|
|
|
: m_pending_set(pending_set)
|
|
|
|
, m_result(result)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::SignalBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason unblock_immediately_reason)
|
|
|
|
{
|
|
|
|
if (unblock_immediately_reason != UnblockImmediatelyReason::TimeoutInThePast)
|
|
|
|
return;
|
|
|
|
// If the specified timeout is 0 the caller is simply trying to poll once for pending signals,
|
|
|
|
// so simply calling check_pending_signals should populate the requested information.
|
|
|
|
check_pending_signals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::SignalBlocker::setup_blocker()
|
|
|
|
{
|
|
|
|
return add_to_blocker_set(thread().m_signal_blocker_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::SignalBlocker::check_pending_signals(bool from_add_blocker)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SpinlockLocker lock(m_lock);
|
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
|
2021-12-19 21:46:55 +00:00
|
|
|
auto matching_pending_signal = bit_scan_forward(thread().pending_signals() & m_pending_set);
|
2021-12-11 23:01:42 +00:00
|
|
|
if (matching_pending_signal == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_did_unblock = true;
|
|
|
|
m_result = {};
|
|
|
|
m_result.si_signo = matching_pending_signal;
|
|
|
|
m_result.si_code = 0; // FIXME: How can we determine this?
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!from_add_blocker)
|
|
|
|
unblock_from_blocker();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
Thread::WaitBlockerSet::ProcessBlockInfo::ProcessBlockInfo(NonnullRefPtr<Process>&& process, WaitBlocker::UnblockFlags flags, u8 signal)
|
2020-12-09 04:18:45 +00:00
|
|
|
: process(move(process))
|
|
|
|
, flags(flags)
|
|
|
|
, signal(signal)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
Thread::WaitBlockerSet::ProcessBlockInfo::~ProcessBlockInfo()
|
2020-12-09 04:18:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
void Thread::WaitBlockerSet::try_unblock(Thread::WaitBlocker& blocker)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
// We if we have any processes pending
|
2020-12-09 04:18:45 +00:00
|
|
|
for (size_t i = 0; i < m_processes.size(); i++) {
|
|
|
|
auto& info = m_processes[i];
|
2020-11-29 23:05:27 +00:00
|
|
|
// We need to call unblock as if we were called from add_blocker
|
|
|
|
// so that we don't trigger a context switch by yielding!
|
|
|
|
if (info.was_waited && blocker.is_wait())
|
|
|
|
continue; // This state was already waited on, do not unblock
|
2020-12-09 04:18:45 +00:00
|
|
|
if (blocker.unblock(info.process, info.flags, info.signal, true)) {
|
2020-11-29 23:05:27 +00:00
|
|
|
if (blocker.is_wait()) {
|
2020-12-09 04:18:45 +00:00
|
|
|
if (info.flags == Thread::WaitBlocker::UnblockFlags::Terminated) {
|
|
|
|
m_processes.remove(i);
|
2021-08-22 13:59:47 +00:00
|
|
|
dbgln_if(WAITBLOCK_DEBUG, "WaitBlockerSet[{}] terminated, remove {}", m_process, *info.process);
|
2020-12-09 04:18:45 +00:00
|
|
|
} else {
|
2021-08-22 13:59:47 +00:00
|
|
|
dbgln_if(WAITBLOCK_DEBUG, "WaitBlockerSet[{}] terminated, mark as waited {}", m_process, *info.process);
|
2020-11-29 23:05:27 +00:00
|
|
|
info.was_waited = true;
|
2020-12-09 04:18:45 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
void Thread::WaitBlockerSet::disowned_by_waiter(Process& process)
|
2020-12-09 02:04:05 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-12-09 02:04:05 +00:00
|
|
|
if (m_finalized)
|
|
|
|
return;
|
2020-12-09 04:18:45 +00:00
|
|
|
for (size_t i = 0; i < m_processes.size();) {
|
|
|
|
auto& info = m_processes[i];
|
|
|
|
if (info.process == &process) {
|
2021-08-22 15:38:16 +00:00
|
|
|
unblock_all_blockers_whose_conditions_are_met_locked([&](Blocker& b, void*, bool&) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Blocker::Type::Wait);
|
2020-12-09 02:04:05 +00:00
|
|
|
auto& blocker = static_cast<WaitBlocker&>(b);
|
2020-12-09 04:18:45 +00:00
|
|
|
bool did_unblock = blocker.unblock(info.process, WaitBlocker::UnblockFlags::Disowned, 0, false);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(did_unblock); // disowning must unblock everyone
|
2020-12-09 02:04:05 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-08-22 13:59:47 +00:00
|
|
|
dbgln_if(WAITBLOCK_DEBUG, "WaitBlockerSet[{}] disowned {}", m_process, *info.process);
|
2020-12-09 04:18:45 +00:00
|
|
|
m_processes.remove(i);
|
2020-12-09 02:04:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
bool Thread::WaitBlockerSet::unblock(Process& process, WaitBlocker::UnblockFlags flags, u8 signal)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(flags != WaitBlocker::UnblockFlags::Disowned);
|
2020-12-09 02:04:05 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
bool did_unblock_any = false;
|
|
|
|
bool did_wait = false;
|
|
|
|
bool was_waited_already = false;
|
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_finalized)
|
|
|
|
return false;
|
|
|
|
if (flags != WaitBlocker::UnblockFlags::Terminated) {
|
|
|
|
// First check if this state was already waited on
|
2020-12-09 04:18:45 +00:00
|
|
|
for (auto& info : m_processes) {
|
|
|
|
if (info.process == &process) {
|
2020-11-29 23:05:27 +00:00
|
|
|
was_waited_already = info.was_waited;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 15:38:16 +00:00
|
|
|
unblock_all_blockers_whose_conditions_are_met_locked([&](Blocker& b, void*, bool&) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Blocker::Type::Wait);
|
2020-11-29 23:05:27 +00:00
|
|
|
auto& blocker = static_cast<WaitBlocker&>(b);
|
|
|
|
if (was_waited_already && blocker.is_wait())
|
|
|
|
return false; // This state was already waited on, do not unblock
|
2020-12-09 04:18:45 +00:00
|
|
|
if (blocker.unblock(process, flags, signal, false)) {
|
2020-11-29 23:05:27 +00:00
|
|
|
did_wait |= blocker.is_wait(); // anyone requesting a wait
|
|
|
|
did_unblock_any = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If no one has waited (yet), or this wasn't a wait, or if it's anything other than
|
|
|
|
// UnblockFlags::Terminated then add it to your list
|
|
|
|
if (!did_unblock_any || !did_wait || flags != WaitBlocker::UnblockFlags::Terminated) {
|
|
|
|
bool updated_existing = false;
|
2020-12-09 04:18:45 +00:00
|
|
|
for (auto& info : m_processes) {
|
|
|
|
if (info.process == &process) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(info.flags != WaitBlocker::UnblockFlags::Terminated);
|
2020-11-29 23:05:27 +00:00
|
|
|
info.flags = flags;
|
|
|
|
info.signal = signal;
|
|
|
|
info.was_waited = did_wait;
|
2021-08-22 13:59:47 +00:00
|
|
|
dbgln_if(WAITBLOCK_DEBUG, "WaitBlockerSet[{}] update {} flags={}, waited={}", m_process, process, (int)flags, info.was_waited);
|
2020-11-29 23:05:27 +00:00
|
|
|
updated_existing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 04:18:45 +00:00
|
|
|
if (!updated_existing) {
|
2021-08-22 13:59:47 +00:00
|
|
|
dbgln_if(WAITBLOCK_DEBUG, "WaitBlockerSet[{}] add {} flags: {}", m_process, process, (int)flags);
|
2020-12-09 04:18:45 +00:00
|
|
|
m_processes.append(ProcessBlockInfo(process, flags, signal));
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
return did_unblock_any;
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
bool Thread::WaitBlockerSet::should_add_blocker(Blocker& b, void*)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
// NOTE: m_lock is held already!
|
|
|
|
if (m_finalized)
|
|
|
|
return false;
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Blocker::Type::Wait);
|
2020-11-29 23:05:27 +00:00
|
|
|
auto& blocker = static_cast<WaitBlocker&>(b);
|
|
|
|
// See if we can match any process immediately
|
2020-12-09 04:18:45 +00:00
|
|
|
for (size_t i = 0; i < m_processes.size(); i++) {
|
|
|
|
auto& info = m_processes[i];
|
|
|
|
if (blocker.unblock(info.process, info.flags, info.signal, true)) {
|
2020-11-29 23:05:27 +00:00
|
|
|
// Only remove the entry if UnblockFlags::Terminated
|
|
|
|
if (info.flags == Thread::WaitBlocker::UnblockFlags::Terminated && blocker.is_wait())
|
2020-12-09 04:18:45 +00:00
|
|
|
m_processes.remove(i);
|
2020-11-29 23:05:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:59:47 +00:00
|
|
|
void Thread::WaitBlockerSet::finalize()
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_finalized);
|
2020-11-29 23:05:27 +00:00
|
|
|
m_finalized = true;
|
|
|
|
|
|
|
|
// Clear the list of threads here so we can drop the references to them
|
2020-12-09 04:18:45 +00:00
|
|
|
m_processes.clear();
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2021-07-14 19:09:37 +00:00
|
|
|
// NOTE: Kernel processes don't have a leaked ref on them.
|
2021-07-16 13:17:32 +00:00
|
|
|
if (!m_process.is_kernel_process()) {
|
2021-07-14 19:09:37 +00:00
|
|
|
// No more waiters, drop the last reference immediately. This may
|
|
|
|
// cause us to be destructed ourselves!
|
|
|
|
VERIFY(m_process.ref_count() > 0);
|
|
|
|
m_process.unref();
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
Thread::WaitBlocker::WaitBlocker(int wait_options, Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, ErrorOr<siginfo_t>& result)
|
2020-11-29 23:05:27 +00:00
|
|
|
: m_wait_options(wait_options)
|
|
|
|
, m_result(result)
|
2021-08-23 04:01:04 +00:00
|
|
|
, m_waitee(move(waitee))
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-24 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Thread::WaitBlocker::setup_blocker()
|
|
|
|
{
|
|
|
|
if (m_wait_options & WNOHANG)
|
|
|
|
return false;
|
|
|
|
return add_to_blocker_set(Process::current().wait_blocker_set());
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 11:11:58 +00:00
|
|
|
void Thread::WaitBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-23 04:01:04 +00:00
|
|
|
Process::current().wait_blocker_set().try_unblock(*this);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::WaitBlocker::was_unblocked(bool)
|
|
|
|
{
|
|
|
|
bool got_sigchld, try_unblock;
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
try_unblock = !m_did_unblock;
|
|
|
|
got_sigchld = m_got_sigchild;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (try_unblock)
|
2021-08-22 13:59:47 +00:00
|
|
|
Process::current().wait_blocker_set().try_unblock(*this);
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
// If we were interrupted by SIGCHLD (which gets special handling
|
|
|
|
// here) we're not going to return with EINTR. But we're going to
|
|
|
|
// deliver SIGCHLD (only) here.
|
|
|
|
auto* current_thread = Thread::current();
|
|
|
|
if (got_sigchld && current_thread->state() != State::Stopped)
|
|
|
|
current_thread->try_dispatch_one_pending_signal(SIGCHLD);
|
|
|
|
}
|
|
|
|
|
2020-12-09 02:04:05 +00:00
|
|
|
void Thread::WaitBlocker::do_was_disowned()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_did_unblock);
|
2020-12-09 02:04:05 +00:00
|
|
|
m_did_unblock = true;
|
2021-01-20 22:11:17 +00:00
|
|
|
m_result = ECHILD;
|
2020-12-09 02:04:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
void Thread::WaitBlocker::do_set_result(const siginfo_t& result)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_did_unblock);
|
2020-11-29 23:05:27 +00:00
|
|
|
m_did_unblock = true;
|
|
|
|
m_result = result;
|
|
|
|
|
|
|
|
if (do_get_interrupted_by_signal() == SIGCHLD) {
|
|
|
|
// This makes it so that wait() will return normally despite the
|
|
|
|
// fact that SIGCHLD was delivered. Calling do_clear_interrupted_by_signal
|
|
|
|
// will disable dispatching signals in Thread::block and prevent
|
|
|
|
// it from returning with EINTR. We will then manually dispatch
|
|
|
|
// SIGCHLD (and only SIGCHLD) in was_unblocked.
|
|
|
|
m_got_sigchild = true;
|
|
|
|
do_clear_interrupted_by_signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
bool Thread::WaitBlocker::unblock(Process& process, UnblockFlags flags, u8 signal, bool from_add_blocker)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(flags != UnblockFlags::Terminated || signal == 0); // signal argument should be ignored for Terminated
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2021-08-23 04:01:04 +00:00
|
|
|
bool do_not_unblock = m_waitee.visit(
|
|
|
|
[&](NonnullRefPtr<Process> const& waitee_process) {
|
|
|
|
return &process != waitee_process;
|
|
|
|
},
|
|
|
|
[&](NonnullRefPtr<ProcessGroup> const& waitee_process_group) {
|
|
|
|
return waitee_process_group->pgid() != process.pgid();
|
|
|
|
},
|
|
|
|
[&](Empty const&) {
|
2020-12-09 02:04:05 +00:00
|
|
|
// Generic waiter won't be unblocked by disown
|
2021-08-23 04:01:04 +00:00
|
|
|
return flags == UnblockFlags::Disowned;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (do_not_unblock)
|
|
|
|
return false;
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
switch (flags) {
|
|
|
|
case UnblockFlags::Terminated:
|
|
|
|
if (!(m_wait_options & WEXITED))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case UnblockFlags::Stopped:
|
|
|
|
if (!(m_wait_options & WSTOPPED))
|
|
|
|
return false;
|
2020-12-09 04:18:45 +00:00
|
|
|
if (!(m_wait_options & WUNTRACED) && !process.is_traced())
|
2020-11-29 23:05:27 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case UnblockFlags::Continued:
|
|
|
|
if (!(m_wait_options & WCONTINUED))
|
|
|
|
return false;
|
2020-12-09 04:18:45 +00:00
|
|
|
if (!(m_wait_options & WUNTRACED) && !process.is_traced())
|
2020-11-29 23:05:27 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2020-12-09 02:04:05 +00:00
|
|
|
case UnblockFlags::Disowned:
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-12-09 02:04:05 +00:00
|
|
|
// Disowning must unblock anyone waiting for this process explicitly
|
|
|
|
if (!m_did_unblock)
|
|
|
|
do_was_disowned();
|
|
|
|
return true;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags == UnblockFlags::Terminated) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(process.is_dead());
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
// Up until this point, this function may have been called
|
|
|
|
// more than once!
|
|
|
|
do_set_result(process.wait_info());
|
|
|
|
} else {
|
2021-02-21 10:16:16 +00:00
|
|
|
siginfo_t siginfo {};
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(g_scheduler_lock);
|
2021-02-21 10:16:16 +00:00
|
|
|
// We need to gather the information before we release the scheduler lock!
|
2020-11-29 23:05:27 +00:00
|
|
|
siginfo.si_signo = SIGCHLD;
|
2020-12-09 04:18:45 +00:00
|
|
|
siginfo.si_pid = process.pid().value();
|
2021-08-28 20:11:16 +00:00
|
|
|
siginfo.si_uid = process.uid().value();
|
2020-11-29 23:05:27 +00:00
|
|
|
siginfo.si_status = signal;
|
|
|
|
|
|
|
|
switch (flags) {
|
|
|
|
case UnblockFlags::Terminated:
|
2020-12-09 02:04:05 +00:00
|
|
|
case UnblockFlags::Disowned:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-29 23:05:27 +00:00
|
|
|
case UnblockFlags::Stopped:
|
|
|
|
siginfo.si_code = CLD_STOPPED;
|
|
|
|
break;
|
|
|
|
case UnblockFlags::Continued:
|
|
|
|
siginfo.si_code = CLD_CONTINUED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_did_unblock)
|
|
|
|
return false;
|
|
|
|
// Up until this point, this function may have been called
|
|
|
|
// more than once!
|
|
|
|
do_set_result(siginfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!from_add_blocker) {
|
2021-08-22 13:59:47 +00:00
|
|
|
// Only call unblock if we weren't called from within add_to_blocker_set!
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(flags != UnblockFlags::Disowned);
|
2020-11-29 23:05:27 +00:00
|
|
|
unblock_from_blocker();
|
|
|
|
}
|
|
|
|
// Because this may be called from add_blocker, in which case we should
|
|
|
|
// not be actually trying to unblock the thread (because it hasn't actually
|
|
|
|
// been blocked yet), we need to return true anyway
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|