2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-04-28 23:46:44 +03:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2019-12-01 13:57:20 +03:00
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
bool WaitQueue::should_add_blocker(Thread::Blocker& b, void* data)
|
2019-12-01 13:57:20 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03: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 08:30:10 +03:00
|
|
|
if (m_wake_requested || !m_should_block) {
|
2020-07-06 00:46:51 +03:00
|
|
|
m_wake_requested = false;
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: do not block thread {}, {}", this, data, m_should_block ? "wake was pending" : "not blocking");
|
2020-07-06 00:46:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: should block thread {}", this, data);
|
2020-07-06 00:46:51 +03:00
|
|
|
return true;
|
2019-12-01 13:57:20 +03:00
|
|
|
}
|
|
|
|
|
2021-01-24 08:30:10 +03:00
|
|
|
u32 WaitQueue::wake_one()
|
2020-08-06 04:13:28 +03:00
|
|
|
{
|
2021-01-24 08:30:10 +03:00
|
|
|
u32 did_wake = 0;
|
2021-08-22 02:37:17 +03:00
|
|
|
ScopedSpinlock lock(m_lock);
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one", this);
|
2020-12-22 21:17:56 +03:00
|
|
|
bool did_unblock_one = do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(data);
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-12-08 07:29:41 +03:00
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one unblocking {}", this, data);
|
2020-12-08 07:29:41 +03:00
|
|
|
if (blocker.unblock()) {
|
|
|
|
stop_iterating = true;
|
2021-01-24 08:30:10 +03:00
|
|
|
did_wake = 1;
|
2020-12-08 07:29:41 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_one;
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_one woke {} threads", this, did_wake);
|
2021-01-24 08:30:10 +03:00
|
|
|
return did_wake;
|
2019-12-01 13:57:20 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
u32 WaitQueue::wake_n(u32 wake_count)
|
2020-04-26 02:57:12 +03:00
|
|
|
{
|
2020-12-08 07:29:41 +03:00
|
|
|
if (wake_count == 0)
|
2021-01-22 19:42:36 +03:00
|
|
|
return 0; // should we assert instead?
|
2021-08-22 02:37:17 +03:00
|
|
|
ScopedSpinlock lock(m_lock);
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n({})", this, wake_count);
|
2020-12-22 09:21:58 +03:00
|
|
|
u32 did_wake = 0;
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2020-12-22 21:17:56 +03:00
|
|
|
bool did_unblock_some = do_unblock([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(data);
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-12-08 07:29:41 +03:00
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n unblocking {}", this, data);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(did_wake < wake_count);
|
2020-12-08 07:29:41 +03:00
|
|
|
if (blocker.unblock()) {
|
2020-12-22 09:21:58 +03:00
|
|
|
if (++did_wake >= wake_count)
|
2020-12-08 07:29:41 +03:00
|
|
|
stop_iterating = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_some;
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_n({}) woke {} threads", this, wake_count, did_wake);
|
2020-12-22 09:21:58 +03:00
|
|
|
return did_wake;
|
2020-04-26 02:57:12 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
u32 WaitQueue::wake_all()
|
2019-12-01 13:57:20 +03:00
|
|
|
{
|
2021-08-22 02:37:17 +03:00
|
|
|
ScopedSpinlock lock(m_lock);
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all", this);
|
2020-12-22 09:21:58 +03:00
|
|
|
u32 did_wake = 0;
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2020-12-22 21:17:56 +03:00
|
|
|
bool did_unblock_any = do_unblock([&](Thread::Blocker& b, void* data, bool&) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(data);
|
|
|
|
VERIFY(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-12-08 07:29:41 +03:00
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all unblocking {}", this, data);
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
if (blocker.unblock()) {
|
|
|
|
did_wake++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-08 07:29:41 +03:00
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_any;
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(WAITQUEUE_DEBUG, "WaitQueue @ {}: wake_all woke {} threads", this, did_wake);
|
2020-12-22 09:21:58 +03: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 15:28:28 +03:00
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|