2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-12-14 23:36:22 +00:00
|
|
|
* Copyright (c) 2020, The SerenityOS developers.
|
2020-01-18 08:38:21 +00: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.
|
|
|
|
*/
|
|
|
|
|
2019-12-01 10:57:20 +00:00
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
|
2020-07-05 21:46:51 +00:00
|
|
|
//#define WAITQUEUE_DEBUG
|
|
|
|
|
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
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
ASSERT(data != nullptr); // Thread that is requesting to be blocked
|
|
|
|
ASSERT(m_lock.is_locked());
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
2020-07-05 21:46:51 +00:00
|
|
|
if (m_wake_requested) {
|
|
|
|
m_wake_requested = false;
|
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": do not block thread " << *static_cast<Thread*>(data) << ", wake was pending";
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-08 04:29:41 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
|
|
|
dbg() << "WaitQueue @ " << this << ": should block thread " << *static_cast<Thread*>(data);
|
|
|
|
#endif
|
2020-07-05 21:46:51 +00:00
|
|
|
return true;
|
2019-12-01 10:57:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
void WaitQueue::wake_one()
|
2020-08-06 01:13:28 +00:00
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-05 21:46:51 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": wake_one";
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
2020-12-08 04:29:41 +00:00
|
|
|
bool did_unblock_one = do_unblock_some([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
|
|
|
|
ASSERT(data);
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2020-07-05 21:46:51 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": wake_one unblocking " << *static_cast<Thread*>(data);
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
2020-12-08 04:29:41 +00:00
|
|
|
if (blocker.unblock()) {
|
|
|
|
stop_iterating = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_one;
|
2019-12-01 10:57:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:46:51 +00:00
|
|
|
void 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)
|
|
|
|
return; // should we assert instaed?
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-05 21:46:51 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": wake_n(" << wake_count << ")";
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
2020-12-08 04:29:41 +00:00
|
|
|
bool did_unblock_some = do_unblock_some([&](Thread::Blocker& b, void* data, bool& stop_iterating) {
|
|
|
|
ASSERT(data);
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2020-07-05 21:46:51 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": wake_n unblocking " << *static_cast<Thread*>(data);
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
2020-12-08 04:29:41 +00:00
|
|
|
ASSERT(wake_count > 0);
|
|
|
|
if (blocker.unblock()) {
|
|
|
|
if (--wake_count == 0)
|
|
|
|
stop_iterating = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_some;
|
2020-04-25 23:57:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 10:57:20 +00:00
|
|
|
void WaitQueue::wake_all()
|
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-07-05 21:46:51 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": wake_all";
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
2020-12-08 04:29:41 +00:00
|
|
|
bool did_unblock_any = do_unblock_all([&](Thread::Blocker& b, void* data) {
|
|
|
|
ASSERT(data);
|
|
|
|
ASSERT(b.blocker_type() == Thread::Blocker::Type::Queue);
|
|
|
|
auto& blocker = static_cast<Thread::QueueBlocker&>(b);
|
2020-07-05 21:46:51 +00:00
|
|
|
#ifdef WAITQUEUE_DEBUG
|
2020-12-08 04:29:41 +00:00
|
|
|
dbg() << "WaitQueue @ " << this << ": wake_all unblocking " << *static_cast<Thread*>(data);
|
2020-07-05 21:46:51 +00:00
|
|
|
#endif
|
2020-12-08 04:29:41 +00:00
|
|
|
return blocker.unblock();
|
|
|
|
});
|
|
|
|
m_wake_requested = !did_unblock_any;
|
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
|
|
|
}
|