2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-12-15 02:36:22 +03:00
|
|
|
* Copyright (c) 2020, The SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-01-13 23:16:18 +03:00
|
|
|
#include <AK/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
|
|
|
{
|
2020-12-08 07:29:41 +03:00
|
|
|
ASSERT(data != nullptr); // Thread that is requesting to be blocked
|
|
|
|
ASSERT(m_lock.is_locked());
|
|
|
|
ASSERT(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-01-24 08:30:10 +03:00
|
|
|
dbgln<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-01-24 01:59:27 +03:00
|
|
|
dbgln<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;
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2021-01-24 01:59:27 +03:00
|
|
|
dbgln<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) {
|
2020-12-08 07:29:41 +03:00
|
|
|
ASSERT(data);
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-01-24 01:59:27 +03:00
|
|
|
dbgln<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-01-24 08:30:10 +03:00
|
|
|
dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_one woke {} threads", this, did_wake);
|
|
|
|
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?
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2021-01-24 01:59:27 +03:00
|
|
|
dbgln<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) {
|
2020-12-08 07:29:41 +03:00
|
|
|
ASSERT(data);
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-01-24 01:59:27 +03:00
|
|
|
dbgln<WAITQUEUE_DEBUG>("WaitQueue @ {}: wake_n unblocking {}", this, data);
|
2020-12-22 09:21:58 +03:00
|
|
|
ASSERT(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-01-24 08:30:10 +03:00
|
|
|
dbgln<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
|
|
|
{
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
dbgln<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&) {
|
2020-12-08 07:29:41 +03:00
|
|
|
ASSERT(data);
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2021-01-13 23:16:18 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
dbgln<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-01-24 08:30:10 +03:00
|
|
|
dbgln<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
|
|
|
}
|