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 {
|
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
bool WaitQueue::should_add_blocker(Thread::Blocker& b, void* data)
|
2019-12-01 10:57:20 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(data != nullptr); // Thread that is requesting to be blocked
|
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2021-01-24 05:30:10 +00:00
|
|
|
if (m_wake_requested || !m_should_block) {
|
2020-07-05 21:46:51 +00:00
|
|
|
m_wake_requested = false;
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: do not block thread {}, {}", this, data, m_should_block ? "wake was pending" : "not blocking");
|
2020-07-05 21:46:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: should block thread {}", this, data);
|
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;
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one", this);
|
2020-12-22 18:17:56 +00:00
|
|
|
bool did_unblock_one = do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(data);
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-12-08 04:29:41 +00:00
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one unblocking {}", this, data);
|
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?
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock 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
|
|
|
|
2020-12-22 18:17:56 +00:00
|
|
|
bool did_unblock_some = do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(data);
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-12-08 04:29:41 +00:00
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n unblocking {}", this, data);
|
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
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock 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
|
|
|
|
2020-12-22 18:17:56 +00:00
|
|
|
bool did_unblock_any = do_unblock([&](Thread::Blocker& b, void* data, bool&) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(data);
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-12-08 04:29:41 +00:00
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-01-13 20:16:18 +00:00
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all unblocking {}", this, data);
|
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
|
|
|
}
|