2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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-03-24 00:03:17 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-14 15:51:54 +03:00
|
|
|
#include <AK/Function.h>
|
2019-07-19 14:04:42 +03:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-02-16 03:27:42 +03:00
|
|
|
#include <AK/Optional.h>
|
2019-04-20 20:23:45 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-10-20 19:11:40 +03:00
|
|
|
#include <AK/String.h>
|
2020-11-15 21:58:19 +03:00
|
|
|
#include <AK/Time.h>
|
2019-03-24 00:03:17 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-09-27 17:53:35 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
|
|
|
#include <AK/Weakable.h>
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2020-12-20 04:48:56 +03:00
|
|
|
#include <Kernel/Arch/i386/SafeMem.h>
|
2020-02-16 03:50:16 +03:00
|
|
|
#include <Kernel/Forward.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/KResult.h>
|
2020-12-15 02:36:22 +03:00
|
|
|
#include <Kernel/LockMode.h>
|
2019-09-04 16:14:54 +03:00
|
|
|
#include <Kernel/Scheduler.h>
|
2020-04-03 14:50:17 +03:00
|
|
|
#include <Kernel/ThreadTracer.h>
|
2020-11-15 21:58:19 +03:00
|
|
|
#include <Kernel/TimerQueue.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-07-19 10:04:12 +03:00
|
|
|
#include <LibC/fd_set.h>
|
2020-07-05 02:37:36 +03:00
|
|
|
#include <LibELF/AuxiliaryVector.h>
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-12-01 05:04:36 +03:00
|
|
|
//#define LOCK_DEBUG
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
enum class DispatchSignalResult {
|
|
|
|
Deferred = 0,
|
|
|
|
Yield,
|
|
|
|
Terminate,
|
|
|
|
Continue
|
2019-05-28 12:53:16 +03:00
|
|
|
};
|
2019-03-24 00:03:17 +03:00
|
|
|
|
|
|
|
struct SignalActionData {
|
2019-06-07 13:56:50 +03:00
|
|
|
VirtualAddress handler_or_sigaction;
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 mask { 0 };
|
2019-03-24 00:03:17 +03:00
|
|
|
int flags { 0 };
|
|
|
|
};
|
|
|
|
|
2019-09-07 16:50:44 +03:00
|
|
|
struct ThreadSpecificData {
|
|
|
|
ThreadSpecificData* self;
|
|
|
|
};
|
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
#define THREAD_PRIORITY_MIN 1
|
|
|
|
#define THREAD_PRIORITY_LOW 10
|
|
|
|
#define THREAD_PRIORITY_NORMAL 30
|
|
|
|
#define THREAD_PRIORITY_HIGH 50
|
|
|
|
#define THREAD_PRIORITY_MAX 99
|
2019-11-06 18:26:51 +03:00
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
#define THREAD_AFFINITY_DEFAULT 0xffffffff
|
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
class Thread
|
|
|
|
: public RefCounted<Thread>
|
|
|
|
, public Weakable<Thread> {
|
2020-04-22 12:54:58 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(Thread);
|
|
|
|
AK_MAKE_NONMOVABLE(Thread);
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
friend class Process;
|
|
|
|
friend class Scheduler;
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
public:
|
2020-06-29 00:34:31 +03:00
|
|
|
inline static Thread* current()
|
|
|
|
{
|
|
|
|
return Processor::current().current_thread();
|
|
|
|
}
|
2020-02-17 17:04:27 +03:00
|
|
|
|
2020-08-02 05:04:56 +03:00
|
|
|
explicit Thread(NonnullRefPtr<Process>);
|
2019-03-24 00:03:17 +03:00
|
|
|
~Thread();
|
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
static RefPtr<Thread> from_tid(ThreadID);
|
2019-03-24 00:03:17 +03:00
|
|
|
static void finalize_dying_threads();
|
|
|
|
|
2020-08-08 18:32:34 +03:00
|
|
|
ThreadID tid() const { return m_tid; }
|
|
|
|
ProcessID pid() const;
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
void set_priority(u32 p) { m_priority = p; }
|
|
|
|
u32 priority() const { return m_priority; }
|
|
|
|
|
2019-12-30 22:10:00 +03:00
|
|
|
u32 effective_priority() const;
|
2019-11-06 18:26:51 +03:00
|
|
|
|
2020-09-26 06:44:43 +03:00
|
|
|
void detach()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_is_joinable = false;
|
|
|
|
}
|
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool is_joinable() const
|
2020-09-26 06:44:43 +03:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return m_is_joinable;
|
|
|
|
}
|
2019-11-18 06:08:10 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
Process& process() { return m_process; }
|
|
|
|
const Process& process() const { return m_process; }
|
|
|
|
|
2020-08-02 05:04:56 +03:00
|
|
|
String backtrace();
|
2020-04-11 21:39:27 +03:00
|
|
|
Vector<FlatPtr> raw_backtrace(FlatPtr ebp, FlatPtr eip) const;
|
2019-07-25 22:02:19 +03:00
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
String name() const
|
|
|
|
{
|
|
|
|
// Because the name can be changed, we can't return a const
|
|
|
|
// reference here. We must make a copy
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
void set_name(const StringView& s)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_name = s;
|
|
|
|
}
|
|
|
|
void set_name(String&& name)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_name = move(name);
|
|
|
|
}
|
2019-12-07 22:45:26 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
void finalize();
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
enum State : u8 {
|
2019-03-24 00:03:17 +03:00
|
|
|
Invalid = 0,
|
|
|
|
Runnable,
|
|
|
|
Running,
|
|
|
|
Dying,
|
|
|
|
Dead,
|
|
|
|
Stopped,
|
2020-12-08 07:29:41 +03:00
|
|
|
Blocked
|
2019-03-24 00:03:17 +03:00
|
|
|
};
|
|
|
|
|
2020-11-15 21:58:19 +03:00
|
|
|
class BlockResult {
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
WokeNormally,
|
|
|
|
NotBlocked,
|
|
|
|
InterruptedBySignal,
|
|
|
|
InterruptedByDeath,
|
|
|
|
InterruptedByTimeout,
|
|
|
|
};
|
|
|
|
|
|
|
|
BlockResult() = delete;
|
|
|
|
|
|
|
|
BlockResult(Type type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(Type type) const
|
|
|
|
{
|
|
|
|
return m_type == type;
|
|
|
|
}
|
|
|
|
bool operator!=(Type type) const
|
|
|
|
{
|
|
|
|
return m_type != type;
|
|
|
|
}
|
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool was_interrupted() const
|
2020-11-15 21:58:19 +03:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case InterruptedBySignal:
|
|
|
|
case InterruptedByDeath:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool timed_out() const
|
2020-11-15 21:58:19 +03:00
|
|
|
{
|
|
|
|
return m_type == InterruptedByTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type m_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BlockTimeout {
|
|
|
|
public:
|
|
|
|
BlockTimeout()
|
|
|
|
: m_infinite(true)
|
|
|
|
{
|
|
|
|
}
|
2020-12-04 08:12:50 +03:00
|
|
|
explicit BlockTimeout(bool is_absolute, const timeval* time, const timespec* start_time = nullptr, clockid_t clock_id = CLOCK_MONOTONIC_COARSE)
|
2020-12-02 02:53:47 +03:00
|
|
|
: m_clock_id(clock_id)
|
|
|
|
, m_infinite(!time)
|
2020-11-15 21:58:19 +03:00
|
|
|
{
|
|
|
|
if (!m_infinite) {
|
|
|
|
if (time->tv_sec > 0 || time->tv_usec > 0) {
|
|
|
|
timeval_to_timespec(*time, m_time);
|
|
|
|
m_should_block = true;
|
|
|
|
}
|
2020-12-02 02:53:47 +03:00
|
|
|
m_start_time = start_time ? *start_time : TimeManagement::the().current_time(clock_id).value();
|
2020-11-15 21:58:19 +03:00
|
|
|
if (!is_absolute)
|
|
|
|
timespec_add(m_time, m_start_time, m_time);
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 08:12:50 +03:00
|
|
|
explicit BlockTimeout(bool is_absolute, const timespec* time, const timespec* start_time = nullptr, clockid_t clock_id = CLOCK_MONOTONIC_COARSE)
|
2020-12-02 02:53:47 +03:00
|
|
|
: m_clock_id(clock_id)
|
|
|
|
, m_infinite(!time)
|
2020-11-15 21:58:19 +03:00
|
|
|
{
|
|
|
|
if (!m_infinite) {
|
|
|
|
if (time->tv_sec > 0 || time->tv_nsec > 0) {
|
|
|
|
m_time = *time;
|
|
|
|
m_should_block = true;
|
|
|
|
}
|
2020-12-02 02:53:47 +03:00
|
|
|
m_start_time = start_time ? *start_time : TimeManagement::the().current_time(clock_id).value();
|
2020-11-15 21:58:19 +03:00
|
|
|
if (!is_absolute)
|
|
|
|
timespec_add(m_time, m_start_time, m_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const timespec& absolute_time() const { return m_time; }
|
|
|
|
const timespec* start_time() const { return !m_infinite ? &m_start_time : nullptr; }
|
2020-12-02 02:53:47 +03:00
|
|
|
clockid_t clock_id() const { return m_clock_id; }
|
2020-11-15 21:58:19 +03:00
|
|
|
bool is_infinite() const { return m_infinite; }
|
|
|
|
bool should_block() const { return m_infinite || m_should_block; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
timespec m_time { 0, 0 };
|
|
|
|
timespec m_start_time { 0, 0 };
|
2020-12-04 08:12:50 +03:00
|
|
|
clockid_t m_clock_id { CLOCK_MONOTONIC_COARSE };
|
2020-11-15 21:58:19 +03:00
|
|
|
bool m_infinite { false };
|
|
|
|
bool m_should_block { false };
|
|
|
|
};
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
class BlockCondition;
|
|
|
|
|
2019-07-18 19:12:37 +03:00
|
|
|
class Blocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
enum class Type {
|
|
|
|
Unknown = 0,
|
|
|
|
File,
|
2020-12-22 09:21:58 +03:00
|
|
|
Futex,
|
2020-11-30 02:05:27 +03:00
|
|
|
Plan9FS,
|
|
|
|
Join,
|
2020-12-08 07:29:41 +03:00
|
|
|
Queue,
|
2020-11-30 02:05:27 +03:00
|
|
|
Routing,
|
|
|
|
Sleep,
|
|
|
|
Wait
|
|
|
|
};
|
|
|
|
virtual ~Blocker();
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const = 0;
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual bool should_block() { return true; }
|
|
|
|
virtual Type blocker_type() const = 0;
|
2020-11-15 21:58:19 +03:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout& timeout) { return timeout; }
|
2020-12-09 07:18:45 +03:00
|
|
|
virtual bool can_be_interrupted() const { return true; }
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual void not_blocking(bool) = 0;
|
|
|
|
virtual void was_unblocked(bool did_timeout)
|
|
|
|
{
|
|
|
|
if (did_timeout) {
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_did_timeout = true;
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 06:44:43 +03:00
|
|
|
void set_interrupted_by_death()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-11-30 02:05:27 +03:00
|
|
|
do_set_interrupted_by_death();
|
|
|
|
}
|
|
|
|
void set_interrupted_by_signal(u8 signal)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
do_set_interrupted_by_signal(signal);
|
2020-09-26 06:44:43 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
u8 was_interrupted_by_signal() const
|
2020-09-26 06:44:43 +03:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-11-30 02:05:27 +03:00
|
|
|
return do_get_interrupted_by_signal();
|
2020-09-26 06:44:43 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual Thread::BlockResult block_result()
|
2020-09-26 06:44:43 +03:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-11-15 21:58:19 +03:00
|
|
|
if (m_was_interrupted_by_death)
|
|
|
|
return Thread::BlockResult::InterruptedByDeath;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (m_was_interrupted_by_signal != 0)
|
2020-11-15 21:58:19 +03:00
|
|
|
return Thread::BlockResult::InterruptedBySignal;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (m_did_timeout)
|
2020-11-15 21:58:19 +03:00
|
|
|
return Thread::BlockResult::InterruptedByTimeout;
|
|
|
|
return Thread::BlockResult::WokeNormally;
|
2020-09-26 06:44:43 +03:00
|
|
|
}
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
void begin_blocking(Badge<Thread>);
|
|
|
|
BlockResult end_blocking(Badge<Thread>, bool);
|
|
|
|
|
2020-09-26 06:44:43 +03:00
|
|
|
protected:
|
2020-11-30 02:05:27 +03:00
|
|
|
void do_set_interrupted_by_death()
|
|
|
|
{
|
|
|
|
m_was_interrupted_by_death = true;
|
|
|
|
}
|
|
|
|
void do_set_interrupted_by_signal(u8 signal)
|
|
|
|
{
|
|
|
|
ASSERT(signal != 0);
|
|
|
|
m_was_interrupted_by_signal = signal;
|
|
|
|
}
|
|
|
|
void do_clear_interrupted_by_signal()
|
|
|
|
{
|
|
|
|
m_was_interrupted_by_signal = 0;
|
|
|
|
}
|
|
|
|
u8 do_get_interrupted_by_signal() const
|
|
|
|
{
|
|
|
|
return m_was_interrupted_by_signal;
|
|
|
|
}
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool was_interrupted() const
|
2020-11-30 02:05:27 +03:00
|
|
|
{
|
|
|
|
return m_was_interrupted_by_death || m_was_interrupted_by_signal != 0;
|
|
|
|
}
|
|
|
|
void unblock_from_blocker()
|
|
|
|
{
|
|
|
|
RefPtr<Thread> thread;
|
|
|
|
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (m_is_blocking) {
|
|
|
|
m_is_blocking = false;
|
|
|
|
ASSERT(m_blocked_thread);
|
|
|
|
thread = m_blocked_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread)
|
|
|
|
thread->unblock_from_blocker(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set_block_condition(BlockCondition&, void* = nullptr);
|
2020-12-22 09:21:58 +03:00
|
|
|
void set_block_condition_raw_locked(BlockCondition* block_condition)
|
|
|
|
{
|
|
|
|
m_block_condition = block_condition;
|
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
|
2020-09-26 06:44:43 +03:00
|
|
|
mutable RecursiveSpinLock m_lock;
|
2019-09-04 16:14:54 +03:00
|
|
|
|
2019-07-20 12:05:52 +03:00
|
|
|
private:
|
2020-11-30 02:05:27 +03:00
|
|
|
BlockCondition* m_block_condition { nullptr };
|
|
|
|
void* m_block_data { nullptr };
|
|
|
|
Thread* m_blocked_thread { nullptr };
|
|
|
|
u8 m_was_interrupted_by_signal { 0 };
|
|
|
|
bool m_is_blocking { false };
|
2020-01-10 21:15:01 +03:00
|
|
|
bool m_was_interrupted_by_death { false };
|
2020-11-30 02:05:27 +03:00
|
|
|
bool m_did_timeout { false };
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
class BlockCondition {
|
|
|
|
AK_MAKE_NONCOPYABLE(BlockCondition);
|
|
|
|
AK_MAKE_NONMOVABLE(BlockCondition);
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlockCondition() = default;
|
|
|
|
|
|
|
|
virtual ~BlockCondition()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
ASSERT(m_blockers.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool add_blocker(Blocker& blocker, void* data)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (!should_add_blocker(blocker, data))
|
|
|
|
return false;
|
|
|
|
m_blockers.append({ &blocker, data });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_blocker(Blocker& blocker, void* data)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
// NOTE: it's possible that the blocker is no longer present
|
|
|
|
m_blockers.remove_first_matching([&](auto& info) {
|
|
|
|
return info.blocker == &blocker && info.data == data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return is_empty_locked();
|
|
|
|
}
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
protected:
|
|
|
|
template<typename UnblockOne>
|
2020-12-22 21:17:56 +03:00
|
|
|
bool unblock(UnblockOne unblock_one)
|
2020-11-30 02:05:27 +03:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-12-22 21:17:56 +03:00
|
|
|
return do_unblock(unblock_one);
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename UnblockOne>
|
2020-12-22 21:17:56 +03:00
|
|
|
bool do_unblock(UnblockOne unblock_one)
|
2020-11-30 02:05:27 +03:00
|
|
|
{
|
|
|
|
ASSERT(m_lock.is_locked());
|
2020-12-08 07:29:41 +03:00
|
|
|
bool stop_iterating = false;
|
2020-12-31 05:29:41 +03:00
|
|
|
bool did_unblock = false;
|
2020-12-08 07:29:41 +03:00
|
|
|
for (size_t i = 0; i < m_blockers.size() && !stop_iterating;) {
|
|
|
|
auto& info = m_blockers[i];
|
|
|
|
if (unblock_one(*info.blocker, info.data, stop_iterating)) {
|
|
|
|
m_blockers.remove(i);
|
2020-12-31 05:29:41 +03:00
|
|
|
did_unblock = true;
|
2020-12-08 07:29:41 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2020-12-31 05:29:41 +03:00
|
|
|
return did_unblock;
|
2020-12-08 07:29:41 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
bool is_empty_locked() const
|
|
|
|
{
|
|
|
|
ASSERT(m_lock.is_locked());
|
|
|
|
return m_blockers.is_empty();
|
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
virtual bool should_add_blocker(Blocker&, void*) { return true; }
|
2020-11-30 02:05:27 +03:00
|
|
|
|
|
|
|
struct BlockerInfo {
|
|
|
|
Blocker* blocker;
|
|
|
|
void* data;
|
|
|
|
};
|
2020-12-22 09:21:58 +03:00
|
|
|
|
|
|
|
Vector<BlockerInfo, 4> do_take_blockers(size_t count)
|
|
|
|
{
|
|
|
|
if (m_blockers.size() <= count)
|
|
|
|
return move(m_blockers);
|
|
|
|
|
|
|
|
size_t move_count = (count <= m_blockers.size()) ? count : m_blockers.size();
|
|
|
|
ASSERT(move_count > 0);
|
|
|
|
|
|
|
|
Vector<BlockerInfo, 4> taken_blockers;
|
|
|
|
taken_blockers.ensure_capacity(move_count);
|
|
|
|
for (size_t i = 0; i < move_count; i++)
|
|
|
|
taken_blockers.append(m_blockers.take(i));
|
|
|
|
m_blockers.remove(0, move_count);
|
|
|
|
return taken_blockers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_append_blockers(Vector<BlockerInfo, 4>&& blockers_to_append)
|
|
|
|
{
|
|
|
|
if (blockers_to_append.is_empty())
|
|
|
|
return;
|
|
|
|
if (m_blockers.is_empty()) {
|
|
|
|
m_blockers = move(blockers_to_append);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_blockers.ensure_capacity(m_blockers.size() + blockers_to_append.size());
|
|
|
|
for (size_t i = 0; i < blockers_to_append.size(); i++)
|
|
|
|
m_blockers.append(blockers_to_append.take(i));
|
|
|
|
blockers_to_append.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
mutable SpinLock<u8> m_lock;
|
|
|
|
|
|
|
|
private:
|
2020-11-30 02:05:27 +03:00
|
|
|
Vector<BlockerInfo, 4> m_blockers;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class JoinBlocker;
|
2019-11-14 22:58:23 +03:00
|
|
|
class JoinBlocker final : public Blocker {
|
|
|
|
public:
|
2020-09-26 06:44:43 +03:00
|
|
|
explicit JoinBlocker(Thread& joinee, KResult& try_join_result, void*& joinee_exit_value);
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual Type blocker_type() const override { return Type::Join; }
|
2019-11-14 22:58:23 +03:00
|
|
|
virtual const char* state_string() const override { return "Joining"; }
|
2020-12-09 07:18:45 +03:00
|
|
|
virtual bool can_be_interrupted() const override { return false; }
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual bool should_block() override { return !m_join_error && m_should_block; }
|
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
|
|
|
|
bool unblock(void*, bool);
|
2019-11-14 22:58:23 +03:00
|
|
|
|
|
|
|
private:
|
2020-11-30 02:05:27 +03:00
|
|
|
NonnullRefPtr<Thread> m_joinee;
|
2019-11-14 23:04:34 +03:00
|
|
|
void*& m_joinee_exit_value;
|
2020-09-26 06:44:43 +03:00
|
|
|
bool m_join_error { false };
|
2020-11-30 02:05:27 +03:00
|
|
|
bool m_did_unblock { false };
|
|
|
|
bool m_should_block { true };
|
|
|
|
};
|
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
class QueueBlocker : public Blocker {
|
|
|
|
public:
|
|
|
|
explicit QueueBlocker(WaitQueue&, const char* block_reason = nullptr);
|
|
|
|
virtual ~QueueBlocker();
|
|
|
|
|
|
|
|
virtual Type blocker_type() const override { return Type::Queue; }
|
|
|
|
virtual const char* state_string() const override { return m_block_reason ? m_block_reason : "Queue"; }
|
|
|
|
virtual void not_blocking(bool) override { }
|
|
|
|
|
|
|
|
virtual bool should_block() override
|
|
|
|
{
|
|
|
|
return m_should_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unblock();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* const m_block_reason;
|
|
|
|
bool m_should_block { true };
|
|
|
|
bool m_did_unblock { false };
|
|
|
|
};
|
|
|
|
|
2020-12-22 09:21:58 +03:00
|
|
|
class FutexBlocker : public Blocker {
|
|
|
|
public:
|
|
|
|
explicit FutexBlocker(FutexQueue&, u32);
|
|
|
|
virtual ~FutexBlocker();
|
|
|
|
|
|
|
|
virtual Type blocker_type() const override { return Type::Futex; }
|
|
|
|
virtual const char* state_string() const override { return "Futex"; }
|
|
|
|
virtual void not_blocking(bool) override { }
|
|
|
|
|
|
|
|
virtual bool should_block() override
|
|
|
|
{
|
|
|
|
return m_should_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 bitset() const { return m_bitset; }
|
|
|
|
|
|
|
|
void begin_requeue()
|
|
|
|
{
|
|
|
|
// We need to hold the lock until we moved it over
|
|
|
|
m_relock_flags = m_lock.lock();
|
|
|
|
}
|
|
|
|
void finish_requeue(FutexQueue&);
|
|
|
|
|
|
|
|
bool unblock_bitset(u32 bitset);
|
|
|
|
bool unblock(bool force = false);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
u32 m_bitset;
|
|
|
|
u32 m_relock_flags { 0 };
|
|
|
|
bool m_should_block { true };
|
|
|
|
bool m_did_unblock { false };
|
|
|
|
};
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
class FileBlocker : public Blocker {
|
|
|
|
public:
|
|
|
|
enum class BlockFlags : u32 {
|
|
|
|
None = 0,
|
|
|
|
|
|
|
|
Read = 1 << 0,
|
|
|
|
Write = 1 << 1,
|
|
|
|
ReadPriority = 1 << 2,
|
|
|
|
|
|
|
|
Accept = 1 << 3,
|
|
|
|
Connect = 1 << 4,
|
|
|
|
SocketFlags = Accept | Connect,
|
|
|
|
|
|
|
|
WriteNotOpen = 1 << 5,
|
|
|
|
WriteError = 1 << 6,
|
|
|
|
WriteHangUp = 1 << 7,
|
|
|
|
ReadHangUp = 1 << 8,
|
|
|
|
Exception = WriteNotOpen | WriteError | WriteHangUp | ReadHangUp,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual Type blocker_type() const override { return Type::File; }
|
|
|
|
|
|
|
|
virtual bool should_block() override
|
|
|
|
{
|
|
|
|
return m_should_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool unblock(bool, void*) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool m_should_block { true };
|
2019-11-14 22:58:23 +03:00
|
|
|
};
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
class FileDescriptionBlocker : public FileBlocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2019-07-19 14:19:47 +03:00
|
|
|
const FileDescription& blocked_description() const;
|
2019-07-18 17:22:26 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual bool unblock(bool, void*) override;
|
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
|
2019-07-19 14:32:56 +03:00
|
|
|
protected:
|
2020-11-30 02:05:27 +03:00
|
|
|
explicit FileDescriptionBlocker(FileDescription&, BlockFlags, BlockFlags&);
|
2019-07-19 14:32:56 +03:00
|
|
|
|
2019-07-18 17:22:26 +03:00
|
|
|
private:
|
2019-07-19 14:19:47 +03:00
|
|
|
NonnullRefPtr<FileDescription> m_blocked_description;
|
2020-11-30 02:05:27 +03:00
|
|
|
const BlockFlags m_flags;
|
|
|
|
BlockFlags& m_unblocked_flags;
|
|
|
|
bool m_did_unblock { false };
|
|
|
|
bool m_should_block { true };
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class AcceptBlocker final : public FileDescriptionBlocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
explicit AcceptBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Accepting"; }
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class ConnectBlocker final : public FileDescriptionBlocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
explicit ConnectBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Connecting"; }
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class WriteBlocker final : public FileDescriptionBlocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
explicit WriteBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Writing"; }
|
2020-11-15 21:58:19 +03:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2020-01-26 19:54:23 +03:00
|
|
|
private:
|
2020-11-15 21:58:19 +03:00
|
|
|
BlockTimeout m_timeout;
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class ReadBlocker final : public FileDescriptionBlocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
explicit ReadBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Reading"; }
|
2020-11-15 21:58:19 +03:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2020-01-26 19:54:23 +03:00
|
|
|
private:
|
2020-11-15 21:58:19 +03:00
|
|
|
BlockTimeout m_timeout;
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class SleepBlocker final : public Blocker {
|
2019-07-18 18:26:11 +03:00
|
|
|
public:
|
2020-11-15 21:58:19 +03:00
|
|
|
explicit SleepBlocker(const BlockTimeout&, timespec* = nullptr);
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Sleeping"; }
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual Type blocker_type() const override { return Type::Sleep; }
|
2020-11-15 21:58:19 +03:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
virtual void was_unblocked(bool) override;
|
|
|
|
virtual Thread::BlockResult block_result() override;
|
2019-07-18 18:26:11 +03:00
|
|
|
|
|
|
|
private:
|
2020-11-30 02:05:27 +03:00
|
|
|
void calculate_remaining();
|
|
|
|
|
2020-11-15 21:58:19 +03:00
|
|
|
BlockTimeout m_deadline;
|
|
|
|
timespec* m_remaining;
|
2019-07-18 18:26:11 +03:00
|
|
|
};
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
class SelectBlocker final : public FileBlocker {
|
2019-07-18 18:39:49 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
struct FDInfo {
|
|
|
|
NonnullRefPtr<FileDescription> description;
|
|
|
|
BlockFlags block_flags;
|
|
|
|
BlockFlags unblocked_flags { BlockFlags::None };
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Vector<FDInfo, FD_SETSIZE> FDVector;
|
|
|
|
SelectBlocker(FDVector& fds);
|
|
|
|
virtual ~SelectBlocker();
|
|
|
|
|
|
|
|
virtual bool unblock(bool, void*) override;
|
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
virtual void was_unblocked(bool) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Selecting"; }
|
2019-07-18 18:39:49 +03:00
|
|
|
|
|
|
|
private:
|
2020-11-30 02:05:27 +03:00
|
|
|
size_t collect_unblocked_flags();
|
|
|
|
|
|
|
|
FDVector& m_fds;
|
|
|
|
bool m_did_unblock { false };
|
2019-07-18 18:39:49 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class WaitBlocker final : public Blocker {
|
2019-07-18 19:05:19 +03:00
|
|
|
public:
|
2020-11-30 02:05:27 +03:00
|
|
|
enum class UnblockFlags {
|
|
|
|
Terminated,
|
|
|
|
Stopped,
|
2020-12-09 05:04:05 +03:00
|
|
|
Continued,
|
|
|
|
Disowned
|
2020-11-30 02:05:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
WaitBlocker(int wait_options, idtype_t id_type, pid_t id, KResultOr<siginfo_t>& result);
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Waiting"; }
|
2020-11-30 02:05:27 +03:00
|
|
|
virtual Type blocker_type() const override { return Type::Wait; }
|
|
|
|
virtual bool should_block() override { return m_should_block; }
|
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
virtual void was_unblocked(bool) override;
|
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
bool unblock(Process& process, UnblockFlags flags, u8 signal, bool from_add_blocker);
|
2020-11-30 02:05:27 +03:00
|
|
|
bool is_wait() const { return !(m_wait_options & WNOWAIT); }
|
2019-07-18 19:05:19 +03:00
|
|
|
|
|
|
|
private:
|
2020-12-09 05:04:05 +03:00
|
|
|
void do_was_disowned();
|
2020-11-30 02:05:27 +03:00
|
|
|
void do_set_result(const siginfo_t&);
|
|
|
|
|
|
|
|
const int m_wait_options;
|
|
|
|
const idtype_t m_id_type;
|
|
|
|
const pid_t m_waitee_id;
|
|
|
|
KResultOr<siginfo_t>& m_result;
|
|
|
|
RefPtr<Process> m_waitee;
|
|
|
|
RefPtr<ProcessGroup> m_waitee_group;
|
|
|
|
bool m_did_unblock { false };
|
|
|
|
bool m_error { false };
|
|
|
|
bool m_got_sigchild { false };
|
|
|
|
bool m_should_block;
|
2019-07-18 19:05:19 +03:00
|
|
|
};
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
class WaitBlockCondition final : public BlockCondition {
|
|
|
|
friend class WaitBlocker;
|
2019-07-19 10:34:11 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
public:
|
|
|
|
WaitBlockCondition(Process& process)
|
|
|
|
: m_process(process)
|
2019-07-19 10:51:48 +03:00
|
|
|
{
|
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
|
2020-12-09 05:04:05 +03:00
|
|
|
void disowned_by_waiter(Process&);
|
2020-12-09 07:18:45 +03:00
|
|
|
bool unblock(Process&, WaitBlocker::UnblockFlags, u8);
|
2020-11-30 02:05:27 +03:00
|
|
|
void try_unblock(WaitBlocker&);
|
|
|
|
void finalize();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool should_add_blocker(Blocker&, void*) override;
|
2019-07-19 10:34:11 +03:00
|
|
|
|
|
|
|
private:
|
2020-12-09 07:18:45 +03:00
|
|
|
struct ProcessBlockInfo {
|
|
|
|
NonnullRefPtr<Process> process;
|
2020-11-30 02:05:27 +03:00
|
|
|
WaitBlocker::UnblockFlags flags;
|
|
|
|
u8 signal;
|
|
|
|
bool was_waited { false };
|
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
explicit ProcessBlockInfo(NonnullRefPtr<Process>&&, WaitBlocker::UnblockFlags, u8);
|
|
|
|
~ProcessBlockInfo();
|
2020-11-30 02:05:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Process& m_process;
|
2020-12-09 07:18:45 +03:00
|
|
|
Vector<ProcessBlockInfo, 2> m_processes;
|
2020-11-30 02:05:27 +03:00
|
|
|
bool m_finalized { false };
|
2019-07-19 10:34:11 +03:00
|
|
|
};
|
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
template<typename AddBlockerHandler>
|
|
|
|
KResult try_join(AddBlockerHandler add_blocker)
|
2020-11-30 02:05:27 +03:00
|
|
|
{
|
|
|
|
if (Thread::current() == this)
|
|
|
|
return KResult(-EDEADLK);
|
|
|
|
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (!m_is_joinable || state() == Dead)
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
add_blocker();
|
2020-11-30 02:05:27 +03:00
|
|
|
|
|
|
|
// From this point on the thread is no longer joinable by anyone
|
|
|
|
// else. It also means that if the join is timed, it becomes
|
|
|
|
// detached when a timeout happens.
|
|
|
|
m_is_joinable = false;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
void did_schedule() { ++m_times_scheduled; }
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 times_scheduled() const { return m_times_scheduled; }
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-08-14 19:24:31 +03:00
|
|
|
void resume_from_stopped();
|
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool should_be_stopped() const;
|
|
|
|
[[nodiscard]] bool is_stopped() const { return m_state == Stopped; }
|
|
|
|
[[nodiscard]] bool is_blocked() const { return m_state == Blocked; }
|
|
|
|
[[nodiscard]] bool is_in_block() const
|
2020-08-03 01:59:01 +03:00
|
|
|
{
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock lock(m_block_lock);
|
|
|
|
return m_in_block;
|
2020-08-03 01:59:01 +03:00
|
|
|
}
|
2020-04-07 18:23:37 +03:00
|
|
|
|
2020-06-28 07:36:15 +03:00
|
|
|
u32 cpu() const { return m_cpu.load(AK::MemoryOrder::memory_order_consume); }
|
|
|
|
void set_cpu(u32 cpu) { m_cpu.store(cpu, AK::MemoryOrder::memory_order_release); }
|
2020-06-29 00:34:31 +03:00
|
|
|
u32 affinity() const { return m_cpu_affinity; }
|
|
|
|
void set_affinity(u32 affinity) { m_cpu_affinity = affinity; }
|
2020-06-28 07:36:15 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 stack_ptr() const { return m_tss.esp; }
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-02-16 02:15:37 +03:00
|
|
|
RegisterState& get_register_dump_from_stack();
|
2019-11-02 12:11:41 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
TSS32& tss() { return m_tss; }
|
2019-07-25 22:02:19 +03:00
|
|
|
const TSS32& tss() const { return m_tss; }
|
2019-03-24 00:03:17 +03:00
|
|
|
State state() const { return m_state; }
|
2019-07-19 10:51:48 +03:00
|
|
|
const char* state_string() const;
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-09-07 16:50:44 +03:00
|
|
|
VirtualAddress thread_specific_data() const { return m_thread_specific_data; }
|
2020-12-25 18:45:35 +03:00
|
|
|
size_t thread_specific_region_size() const;
|
|
|
|
size_t thread_specific_region_alignment() const;
|
2019-09-07 16:50:44 +03:00
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
ALWAYS_INLINE void yield_if_stopped()
|
|
|
|
{
|
|
|
|
// If some thread stopped us, we need to yield to someone else
|
|
|
|
// We check this when entering/exiting a system call. A thread
|
|
|
|
// may continue to execute in user land until the next timer
|
|
|
|
// tick or entering the next system call, or if it's in kernel
|
|
|
|
// mode then we will intercept prior to returning back to user
|
|
|
|
// mode.
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
while (state() == Thread::Stopped) {
|
|
|
|
lock.unlock();
|
|
|
|
// We shouldn't be holding the big lock here
|
|
|
|
yield_while_not_holding_big_lock();
|
|
|
|
lock.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:14:54 +03:00
|
|
|
template<typename T, class... Args>
|
2020-11-15 21:58:19 +03:00
|
|
|
[[nodiscard]] BlockResult block(const BlockTimeout& timeout, Args&&... args)
|
2019-07-19 11:12:50 +03:00
|
|
|
{
|
2020-12-09 07:18:45 +03:00
|
|
|
ASSERT(!Processor::current().in_irq());
|
2020-12-15 02:36:22 +03:00
|
|
|
ASSERT(this == Thread::current());
|
2020-12-09 07:18:45 +03:00
|
|
|
ScopedCritical critical;
|
2020-12-01 23:02:54 +03:00
|
|
|
ScopedSpinLock scheduler_lock(g_scheduler_lock);
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock block_lock(m_block_lock);
|
|
|
|
// We need to hold m_block_lock so that nobody can unblock a blocker as soon
|
2020-11-30 02:05:27 +03:00
|
|
|
// as it is constructed and registered elsewhere
|
2020-12-12 06:19:45 +03:00
|
|
|
m_in_block = true;
|
2019-12-01 14:45:51 +03:00
|
|
|
T t(forward<Args>(args)...);
|
2020-08-03 01:59:01 +03:00
|
|
|
|
2021-01-04 02:58:50 +03:00
|
|
|
// Relaxed semantics are fine for timeout_unblocked because we
|
|
|
|
// synchronize on the spin locks already.
|
|
|
|
Atomic<bool, AK::MemoryOrder::memory_order_relaxed> timeout_unblocked(false);
|
2020-11-15 21:58:19 +03:00
|
|
|
RefPtr<Timer> timer;
|
2020-10-26 05:22:59 +03:00
|
|
|
{
|
2020-12-08 07:29:41 +03:00
|
|
|
switch (state()) {
|
|
|
|
case Thread::Stopped:
|
|
|
|
// It's possible that we were requested to be stopped!
|
|
|
|
break;
|
|
|
|
case Thread::Running:
|
|
|
|
ASSERT(m_blocker == nullptr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2020-08-03 01:59:01 +03:00
|
|
|
|
2020-11-15 21:58:19 +03:00
|
|
|
m_blocker = &t;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (!t.should_block()) {
|
2020-10-26 05:22:59 +03:00
|
|
|
// Don't block if the wake condition is already met
|
2020-11-30 02:05:27 +03:00
|
|
|
t.not_blocking(false);
|
2020-11-15 21:58:19 +03:00
|
|
|
m_blocker = nullptr;
|
2020-12-12 06:19:45 +03:00
|
|
|
m_in_block = false;
|
2020-10-26 05:22:59 +03:00
|
|
|
return BlockResult::NotBlocked;
|
|
|
|
}
|
2019-07-21 13:14:58 +03:00
|
|
|
|
2020-11-15 21:58:19 +03:00
|
|
|
auto& block_timeout = t.override_timeout(timeout);
|
|
|
|
if (!block_timeout.is_infinite()) {
|
2020-12-08 07:29:41 +03:00
|
|
|
// Process::kill_all_threads may be called at any time, which will mark all
|
|
|
|
// threads to die. In that case
|
2020-12-09 07:18:45 +03:00
|
|
|
timer = TimerQueue::the().add_timer_without_id(block_timeout.clock_id(), block_timeout.absolute_time(), [&]() {
|
|
|
|
ASSERT(!Processor::current().in_irq());
|
2020-12-08 07:29:41 +03:00
|
|
|
ASSERT(!g_scheduler_lock.own_lock());
|
|
|
|
ASSERT(!m_block_lock.own_lock());
|
2020-11-15 21:58:19 +03:00
|
|
|
// NOTE: this may execute on the same or any other processor!
|
2020-12-08 07:29:41 +03:00
|
|
|
ScopedSpinLock scheduler_lock(g_scheduler_lock);
|
|
|
|
ScopedSpinLock block_lock(m_block_lock);
|
2021-01-04 02:58:50 +03:00
|
|
|
if (m_blocker && timeout_unblocked.exchange(true) == false)
|
2020-12-08 07:29:41 +03:00
|
|
|
unblock();
|
2020-11-15 21:58:19 +03:00
|
|
|
});
|
2020-12-09 07:18:45 +03:00
|
|
|
if (!timer) {
|
2020-11-15 21:58:19 +03:00
|
|
|
// Timeout is already in the past
|
2020-11-30 02:05:27 +03:00
|
|
|
t.not_blocking(true);
|
2020-11-15 21:58:19 +03:00
|
|
|
m_blocker = nullptr;
|
2020-12-12 06:19:45 +03:00
|
|
|
m_in_block = false;
|
2020-11-15 21:58:19 +03:00
|
|
|
return BlockResult::InterruptedByTimeout;
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 06:44:43 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
t.begin_blocking({});
|
|
|
|
|
2020-10-26 05:22:59 +03:00
|
|
|
set_state(Thread::Blocked);
|
|
|
|
}
|
2020-09-26 06:44:43 +03:00
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
block_lock.unlock();
|
2019-07-21 13:14:58 +03:00
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
bool did_timeout = false;
|
2020-12-15 02:36:22 +03:00
|
|
|
auto previous_locked = LockMode::Unlocked;
|
|
|
|
u32 lock_count_to_restore = 0;
|
2020-12-09 07:18:45 +03:00
|
|
|
for (;;) {
|
|
|
|
scheduler_lock.unlock();
|
|
|
|
|
|
|
|
// Yield to the scheduler, and wait for us to resume unblocked.
|
2020-12-15 02:36:22 +03:00
|
|
|
if (previous_locked == LockMode::Unlocked)
|
|
|
|
previous_locked = unlock_process_if_locked(lock_count_to_restore);
|
2020-12-12 06:19:45 +03:00
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
ASSERT(!g_scheduler_lock.own_lock());
|
|
|
|
ASSERT(Processor::current().in_critical());
|
|
|
|
yield_while_not_holding_big_lock();
|
2020-12-08 07:29:41 +03:00
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
scheduler_lock.lock();
|
|
|
|
ScopedSpinLock block_lock2(m_block_lock);
|
|
|
|
if (should_be_stopped() || state() == Stopped) {
|
2021-01-09 02:42:44 +03:00
|
|
|
dbgln("Thread should be stopped, current state: {}", state_string());
|
2020-12-09 07:18:45 +03:00
|
|
|
set_state(Thread::Blocked);
|
|
|
|
continue;
|
2020-12-08 07:29:41 +03:00
|
|
|
}
|
2020-12-09 07:18:45 +03:00
|
|
|
if (m_blocker && !m_blocker->can_be_interrupted() && !m_should_die) {
|
|
|
|
block_lock2.unlock();
|
2021-01-09 02:42:44 +03:00
|
|
|
dbgln("Thread should not be unblocking, current state: ", state_string());
|
2020-12-09 07:18:45 +03:00
|
|
|
set_state(Thread::Blocked);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Prevent the timeout from unblocking this thread if it happens to
|
|
|
|
// be in the process of firing already
|
2021-01-04 02:58:50 +03:00
|
|
|
did_timeout |= timeout_unblocked.exchange(true);
|
2020-12-09 07:18:45 +03:00
|
|
|
if (m_blocker) {
|
|
|
|
// Remove ourselves...
|
|
|
|
ASSERT(m_blocker == &t);
|
|
|
|
m_blocker = nullptr;
|
|
|
|
}
|
2020-12-12 06:19:45 +03:00
|
|
|
m_in_block = false;
|
2020-12-09 07:18:45 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-09-26 06:44:43 +03:00
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
if (t.was_interrupted_by_signal()) {
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
dispatch_one_pending_signal();
|
2020-11-15 21:58:19 +03:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:02:54 +03:00
|
|
|
// Notify the blocker that we are no longer blocking. It may need
|
|
|
|
// to clean up now while we're still holding m_lock
|
|
|
|
auto result = t.end_blocking({}, did_timeout); // calls was_unblocked internally
|
|
|
|
|
2020-12-08 07:29:41 +03:00
|
|
|
scheduler_lock.unlock();
|
2020-11-15 21:58:19 +03:00
|
|
|
if (timer && !did_timeout) {
|
|
|
|
// Cancel the timer while not holding any locks. This allows
|
|
|
|
// the timer function to complete before we remove it
|
|
|
|
// (e.g. if it's on another processor)
|
|
|
|
TimerQueue::the().cancel_timer(timer.release_nonnull());
|
2020-12-09 07:18:45 +03:00
|
|
|
}
|
2020-12-15 02:36:22 +03:00
|
|
|
if (previous_locked != LockMode::Unlocked) {
|
2020-12-09 07:18:45 +03:00
|
|
|
// NOTE: this may trigger another call to Thread::block(), so
|
|
|
|
// we need to do this after we're all done and restored m_in_block!
|
2020-12-15 02:36:22 +03:00
|
|
|
relock_process(previous_locked, lock_count_to_restore);
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
return result;
|
2019-12-01 13:57:20 +03:00
|
|
|
}
|
2019-07-19 11:12:50 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
void unblock_from_blocker(Blocker&);
|
|
|
|
void unblock(u8 signal = 0);
|
2019-07-20 12:05:52 +03:00
|
|
|
|
2020-12-15 02:36:22 +03:00
|
|
|
template<class... Args>
|
|
|
|
Thread::BlockResult wait_on(WaitQueue& wait_queue, const Thread::BlockTimeout& timeout, Args&&... args)
|
|
|
|
{
|
|
|
|
ASSERT(this == Thread::current());
|
|
|
|
return block<Thread::QueueBlocker>(timeout, wait_queue, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:53:47 +03:00
|
|
|
BlockResult sleep(clockid_t, const timespec&, timespec* = nullptr);
|
|
|
|
BlockResult sleep(const timespec& duration, timespec* remaining_time = nullptr)
|
|
|
|
{
|
2020-12-04 08:12:50 +03:00
|
|
|
return sleep(CLOCK_MONOTONIC_COARSE, duration, remaining_time);
|
2020-12-02 02:53:47 +03:00
|
|
|
}
|
|
|
|
BlockResult sleep_until(clockid_t, const timespec&);
|
|
|
|
BlockResult sleep_until(const timespec& duration)
|
|
|
|
{
|
2020-12-04 08:12:50 +03:00
|
|
|
return sleep_until(CLOCK_MONOTONIC_COARSE, duration);
|
2020-12-02 02:53:47 +03:00
|
|
|
}
|
2020-11-15 21:58:19 +03:00
|
|
|
|
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
|
|
|
// Tell this thread to unblock if needed,
|
|
|
|
// gracefully unwind the stack and die.
|
|
|
|
void set_should_die();
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool should_die() const { return m_should_die; }
|
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
|
|
|
void die_if_needed();
|
|
|
|
|
2020-11-17 06:51:34 +03:00
|
|
|
void exit(void* = nullptr);
|
|
|
|
|
2020-12-04 08:12:50 +03:00
|
|
|
bool tick(bool in_kernel);
|
2019-07-03 22:17:35 +03:00
|
|
|
void set_ticks_left(u32 t) { m_ticks_left = t; }
|
|
|
|
u32 ticks_left() const { return m_ticks_left; }
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 kernel_stack_base() const { return m_kernel_stack_base; }
|
2019-09-04 05:31:38 +03:00
|
|
|
u32 kernel_stack_top() const { return m_kernel_stack_top; }
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-12-09 07:18:45 +03:00
|
|
|
void set_state(State, u8 = 0);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool is_initialized() const { return m_initialized; }
|
2020-06-27 22:42:28 +03:00
|
|
|
void set_initialized(bool initialized) { m_initialized = initialized; }
|
|
|
|
|
2019-10-07 12:22:50 +03:00
|
|
|
void send_urgent_signal_to_self(u8 signal);
|
2019-07-03 22:17:35 +03:00
|
|
|
void send_signal(u8 signal, Process* sender);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-09-09 05:37:15 +03:00
|
|
|
u32 update_signal_mask(u32 signal_mask);
|
|
|
|
u32 signal_mask_block(sigset_t signal_set, bool block);
|
|
|
|
u32 signal_mask() const;
|
|
|
|
void clear_signals();
|
|
|
|
|
2019-08-06 20:43:07 +03:00
|
|
|
void set_dump_backtrace_on_finalization() { m_dump_backtrace_on_finalization = true; }
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
DispatchSignalResult dispatch_one_pending_signal();
|
|
|
|
DispatchSignalResult try_dispatch_one_pending_signal(u8 signal);
|
|
|
|
DispatchSignalResult dispatch_signal(u8 signal);
|
2020-12-08 07:29:41 +03:00
|
|
|
void check_dispatch_pending_signal();
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool has_unmasked_pending_signals() const { return m_have_any_unmasked_pending_signals.load(AK::memory_order_consume); }
|
2019-07-03 22:17:35 +03:00
|
|
|
void terminate_due_to_signal(u8 signal);
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool should_ignore_signal(u8 signal) const;
|
|
|
|
[[nodiscard]] bool has_signal_handler(u8 signal) const;
|
|
|
|
[[nodiscard]] bool has_pending_signal(u8 signal) const;
|
2020-09-09 05:37:15 +03:00
|
|
|
u32 pending_signals() const;
|
2020-11-30 02:05:27 +03:00
|
|
|
u32 pending_signals_for_state() const;
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-03-27 17:27:45 +03:00
|
|
|
FPUState& fpu_state() { return *m_fpu_state; }
|
2019-03-24 00:03:17 +03:00
|
|
|
|
|
|
|
void set_default_signal_dispositions();
|
2020-09-12 06:11:07 +03:00
|
|
|
bool push_value_on_stack(FlatPtr);
|
2019-12-19 01:03:23 +03:00
|
|
|
|
2020-09-16 20:47:47 +03:00
|
|
|
KResult make_thread_specific_region(Badge<Process>);
|
2019-09-07 16:50:44 +03:00
|
|
|
|
2019-11-26 23:35:24 +03:00
|
|
|
unsigned syscall_count() const { return m_syscall_count; }
|
|
|
|
void did_syscall() { ++m_syscall_count; }
|
|
|
|
unsigned inode_faults() const { return m_inode_faults; }
|
|
|
|
void did_inode_fault() { ++m_inode_faults; }
|
|
|
|
unsigned zero_faults() const { return m_zero_faults; }
|
|
|
|
void did_zero_fault() { ++m_zero_faults; }
|
|
|
|
unsigned cow_faults() const { return m_cow_faults; }
|
|
|
|
void did_cow_fault() { ++m_cow_faults; }
|
|
|
|
|
2019-12-01 19:36:06 +03:00
|
|
|
unsigned file_read_bytes() const { return m_file_read_bytes; }
|
|
|
|
unsigned file_write_bytes() const { return m_file_write_bytes; }
|
|
|
|
|
|
|
|
void did_file_read(unsigned bytes)
|
|
|
|
{
|
|
|
|
m_file_read_bytes += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void did_file_write(unsigned bytes)
|
|
|
|
{
|
|
|
|
m_file_write_bytes += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned unix_socket_read_bytes() const { return m_unix_socket_read_bytes; }
|
|
|
|
unsigned unix_socket_write_bytes() const { return m_unix_socket_write_bytes; }
|
|
|
|
|
|
|
|
void did_unix_socket_read(unsigned bytes)
|
|
|
|
{
|
|
|
|
m_unix_socket_read_bytes += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void did_unix_socket_write(unsigned bytes)
|
|
|
|
{
|
|
|
|
m_unix_socket_write_bytes += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ipv4_socket_read_bytes() const { return m_ipv4_socket_read_bytes; }
|
|
|
|
unsigned ipv4_socket_write_bytes() const { return m_ipv4_socket_write_bytes; }
|
|
|
|
|
|
|
|
void did_ipv4_socket_read(unsigned bytes)
|
|
|
|
{
|
|
|
|
m_ipv4_socket_read_bytes += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void did_ipv4_socket_write(unsigned bytes)
|
|
|
|
{
|
|
|
|
m_ipv4_socket_write_bytes += bytes;
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:32:07 +03:00
|
|
|
void set_active(bool active)
|
|
|
|
{
|
2020-09-26 06:44:43 +03:00
|
|
|
m_is_active.store(active, AK::memory_order_release);
|
2020-07-05 23:32:07 +03:00
|
|
|
}
|
2020-12-26 12:47:08 +03:00
|
|
|
|
|
|
|
[[nodiscard]] bool is_active() const
|
2020-12-08 07:29:41 +03:00
|
|
|
{
|
|
|
|
return m_is_active.load(AK::MemoryOrder::memory_order_acquire);
|
|
|
|
}
|
2020-07-05 23:32:07 +03:00
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool is_finalizable() const
|
2020-07-05 23:32:07 +03:00
|
|
|
{
|
2020-09-26 06:44:43 +03:00
|
|
|
// We can't finalize as long as this thread is still running
|
|
|
|
// Note that checking for Running state here isn't sufficient
|
|
|
|
// as the thread may not be in Running state but switching out.
|
|
|
|
// m_is_active is set to false once the context switch is
|
|
|
|
// complete and the thread is not executing on any processor.
|
2020-12-08 07:29:41 +03:00
|
|
|
if (m_is_active.load(AK::memory_order_acquire))
|
2020-09-26 06:44:43 +03:00
|
|
|
return false;
|
|
|
|
// We can't finalize until the thread is either detached or
|
|
|
|
// a join has started. We can't make m_is_joinable atomic
|
|
|
|
// because that would introduce a race in try_join.
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return !m_is_joinable;
|
2020-07-05 23:32:07 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:53:35 +03:00
|
|
|
RefPtr<Thread> clone(Process&);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename Callback>
|
2019-07-19 13:16:00 +03:00
|
|
|
static IterationDecision for_each_in_state(State, Callback);
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename Callback>
|
2019-07-19 13:16:00 +03:00
|
|
|
static IterationDecision for_each_living(Callback);
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename Callback>
|
2019-07-19 13:16:00 +03:00
|
|
|
static IterationDecision for_each(Callback);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] static bool is_runnable_state(Thread::State state)
|
2019-05-18 19:31:36 +03:00
|
|
|
{
|
|
|
|
return state == Thread::State::Running || state == Thread::State::Runnable;
|
|
|
|
}
|
|
|
|
|
2019-10-20 19:11:40 +03:00
|
|
|
static constexpr u32 default_kernel_stack_size = 65536;
|
AK: Rename KB, MB, GB to KiB, MiB, GiB
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9".
The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30".
Let's use the correct name, at least in code.
Only changes the name of the constants, no other behavior change.
2020-08-15 20:55:00 +03:00
|
|
|
static constexpr u32 default_userspace_stack_size = 4 * MiB;
|
2019-10-20 19:11:40 +03:00
|
|
|
|
2020-12-04 08:12:50 +03:00
|
|
|
u32 ticks_in_user() const { return m_ticks_in_user; }
|
|
|
|
u32 ticks_in_kernel() const { return m_ticks_in_kernel; }
|
|
|
|
|
2020-08-03 01:59:01 +03:00
|
|
|
RecursiveSpinLock& get_lock() const { return m_lock; }
|
|
|
|
|
2020-12-01 05:04:36 +03:00
|
|
|
#ifdef LOCK_DEBUG
|
2020-12-15 02:36:22 +03:00
|
|
|
void holding_lock(Lock& lock, int refs_delta, const char* file = nullptr, int line = 0)
|
2020-12-01 05:04:36 +03:00
|
|
|
{
|
2020-12-15 02:36:22 +03:00
|
|
|
ASSERT(refs_delta != 0);
|
|
|
|
m_holding_locks.fetch_add(refs_delta, AK::MemoryOrder::memory_order_relaxed);
|
2020-12-01 05:04:36 +03:00
|
|
|
ScopedSpinLock list_lock(m_holding_locks_lock);
|
2020-12-15 02:36:22 +03:00
|
|
|
if (refs_delta > 0) {
|
2020-12-01 05:04:36 +03:00
|
|
|
bool have_existing = false;
|
|
|
|
for (size_t i = 0; i < m_holding_locks_list.size(); i++) {
|
|
|
|
auto& info = m_holding_locks_list[i];
|
|
|
|
if (info.lock == &lock) {
|
|
|
|
have_existing = true;
|
2020-12-15 02:36:22 +03:00
|
|
|
info.count += refs_delta;
|
2020-12-01 05:04:36 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!have_existing)
|
|
|
|
m_holding_locks_list.append({ &lock, file ? file : "unknown", line, 1 });
|
|
|
|
} else {
|
2020-12-15 02:36:22 +03:00
|
|
|
ASSERT(refs_delta < 0);
|
2020-12-01 05:04:36 +03:00
|
|
|
bool found = false;
|
|
|
|
for (size_t i = 0; i < m_holding_locks_list.size(); i++) {
|
|
|
|
auto& info = m_holding_locks_list[i];
|
|
|
|
if (info.lock == &lock) {
|
2020-12-15 02:36:22 +03:00
|
|
|
ASSERT(info.count >= (unsigned)-refs_delta);
|
|
|
|
info.count -= (unsigned)-refs_delta;
|
|
|
|
if (info.count == 0)
|
2020-12-01 05:04:36 +03:00
|
|
|
m_holding_locks_list.remove(i);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u32 lock_count() const
|
|
|
|
{
|
|
|
|
return m_holding_locks.load(AK::MemoryOrder::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-06 00:52:14 +03:00
|
|
|
bool was_created() const
|
|
|
|
{
|
|
|
|
return m_kernel_stack_region;
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:04:42 +03:00
|
|
|
private:
|
|
|
|
IntrusiveListNode m_runnable_list_node;
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
private:
|
2020-09-12 01:44:54 +03:00
|
|
|
friend struct SchedulerData;
|
2019-12-22 14:23:44 +03:00
|
|
|
friend class WaitQueue;
|
2020-11-30 02:05:27 +03:00
|
|
|
|
|
|
|
class JoinBlockCondition : public BlockCondition {
|
|
|
|
public:
|
|
|
|
void thread_did_exit(void* exit_value)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
ASSERT(!m_thread_did_exit);
|
|
|
|
m_thread_did_exit = true;
|
|
|
|
m_exit_value.store(exit_value, AK::MemoryOrder::memory_order_release);
|
|
|
|
do_unblock_joiner();
|
|
|
|
}
|
|
|
|
void thread_finalizing()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
do_unblock_joiner();
|
|
|
|
}
|
|
|
|
void* exit_value() const
|
|
|
|
{
|
|
|
|
ASSERT(m_thread_did_exit);
|
|
|
|
return m_exit_value.load(AK::MemoryOrder::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
void try_unblock(JoinBlocker& blocker)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (m_thread_did_exit)
|
|
|
|
blocker.unblock(exit_value(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool should_add_blocker(Blocker& b, void*) override
|
|
|
|
{
|
|
|
|
ASSERT(b.blocker_type() == Blocker::Type::Join);
|
|
|
|
auto& blocker = static_cast<JoinBlocker&>(b);
|
|
|
|
|
|
|
|
// NOTE: m_lock is held already!
|
2020-12-01 05:04:36 +03:00
|
|
|
if (m_thread_did_exit) {
|
2020-11-30 02:05:27 +03:00
|
|
|
blocker.unblock(exit_value(), true);
|
2020-12-01 05:04:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void do_unblock_joiner()
|
|
|
|
{
|
2020-12-22 21:17:56 +03:00
|
|
|
do_unblock([&](Blocker& b, void*, bool&) {
|
2020-11-30 02:05:27 +03:00
|
|
|
ASSERT(b.blocker_type() == Blocker::Type::Join);
|
|
|
|
auto& blocker = static_cast<JoinBlocker&>(b);
|
|
|
|
return blocker.unblock(exit_value(), false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Atomic<void*> m_exit_value { nullptr };
|
|
|
|
bool m_thread_did_exit { false };
|
|
|
|
};
|
|
|
|
|
2020-12-15 02:36:22 +03:00
|
|
|
LockMode unlock_process_if_locked(u32&);
|
|
|
|
void relock_process(LockMode, u32);
|
2020-07-03 21:12:34 +03:00
|
|
|
String backtrace_impl();
|
2020-02-18 15:44:27 +03:00
|
|
|
void reset_fpu_state();
|
|
|
|
|
2020-08-03 01:59:01 +03:00
|
|
|
mutable RecursiveSpinLock m_lock;
|
2020-12-08 07:29:41 +03:00
|
|
|
mutable RecursiveSpinLock m_block_lock;
|
2020-08-02 05:04:56 +03:00
|
|
|
NonnullRefPtr<Process> m_process;
|
2020-08-08 18:32:34 +03:00
|
|
|
ThreadID m_tid { -1 };
|
2019-03-24 00:03:17 +03:00
|
|
|
TSS32 m_tss;
|
2020-06-28 07:36:15 +03:00
|
|
|
Atomic<u32> m_cpu { 0 };
|
2020-06-29 00:34:31 +03:00
|
|
|
u32 m_cpu_affinity { THREAD_AFFINITY_DEFAULT };
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 m_ticks_left { 0 };
|
|
|
|
u32 m_times_scheduled { 0 };
|
2020-12-04 08:12:50 +03:00
|
|
|
u32 m_ticks_in_user { 0 };
|
|
|
|
u32 m_ticks_in_kernel { 0 };
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 m_pending_signals { 0 };
|
|
|
|
u32 m_signal_mask { 0 };
|
|
|
|
u32 m_kernel_stack_base { 0 };
|
2019-09-04 05:31:38 +03:00
|
|
|
u32 m_kernel_stack_top { 0 };
|
2019-09-27 15:19:07 +03:00
|
|
|
OwnPtr<Region> m_kernel_stack_region;
|
2019-09-07 16:50:44 +03:00
|
|
|
VirtualAddress m_thread_specific_data;
|
2019-03-24 00:03:17 +03:00
|
|
|
SignalActionData m_signal_action_data[32];
|
2019-09-09 06:58:42 +03:00
|
|
|
Blocker* m_blocker { nullptr };
|
2019-11-14 22:58:23 +03:00
|
|
|
|
2020-12-01 05:04:36 +03:00
|
|
|
#ifdef LOCK_DEBUG
|
|
|
|
struct HoldingLockInfo {
|
|
|
|
Lock* lock;
|
|
|
|
const char* file;
|
|
|
|
int line;
|
|
|
|
unsigned count;
|
|
|
|
};
|
|
|
|
Atomic<u32> m_holding_locks { 0 };
|
|
|
|
SpinLock<u8> m_holding_locks_lock;
|
|
|
|
Vector<HoldingLockInfo> m_holding_locks_list;
|
|
|
|
#endif
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
JoinBlockCondition m_join_condition;
|
2020-09-26 06:44:43 +03:00
|
|
|
Atomic<bool> m_is_active { false };
|
2019-11-18 06:08:10 +03:00
|
|
|
bool m_is_joinable { true };
|
2019-11-14 22:58:23 +03:00
|
|
|
|
2019-11-26 23:35:24 +03:00
|
|
|
unsigned m_syscall_count { 0 };
|
|
|
|
unsigned m_inode_faults { 0 };
|
|
|
|
unsigned m_zero_faults { 0 };
|
|
|
|
unsigned m_cow_faults { 0 };
|
|
|
|
|
2019-12-01 19:36:06 +03:00
|
|
|
unsigned m_file_read_bytes { 0 };
|
|
|
|
unsigned m_file_write_bytes { 0 };
|
|
|
|
|
|
|
|
unsigned m_unix_socket_read_bytes { 0 };
|
|
|
|
unsigned m_unix_socket_write_bytes { 0 };
|
|
|
|
|
|
|
|
unsigned m_ipv4_socket_read_bytes { 0 };
|
|
|
|
unsigned m_ipv4_socket_write_bytes { 0 };
|
|
|
|
|
2019-03-27 17:27:45 +03:00
|
|
|
FPUState* m_fpu_state { nullptr };
|
2019-04-20 20:29:48 +03:00
|
|
|
State m_state { Invalid };
|
2019-12-07 22:45:26 +03:00
|
|
|
String m_name;
|
2019-12-30 20:46:17 +03:00
|
|
|
u32 m_priority { THREAD_PRIORITY_NORMAL };
|
|
|
|
u32 m_extra_priority { 0 };
|
2020-01-27 22:47:10 +03:00
|
|
|
|
2020-03-01 17:14:17 +03:00
|
|
|
State m_stop_state { Invalid };
|
2020-01-27 22:47:10 +03:00
|
|
|
|
2019-08-06 20:43:07 +03:00
|
|
|
bool m_dump_backtrace_on_finalization { false };
|
Kernel: Unwind kernel stacks before dying
While executing in the kernel, a thread can acquire various resources
that need cleanup, such as locks and references to RefCounted objects.
This cleanup normally happens on the exit path, such as in destructors
for various RAII guards. But we weren't calling those exit paths when
killing threads that have been executing in the kernel, such as threads
blocked on reading or sleeping, thus causing leaks.
This commit changes how killing threads works. Now, instead of killing
a thread directly, one is supposed to call thread->set_should_die(),
which will unblock it and make it unwind the stack if it is blocked
in the kernel. Then, just before returning to the userspace, the thread
will automatically die.
2019-11-14 18:46:01 +03:00
|
|
|
bool m_should_die { false };
|
2020-07-05 02:37:36 +03:00
|
|
|
bool m_initialized { false };
|
2020-12-12 06:19:45 +03:00
|
|
|
bool m_in_block { false };
|
2020-09-07 17:31:00 +03:00
|
|
|
Atomic<bool> m_have_any_unmasked_pending_signals { false };
|
2019-07-19 10:57:35 +03:00
|
|
|
|
2019-11-16 14:18:59 +03:00
|
|
|
void yield_without_holding_big_lock();
|
2020-12-15 02:36:22 +03:00
|
|
|
void donate_without_holding_big_lock(RefPtr<Thread>&, const char*);
|
2020-12-08 07:29:41 +03:00
|
|
|
void yield_while_not_holding_big_lock();
|
2020-08-03 01:59:01 +03:00
|
|
|
void update_state_for_thread(Thread::State previous_state);
|
2021-01-01 08:45:16 +03:00
|
|
|
void drop_thread_count(bool);
|
2019-03-24 00:03:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-07-19 13:16:00 +03:00
|
|
|
inline IterationDecision Thread::for_each_living(Callback callback)
|
2019-03-24 00:03:17 +03:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-07-19 13:22:46 +03:00
|
|
|
return Thread::for_each([callback](Thread& thread) -> IterationDecision {
|
|
|
|
if (thread.state() != Thread::State::Dead && thread.state() != Thread::State::Dying)
|
|
|
|
return callback(thread);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-03-24 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-07-19 13:16:00 +03:00
|
|
|
inline IterationDecision Thread::for_each(Callback callback)
|
2019-03-24 00:03:17 +03:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2020-06-29 00:34:31 +03:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
2019-07-19 18:21:13 +03:00
|
|
|
auto ret = Scheduler::for_each_runnable(callback);
|
2019-07-19 13:16:00 +03:00
|
|
|
if (ret == IterationDecision::Break)
|
|
|
|
return ret;
|
2019-07-19 18:21:13 +03:00
|
|
|
return Scheduler::for_each_nonrunnable(callback);
|
2019-05-18 19:31:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2019-07-19 18:21:13 +03:00
|
|
|
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
|
2019-05-18 19:31:36 +03:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2020-06-29 00:34:31 +03:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
2019-07-19 18:21:13 +03:00
|
|
|
auto new_callback = [=](Thread& thread) -> IterationDecision {
|
|
|
|
if (thread.state() == state)
|
|
|
|
return callback(thread);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
};
|
|
|
|
if (is_runnable_state(state))
|
|
|
|
return Scheduler::for_each_runnable(new_callback);
|
|
|
|
return Scheduler::for_each_nonrunnable(new_callback);
|
2019-03-24 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2019-10-13 15:36:55 +03:00
|
|
|
const LogStream& operator<<(const LogStream&, const Thread&);
|
2019-08-07 21:43:54 +03:00
|
|
|
|
|
|
|
struct SchedulerData {
|
|
|
|
typedef IntrusiveList<Thread, &Thread::m_runnable_list_node> ThreadList;
|
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
ThreadList m_runnable_threads;
|
2019-08-07 21:43:54 +03:00
|
|
|
ThreadList m_nonrunnable_threads;
|
|
|
|
|
2020-08-03 01:59:01 +03:00
|
|
|
bool has_thread(Thread& thread) const
|
|
|
|
{
|
|
|
|
return m_runnable_threads.contains(thread) || m_nonrunnable_threads.contains(thread);
|
|
|
|
}
|
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
ThreadList& thread_list_for_state(Thread::State state)
|
2019-08-07 21:43:54 +03:00
|
|
|
{
|
|
|
|
if (Thread::is_runnable_state(state))
|
2019-12-30 20:46:17 +03:00
|
|
|
return m_runnable_threads;
|
2019-08-07 21:43:54 +03:00
|
|
|
return m_nonrunnable_threads;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline IterationDecision Scheduler::for_each_runnable(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2020-08-02 05:04:56 +03:00
|
|
|
ASSERT(g_scheduler_lock.own_lock());
|
2019-12-30 20:46:17 +03:00
|
|
|
auto& tl = g_scheduler_data->m_runnable_threads;
|
|
|
|
for (auto it = tl.begin(); it != tl.end();) {
|
|
|
|
auto& thread = *it;
|
|
|
|
it = ++it;
|
|
|
|
if (callback(thread) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
2019-08-07 21:43:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
inline IterationDecision Scheduler::for_each_nonrunnable(Callback callback)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2020-08-02 05:04:56 +03:00
|
|
|
ASSERT(g_scheduler_lock.own_lock());
|
2019-08-07 21:43:54 +03:00
|
|
|
auto& tl = g_scheduler_data->m_nonrunnable_threads;
|
|
|
|
for (auto it = tl.begin(); it != tl.end();) {
|
2019-08-17 12:25:32 +03:00
|
|
|
auto& thread = *it;
|
2019-08-07 21:43:54 +03:00
|
|
|
it = ++it;
|
2019-08-17 12:25:32 +03:00
|
|
|
if (callback(thread) == IterationDecision::Break)
|
2019-08-07 21:43:54 +03:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
2019-09-07 16:50:44 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|
2021-01-09 02:42:44 +03:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Kernel::Thread> : AK::Formatter<FormatString> {
|
|
|
|
void format(FormatBuilder&, const Kernel::Thread&);
|
|
|
|
};
|