2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2019-12-01 10:57:20 +00:00
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-23 23:07:16 +00:00
|
|
|
bool WaitQueue::should_add_blocker(Thread::Blocker& b, void*)
|
2019-12-01 10:57:20 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2021-08-22 23:28:12 +00:00
|
|
|
if (m_wake_requested) {
|
2020-07-05 21:46:51 +00:00
|
|
|
m_wake_requested = false;
|
2021-08-23 23:07:16 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: do not block thread {}", this, b.thread());
|
2020-07-05 21:46:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-23 23:07:16 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: should block thread {}", this, b.thread());
|
2020-07-05 21:46:51 +00:00
|
|
|
return true;
|
2019-12-01 10:57:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 05:30:10 +00:00
|
|
|
u32 WaitQueue::wake_one()
|
2020-08-06 01:13:28 +00:00
|
|
|
{
|
2021-01-24 05:30:10 +00:00
|
|
|
u32 did_wake = 0;
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one", this);
|
2021-08-23 23:07:16 +00:00
|
|
|
bool did_unblock_one = unblock_all_blockers_whose_conditions_are_met_locked([&](Thread::Blocker& b, void*, bool& stop_iterating) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2021-08-22 22:10:33 +00:00
|
|
|
auto& blocker = static_cast<Thread::WaitQueueBlocker&>(b);
|
2021-08-23 23:07:16 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one unblocking {}", this, blocker.thread());
|
2020-12-08 04:29:41 +00:00
|
|
|
if (blocker.unblock()) {
|
|
|
|
stop_iterating = true;
|
2021-01-24 05:30:10 +00:00
|
|
|
did_wake = 1;
|
2020-12-08 04:29:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_one;
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one woke {} threads", this, did_wake);
|
2021-01-24 05:30:10 +00:00
|
|
|
return did_wake;
|
2019-12-01 10:57:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
u32 WaitQueue::wake_n(u32 wake_count)
|
2020-04-25 23:57:12 +00:00
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
if (wake_count == 0)
|
2021-01-22 16:42:36 +00:00
|
|
|
return 0; // should we assert instead?
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n({})", this, wake_count);
|
2020-12-22 06:21:58 +00:00
|
|
|
u32 did_wake = 0;
|
2021-01-13 20:16:18 +00:00
|
|
|
|
2021-08-23 23:07:16 +00:00
|
|
|
bool did_unblock_some = unblock_all_blockers_whose_conditions_are_met_locked([&](Thread::Blocker& b, void*, bool& stop_iterating) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2021-08-22 22:10:33 +00:00
|
|
|
auto& blocker = static_cast<Thread::WaitQueueBlocker&>(b);
|
2021-08-23 23:07:16 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n unblocking {}", this, blocker.thread());
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(did_wake < wake_count);
|
2020-12-08 04:29:41 +00:00
|
|
|
if (blocker.unblock()) {
|
2020-12-22 06:21:58 +00:00
|
|
|
if (++did_wake >= wake_count)
|
2020-12-08 04:29:41 +00:00
|
|
|
stop_iterating = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_some;
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n({}) woke {} threads", this, wake_count, did_wake);
|
2020-12-22 06:21:58 +00:00
|
|
|
return did_wake;
|
2020-04-25 23:57:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
u32 WaitQueue::wake_all()
|
2019-12-01 10:57:20 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-01-13 20:16:18 +00:00
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all", this);
|
2020-12-22 06:21:58 +00:00
|
|
|
u32 did_wake = 0;
|
2021-01-13 20:16:18 +00:00
|
|
|
|
2021-08-23 23:07:16 +00:00
|
|
|
bool did_unblock_any = unblock_all_blockers_whose_conditions_are_met_locked([&](Thread::Blocker& b, void*, bool&) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2021-08-22 22:10:33 +00:00
|
|
|
auto& blocker = static_cast<Thread::WaitQueueBlocker&>(b);
|
2021-01-13 20:16:18 +00:00
|
|
|
|
2021-08-23 23:07:16 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all unblocking {}", this, blocker.thread());
|
2021-01-13 20:16:18 +00:00
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
if (blocker.unblock()) {
|
|
|
|
did_wake++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-08 04:29:41 +00:00
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_any;
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all woke {} threads", this, did_wake);
|
2020-12-22 06:21:58 +00:00
|
|
|
return did_wake;
|
Kernel: Allow process with multiple threads to call exec and exit
This allows a process wich has more than 1 thread to call exec, even
from a thread. This kills all the other threads, but it won't wait for
them to finish, just makes sure that they are not in a running/runable
state.
In the case where a thread does exec, the new program PID will be the
thread TID, to keep the PID == TID in the new process.
This introduces a new function inside the Process class,
kill_threads_except_self which is called on exit() too (exit with
multiple threads wasn't properly working either).
Inside the Lock class, there is the need for a new function,
clear_waiters, which removes all the waiters from the
Process::big_lock. This is needed since after a exit/exec, there should
be no other threads waiting for this lock, the threads should be simply
killed. Only queued threads should wait for this lock at this point,
since blocked threads are handled in set_should_die.
2020-02-18 12:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|