2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-16 09:36:52 +00:00
|
|
|
#include <AK/Concepts.h>
|
2021-03-07 11:01:11 +00:00
|
|
|
#include <AK/EnumBits.h>
|
2019-07-14 12:51:54 +00:00
|
|
|
#include <AK/Function.h>
|
2021-01-28 05:58:24 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-07-19 11:04:42 +00:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-02-16 00:27:42 +00:00
|
|
|
#include <AK/Optional.h>
|
2019-04-20 17:23:45 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2021-04-24 22:17:02 +00:00
|
|
|
#include <AK/SourceLocation.h>
|
2019-10-20 16:11:40 +00:00
|
|
|
#include <AK/String.h>
|
2020-11-15 18:58:19 +00:00
|
|
|
#include <AK/Time.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-09-27 14:53:35 +00:00
|
|
|
#include <AK/WeakPtr.h>
|
|
|
|
#include <AK/Weakable.h>
|
2021-06-21 15:34:09 +00:00
|
|
|
#include <Kernel/Arch/x86/RegisterState.h>
|
2021-03-07 20:28:28 +00:00
|
|
|
#include <Kernel/Arch/x86/SafeMem.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
Kernel: Introduce the new ProcFS design
The new ProcFS design consists of two main parts:
1. The representative ProcFS class, which is derived from the FS class.
The ProcFS and its inodes are much more lean - merely 3 classes to
represent the common type of inodes - regular files, symbolic links and
directories. They're backed by a ProcFSExposedComponent object, which
is responsible for the functional operation behind the scenes.
2. The backend of the ProcFS - the ProcFSComponentsRegistrar class
and all derived classes from the ProcFSExposedComponent class. These
together form the entire backend and handle all the functions you can
expect from the ProcFS.
The ProcFSExposedComponent derived classes split to 3 types in the
manner of lifetime in the kernel:
1. Persistent objects - this category includes all basic objects, like
the root folder, /proc/bus folder, main blob files in the root folders,
etc. These objects are persistent and cannot die ever.
2. Semi-persistent objects - this category includes all PID folders,
and subdirectories to the PID folders. It also includes exposed objects
like the unveil JSON'ed blob. These object are persistent as long as the
the responsible process they represent is still alive.
3. Dynamic objects - this category includes files in the subdirectories
of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially,
these objects are always created dynamically and when no longer in need
after being used, they're deallocated.
Nevertheless, the new allocated backend objects and inodes try to use
the same InodeIndex if possible - this might change only when a thread
dies and a new thread is born with a new thread stack, or when a file
descriptor is closed and a new one within the same file descriptor
number is opened. This is needed to actually be able to do something
useful with these objects.
The new design assures that many ProcFS instances can be used at once,
with one backend for usage for all instances.
2021-06-12 01:23:58 +00:00
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
2020-02-16 00:50:16 +00:00
|
|
|
#include <Kernel/Forward.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/KResult.h>
|
2020-12-14 23:36:22 +00:00
|
|
|
#include <Kernel/LockMode.h>
|
2019-09-04 13:14:54 +00:00
|
|
|
#include <Kernel/Scheduler.h>
|
2020-04-03 11:50:17 +00:00
|
|
|
#include <Kernel/ThreadTracer.h>
|
2020-11-15 18:58:19 +00:00
|
|
|
#include <Kernel/TimerQueue.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2021-05-28 09:18:58 +00:00
|
|
|
#include <Kernel/VM/Range.h>
|
2019-07-19 07:04:12 +00:00
|
|
|
#include <LibC/fd_set.h>
|
2021-02-21 10:59:53 +00:00
|
|
|
#include <LibC/signal_numbers.h>
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-01-23 06:24:33 +00:00
|
|
|
extern RecursiveSpinLock s_mm_lock;
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
enum class DispatchSignalResult {
|
|
|
|
Deferred = 0,
|
|
|
|
Yield,
|
|
|
|
Terminate,
|
|
|
|
Continue
|
2019-05-28 09:53:16 +00:00
|
|
|
};
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
struct SignalActionData {
|
2019-06-07 10:56:50 +00:00
|
|
|
VirtualAddress handler_or_sigaction;
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 mask { 0 };
|
2019-03-23 21:03:17 +00:00
|
|
|
int flags { 0 };
|
|
|
|
};
|
|
|
|
|
2019-09-07 13:50:44 +00:00
|
|
|
struct ThreadSpecificData {
|
|
|
|
ThreadSpecificData* self;
|
|
|
|
};
|
|
|
|
|
2019-12-30 17:46:17 +00: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 15:26:51 +00:00
|
|
|
|
2020-06-28 21:34:31 +00:00
|
|
|
#define THREAD_AFFINITY_DEFAULT 0xffffffff
|
|
|
|
|
2021-06-26 17:57:16 +00:00
|
|
|
struct ThreadRegisters {
|
|
|
|
#if ARCH(I386)
|
|
|
|
FlatPtr ss;
|
|
|
|
FlatPtr gs;
|
|
|
|
FlatPtr fs;
|
|
|
|
FlatPtr es;
|
|
|
|
FlatPtr ds;
|
|
|
|
FlatPtr edi;
|
|
|
|
FlatPtr esi;
|
|
|
|
FlatPtr ebp;
|
|
|
|
FlatPtr esp;
|
|
|
|
FlatPtr ebx;
|
|
|
|
FlatPtr edx;
|
|
|
|
FlatPtr ecx;
|
|
|
|
FlatPtr eax;
|
|
|
|
FlatPtr eip;
|
|
|
|
FlatPtr esp0;
|
|
|
|
FlatPtr ss0;
|
|
|
|
#else
|
|
|
|
FlatPtr rdi;
|
|
|
|
FlatPtr rsi;
|
|
|
|
FlatPtr rbp;
|
|
|
|
FlatPtr rsp;
|
|
|
|
FlatPtr rbx;
|
|
|
|
FlatPtr rdx;
|
|
|
|
FlatPtr rcx;
|
|
|
|
FlatPtr rax;
|
|
|
|
FlatPtr r8;
|
|
|
|
FlatPtr r9;
|
|
|
|
FlatPtr r10;
|
|
|
|
FlatPtr r11;
|
|
|
|
FlatPtr r12;
|
|
|
|
FlatPtr r13;
|
|
|
|
FlatPtr r14;
|
|
|
|
FlatPtr r15;
|
|
|
|
FlatPtr rip;
|
|
|
|
FlatPtr rsp0;
|
|
|
|
#endif
|
|
|
|
FlatPtr cs;
|
|
|
|
#if ARCH(I386)
|
|
|
|
FlatPtr eflags;
|
|
|
|
#else
|
|
|
|
FlatPtr rflags;
|
|
|
|
#endif
|
|
|
|
FlatPtr cr3;
|
|
|
|
};
|
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
class Thread
|
|
|
|
: public RefCounted<Thread>
|
|
|
|
, public Weakable<Thread> {
|
2020-04-22 09:54:58 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(Thread);
|
|
|
|
AK_MAKE_NONMOVABLE(Thread);
|
|
|
|
|
2021-07-10 16:23:16 +00:00
|
|
|
friend class Lock;
|
2019-03-23 21:03:17 +00:00
|
|
|
friend class Process;
|
2021-03-11 13:12:55 +00:00
|
|
|
friend class ProtectedProcessBase;
|
2019-03-23 21:03:17 +00:00
|
|
|
friend class Scheduler;
|
2021-04-16 12:03:24 +00:00
|
|
|
friend struct ThreadReadyQueue;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2021-01-28 05:58:24 +00:00
|
|
|
static SpinLock<u8> g_tid_map_lock;
|
|
|
|
static HashMap<ThreadID, Thread*>* g_tid_map;
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
public:
|
2020-06-28 21:34:31 +00:00
|
|
|
inline static Thread* current()
|
|
|
|
{
|
2021-01-26 21:16:07 +00:00
|
|
|
return Processor::current_thread();
|
2020-06-28 21:34:31 +00:00
|
|
|
}
|
2020-02-17 14:04:27 +00:00
|
|
|
|
2021-01-28 05:58:24 +00:00
|
|
|
static void initialize();
|
|
|
|
|
2021-02-07 17:13:51 +00:00
|
|
|
static KResultOr<NonnullRefPtr<Thread>> try_create(NonnullRefPtr<Process>);
|
2019-03-23 21:03:17 +00:00
|
|
|
~Thread();
|
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
static RefPtr<Thread> from_tid(ThreadID);
|
2019-03-23 21:03:17 +00:00
|
|
|
static void finalize_dying_threads();
|
|
|
|
|
2020-08-08 15:32:34 +00:00
|
|
|
ThreadID tid() const { return m_tid; }
|
|
|
|
ProcessID pid() const;
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-12-30 17:46:17 +00:00
|
|
|
void set_priority(u32 p) { m_priority = p; }
|
|
|
|
u32 priority() const { return m_priority; }
|
|
|
|
|
2020-09-26 03:44:43 +00:00
|
|
|
void detach()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
m_is_joinable = false;
|
|
|
|
}
|
|
|
|
|
2020-12-26 09:47:08 +00:00
|
|
|
[[nodiscard]] bool is_joinable() const
|
2020-09-26 03:44:43 +00:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return m_is_joinable;
|
|
|
|
}
|
2019-11-18 03:08:10 +00:00
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
Process& process() { return m_process; }
|
|
|
|
const Process& process() const { return m_process; }
|
|
|
|
|
2020-09-27 14:53:35 +00: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 19:45:26 +00:00
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
void finalize();
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
enum State : u8 {
|
2019-03-23 21:03:17 +00:00
|
|
|
Invalid = 0,
|
|
|
|
Runnable,
|
|
|
|
Running,
|
|
|
|
Dying,
|
|
|
|
Dead,
|
|
|
|
Stopped,
|
2020-12-08 04:29:41 +00:00
|
|
|
Blocked
|
2019-03-23 21:03:17 +00:00
|
|
|
};
|
|
|
|
|
2021-02-14 23:06:10 +00:00
|
|
|
class [[nodiscard]] BlockResult {
|
2020-11-15 18:58:19 +00:00
|
|
|
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 09:47:08 +00:00
|
|
|
[[nodiscard]] bool was_interrupted() const
|
2020-11-15 18:58:19 +00:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case InterruptedBySignal:
|
|
|
|
case InterruptedByDeath:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 09:47:08 +00:00
|
|
|
[[nodiscard]] bool timed_out() const
|
2020-11-15 18:58:19 +00:00
|
|
|
{
|
|
|
|
return m_type == InterruptedByTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type m_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BlockTimeout {
|
|
|
|
public:
|
|
|
|
BlockTimeout()
|
|
|
|
: m_infinite(true)
|
|
|
|
{
|
|
|
|
}
|
2021-02-27 22:56:16 +00:00
|
|
|
explicit BlockTimeout(bool is_absolute, const Time* time, const Time* start_time = nullptr, clockid_t clock_id = CLOCK_MONOTONIC_COARSE);
|
2020-11-15 18:58:19 +00:00
|
|
|
|
2021-02-27 22:56:16 +00:00
|
|
|
const Time& absolute_time() const { return m_time; }
|
|
|
|
const Time* start_time() const { return !m_infinite ? &m_start_time : nullptr; }
|
2020-12-01 23:53:47 +00:00
|
|
|
clockid_t clock_id() const { return m_clock_id; }
|
2020-11-15 18:58:19 +00:00
|
|
|
bool is_infinite() const { return m_infinite; }
|
|
|
|
bool should_block() const { return m_infinite || m_should_block; };
|
|
|
|
|
|
|
|
private:
|
2021-02-27 22:56:16 +00:00
|
|
|
Time m_time {};
|
|
|
|
Time m_start_time {};
|
2020-12-04 05:12:50 +00:00
|
|
|
clockid_t m_clock_id { CLOCK_MONOTONIC_COARSE };
|
2020-11-15 18:58:19 +00:00
|
|
|
bool m_infinite { false };
|
|
|
|
bool m_should_block { false };
|
|
|
|
};
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
class BlockCondition;
|
|
|
|
|
2019-07-18 16:12:37 +00:00
|
|
|
class Blocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
enum class Type {
|
|
|
|
Unknown = 0,
|
|
|
|
File,
|
2020-12-22 06:21:58 +00:00
|
|
|
Futex,
|
2020-11-29 23:05:27 +00:00
|
|
|
Plan9FS,
|
|
|
|
Join,
|
2020-12-08 04:29:41 +00:00
|
|
|
Queue,
|
2020-11-29 23:05:27 +00:00
|
|
|
Routing,
|
|
|
|
Sleep,
|
|
|
|
Wait
|
|
|
|
};
|
|
|
|
virtual ~Blocker();
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const = 0;
|
2020-11-29 23:05:27 +00:00
|
|
|
virtual bool should_block() { return true; }
|
|
|
|
virtual Type blocker_type() const = 0;
|
2020-11-15 18:58:19 +00:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout& timeout) { return timeout; }
|
2020-12-09 04:18:45 +00:00
|
|
|
virtual bool can_be_interrupted() const { return true; }
|
2020-11-29 23:05:27 +00: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 03:44:43 +00:00
|
|
|
void set_interrupted_by_death()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-11-29 23:05:27 +00: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 03:44:43 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
u8 was_interrupted_by_signal() const
|
2020-09-26 03:44:43 +00:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-11-29 23:05:27 +00:00
|
|
|
return do_get_interrupted_by_signal();
|
2020-09-26 03:44:43 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
virtual Thread::BlockResult block_result()
|
2020-09-26 03:44:43 +00:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-11-15 18:58:19 +00:00
|
|
|
if (m_was_interrupted_by_death)
|
|
|
|
return Thread::BlockResult::InterruptedByDeath;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_was_interrupted_by_signal != 0)
|
2020-11-15 18:58:19 +00:00
|
|
|
return Thread::BlockResult::InterruptedBySignal;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (m_did_timeout)
|
2020-11-15 18:58:19 +00:00
|
|
|
return Thread::BlockResult::InterruptedByTimeout;
|
|
|
|
return Thread::BlockResult::WokeNormally;
|
2020-09-26 03:44:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
void begin_blocking(Badge<Thread>);
|
|
|
|
BlockResult end_blocking(Badge<Thread>, bool);
|
|
|
|
|
2020-09-26 03:44:43 +00:00
|
|
|
protected:
|
2020-11-29 23:05:27 +00:00
|
|
|
void do_set_interrupted_by_death()
|
|
|
|
{
|
|
|
|
m_was_interrupted_by_death = true;
|
|
|
|
}
|
|
|
|
void do_set_interrupted_by_signal(u8 signal)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(signal != 0);
|
2020-11-29 23:05:27 +00:00
|
|
|
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 09:47:08 +00:00
|
|
|
[[nodiscard]] bool was_interrupted() const
|
2020-11-29 23:05:27 +00: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;
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_blocked_thread);
|
2020-11-29 23:05:27 +00:00
|
|
|
thread = m_blocked_thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread)
|
|
|
|
thread->unblock_from_blocker(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set_block_condition(BlockCondition&, void* = nullptr);
|
2020-12-22 06:21:58 +00:00
|
|
|
void set_block_condition_raw_locked(BlockCondition* block_condition)
|
|
|
|
{
|
|
|
|
m_block_condition = block_condition;
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2020-09-26 03:44:43 +00:00
|
|
|
mutable RecursiveSpinLock m_lock;
|
2019-09-04 13:14:54 +00:00
|
|
|
|
2019-07-20 09:05:52 +00:00
|
|
|
private:
|
2020-11-29 23:05:27 +00: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 18:15:01 +00:00
|
|
|
bool m_was_interrupted_by_death { false };
|
2020-11-29 23:05:27 +00:00
|
|
|
bool m_did_timeout { false };
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
class BlockCondition {
|
|
|
|
AK_MAKE_NONCOPYABLE(BlockCondition);
|
|
|
|
AK_MAKE_NONMOVABLE(BlockCondition);
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlockCondition() = default;
|
|
|
|
|
|
|
|
virtual ~BlockCondition()
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_blockers.is_empty());
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 06:21:58 +00:00
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return is_empty_locked();
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
protected:
|
|
|
|
template<typename UnblockOne>
|
2020-12-22 18:17:56 +00:00
|
|
|
bool unblock(UnblockOne unblock_one)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-12-22 18:17:56 +00:00
|
|
|
return do_unblock(unblock_one);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename UnblockOne>
|
2020-12-22 18:17:56 +00:00
|
|
|
bool do_unblock(UnblockOne unblock_one)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_lock.is_locked());
|
2020-12-08 04:29:41 +00:00
|
|
|
bool stop_iterating = false;
|
2020-12-31 02:29:41 +00:00
|
|
|
bool did_unblock = false;
|
2020-12-08 04:29:41 +00: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 02:29:41 +00:00
|
|
|
did_unblock = true;
|
2020-12-08 04:29:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2020-12-31 02:29:41 +00:00
|
|
|
return did_unblock;
|
2020-12-08 04:29:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
bool is_empty_locked() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_lock.is_locked());
|
2020-12-22 06:21:58 +00:00
|
|
|
return m_blockers.is_empty();
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
virtual bool should_add_blocker(Blocker&, void*) { return true; }
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
struct BlockerInfo {
|
|
|
|
Blocker* blocker;
|
|
|
|
void* data;
|
|
|
|
};
|
2020-12-22 06:21:58 +00: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();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(move_count > 0);
|
2020-12-22 06:21:58 +00:00
|
|
|
|
|
|
|
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-29 23:05:27 +00:00
|
|
|
Vector<BlockerInfo, 4> m_blockers;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class JoinBlocker;
|
2019-11-14 19:58:23 +00:00
|
|
|
class JoinBlocker final : public Blocker {
|
|
|
|
public:
|
2020-09-26 03:44:43 +00:00
|
|
|
explicit JoinBlocker(Thread& joinee, KResult& try_join_result, void*& joinee_exit_value);
|
2020-11-29 23:05:27 +00:00
|
|
|
virtual Type blocker_type() const override { return Type::Join; }
|
2019-11-14 19:58:23 +00:00
|
|
|
virtual const char* state_string() const override { return "Joining"; }
|
2020-12-09 04:18:45 +00:00
|
|
|
virtual bool can_be_interrupted() const override { return false; }
|
2020-11-29 23:05:27 +00: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 19:58:23 +00:00
|
|
|
|
|
|
|
private:
|
2020-11-29 23:05:27 +00:00
|
|
|
NonnullRefPtr<Thread> m_joinee;
|
2019-11-14 20:04:34 +00:00
|
|
|
void*& m_joinee_exit_value;
|
2020-09-26 03:44:43 +00:00
|
|
|
bool m_join_error { false };
|
2020-11-29 23:05:27 +00:00
|
|
|
bool m_did_unblock { false };
|
|
|
|
bool m_should_block { true };
|
|
|
|
};
|
|
|
|
|
2020-12-08 04:29:41 +00: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 06:21:58 +00: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-29 23:05:27 +00:00
|
|
|
class FileBlocker : public Blocker {
|
|
|
|
public:
|
2021-03-03 19:21:24 +00:00
|
|
|
enum class BlockFlags : u16 {
|
2020-11-29 23:05:27 +00:00
|
|
|
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 19:58:23 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
class FileDescriptionBlocker : public FileBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2019-07-19 11:19:47 +00:00
|
|
|
const FileDescription& blocked_description() const;
|
2019-07-18 14:22:26 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
virtual bool unblock(bool, void*) override;
|
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
|
2019-07-19 11:32:56 +00:00
|
|
|
protected:
|
2020-11-29 23:05:27 +00:00
|
|
|
explicit FileDescriptionBlocker(FileDescription&, BlockFlags, BlockFlags&);
|
2019-07-19 11:32:56 +00:00
|
|
|
|
2019-07-18 14:22:26 +00:00
|
|
|
private:
|
2019-07-19 11:19:47 +00:00
|
|
|
NonnullRefPtr<FileDescription> m_blocked_description;
|
2020-11-29 23:05:27 +00:00
|
|
|
const BlockFlags m_flags;
|
|
|
|
BlockFlags& m_unblocked_flags;
|
|
|
|
bool m_did_unblock { false };
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class AcceptBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
explicit AcceptBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Accepting"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class ConnectBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
explicit ConnectBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Connecting"; }
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class WriteBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
explicit WriteBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Writing"; }
|
2020-11-15 18:58:19 +00:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2020-01-26 16:54:23 +00:00
|
|
|
private:
|
2020-11-15 18:58:19 +00:00
|
|
|
BlockTimeout m_timeout;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class ReadBlocker final : public FileDescriptionBlocker {
|
2019-07-18 14:22:26 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
explicit ReadBlocker(FileDescription&, BlockFlags&);
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Reading"; }
|
2020-11-15 18:58:19 +00:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2020-01-26 16:54:23 +00:00
|
|
|
private:
|
2020-11-15 18:58:19 +00:00
|
|
|
BlockTimeout m_timeout;
|
2019-07-18 14:22:26 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class SleepBlocker final : public Blocker {
|
2019-07-18 15:26:11 +00:00
|
|
|
public:
|
2021-02-27 22:56:16 +00:00
|
|
|
explicit SleepBlocker(const BlockTimeout&, Time* = nullptr);
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Sleeping"; }
|
2020-11-29 23:05:27 +00:00
|
|
|
virtual Type blocker_type() const override { return Type::Sleep; }
|
2020-11-15 18:58:19 +00:00
|
|
|
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
|
2020-11-29 23:05:27 +00:00
|
|
|
virtual void not_blocking(bool) override;
|
|
|
|
virtual void was_unblocked(bool) override;
|
|
|
|
virtual Thread::BlockResult block_result() override;
|
2019-07-18 15:26:11 +00:00
|
|
|
|
|
|
|
private:
|
2020-11-29 23:05:27 +00:00
|
|
|
void calculate_remaining();
|
|
|
|
|
2020-11-15 18:58:19 +00:00
|
|
|
BlockTimeout m_deadline;
|
2021-02-27 22:56:16 +00:00
|
|
|
Time* m_remaining;
|
2019-07-18 15:26:11 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
class SelectBlocker final : public FileBlocker {
|
2019-07-18 15:39:49 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
struct FDInfo {
|
|
|
|
NonnullRefPtr<FileDescription> description;
|
2021-03-03 19:21:24 +00:00
|
|
|
BlockFlags block_flags { BlockFlags::None };
|
2020-11-29 23:05:27 +00:00
|
|
|
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 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Selecting"; }
|
2019-07-18 15:39:49 +00:00
|
|
|
|
|
|
|
private:
|
2020-11-29 23:05:27 +00:00
|
|
|
size_t collect_unblocked_flags();
|
|
|
|
|
|
|
|
FDVector& m_fds;
|
|
|
|
bool m_did_unblock { false };
|
2019-07-18 15:39:49 +00:00
|
|
|
};
|
|
|
|
|
2019-07-19 11:19:47 +00:00
|
|
|
class WaitBlocker final : public Blocker {
|
2019-07-18 16:05:19 +00:00
|
|
|
public:
|
2020-11-29 23:05:27 +00:00
|
|
|
enum class UnblockFlags {
|
|
|
|
Terminated,
|
|
|
|
Stopped,
|
2020-12-09 02:04:05 +00:00
|
|
|
Continued,
|
|
|
|
Disowned
|
2020-11-29 23:05:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WaitBlocker(int wait_options, idtype_t id_type, pid_t id, KResultOr<siginfo_t>& result);
|
2019-07-19 07:51:48 +00:00
|
|
|
virtual const char* state_string() const override { return "Waiting"; }
|
2020-11-29 23:05:27 +00: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 04:18:45 +00:00
|
|
|
bool unblock(Process& process, UnblockFlags flags, u8 signal, bool from_add_blocker);
|
2020-11-29 23:05:27 +00:00
|
|
|
bool is_wait() const { return !(m_wait_options & WNOWAIT); }
|
2019-07-18 16:05:19 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-09 02:04:05 +00:00
|
|
|
void do_was_disowned();
|
2020-11-29 23:05:27 +00: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 16:05:19 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
class WaitBlockCondition final : public BlockCondition {
|
|
|
|
friend class WaitBlocker;
|
2019-07-19 07:34:11 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
public:
|
|
|
|
WaitBlockCondition(Process& process)
|
|
|
|
: m_process(process)
|
2019-07-19 07:51:48 +00:00
|
|
|
{
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2020-12-09 02:04:05 +00:00
|
|
|
void disowned_by_waiter(Process&);
|
2020-12-09 04:18:45 +00:00
|
|
|
bool unblock(Process&, WaitBlocker::UnblockFlags, u8);
|
2020-11-29 23:05:27 +00:00
|
|
|
void try_unblock(WaitBlocker&);
|
|
|
|
void finalize();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool should_add_blocker(Blocker&, void*) override;
|
2019-07-19 07:34:11 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-09 04:18:45 +00:00
|
|
|
struct ProcessBlockInfo {
|
|
|
|
NonnullRefPtr<Process> process;
|
2020-11-29 23:05:27 +00:00
|
|
|
WaitBlocker::UnblockFlags flags;
|
|
|
|
u8 signal;
|
|
|
|
bool was_waited { false };
|
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
explicit ProcessBlockInfo(NonnullRefPtr<Process>&&, WaitBlocker::UnblockFlags, u8);
|
|
|
|
~ProcessBlockInfo();
|
2020-11-29 23:05:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Process& m_process;
|
2020-12-09 04:18:45 +00:00
|
|
|
Vector<ProcessBlockInfo, 2> m_processes;
|
2020-11-29 23:05:27 +00:00
|
|
|
bool m_finalized { false };
|
2019-07-19 07:34:11 +00:00
|
|
|
};
|
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
template<typename AddBlockerHandler>
|
|
|
|
KResult try_join(AddBlockerHandler add_blocker)
|
2020-11-29 23:05:27 +00:00
|
|
|
{
|
|
|
|
if (Thread::current() == this)
|
2021-01-20 22:11:17 +00:00
|
|
|
return EDEADLK;
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (!m_is_joinable || state() == Dead)
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINVAL;
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2020-12-08 04:29:41 +00:00
|
|
|
add_blocker();
|
2020-11-29 23:05:27 +00: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-23 21:03:17 +00:00
|
|
|
void did_schedule() { ++m_times_scheduled; }
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 times_scheduled() const { return m_times_scheduled; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2020-08-14 16:24:31 +00:00
|
|
|
void resume_from_stopped();
|
|
|
|
|
2020-12-26 09:47:08 +00: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-02 22:59:01 +00:00
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock lock(m_block_lock);
|
|
|
|
return m_in_block;
|
2020-08-02 22:59:01 +00:00
|
|
|
}
|
2020-04-07 15:23:37 +00:00
|
|
|
|
2020-06-28 04:36:15 +00: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-28 21:34:31 +00:00
|
|
|
u32 affinity() const { return m_cpu_affinity; }
|
|
|
|
void set_affinity(u32 affinity) { m_cpu_affinity = affinity; }
|
2020-06-28 04:36:15 +00:00
|
|
|
|
2020-02-15 23:15:37 +00:00
|
|
|
RegisterState& get_register_dump_from_stack();
|
2021-01-28 07:41:18 +00:00
|
|
|
const RegisterState& get_register_dump_from_stack() const { return const_cast<Thread*>(this)->get_register_dump_from_stack(); }
|
2019-11-02 09:11:41 +00:00
|
|
|
|
2021-04-15 16:29:00 +00:00
|
|
|
DebugRegisterState& debug_register_state() { return m_debug_register_state; }
|
|
|
|
const DebugRegisterState& debug_register_state() const { return m_debug_register_state; }
|
|
|
|
|
2021-06-26 17:57:16 +00:00
|
|
|
ThreadRegisters& regs() { return m_regs; }
|
|
|
|
ThreadRegisters const& regs() const { return m_regs; }
|
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
State state() const { return m_state; }
|
2019-07-19 07:51:48 +00:00
|
|
|
const char* state_string() const;
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-09-07 13:50:44 +00:00
|
|
|
VirtualAddress thread_specific_data() const { return m_thread_specific_data; }
|
2020-12-25 15:45:35 +00:00
|
|
|
size_t thread_specific_region_size() const;
|
|
|
|
size_t thread_specific_region_alignment() const;
|
2019-09-07 13:50:44 +00:00
|
|
|
|
2020-12-08 04:29:41 +00: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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:23:16 +00:00
|
|
|
void block(Kernel::Lock&, ScopedSpinLock<SpinLock<u8>>&, u32);
|
|
|
|
|
2021-03-30 08:59:38 +00:00
|
|
|
template<typename BlockerType, class... Args>
|
2020-11-15 18:58:19 +00:00
|
|
|
[[nodiscard]] BlockResult block(const BlockTimeout& timeout, Args&&... args)
|
2019-07-19 08:12:50 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!Processor::current().in_irq());
|
|
|
|
VERIFY(this == Thread::current());
|
2020-12-09 04:18:45 +00:00
|
|
|
ScopedCritical critical;
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!s_mm_lock.own_lock());
|
2021-01-23 18:08:34 +00:00
|
|
|
|
2020-12-08 04:29:41 +00: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-29 23:05:27 +00:00
|
|
|
// as it is constructed and registered elsewhere
|
2020-12-12 03:19:45 +00:00
|
|
|
m_in_block = true;
|
2021-03-30 08:59:38 +00:00
|
|
|
BlockerType blocker(forward<Args>(args)...);
|
2020-08-02 22:59:01 +00:00
|
|
|
|
2021-01-23 18:08:34 +00:00
|
|
|
ScopedSpinLock scheduler_lock(g_scheduler_lock);
|
2021-01-03 23:58:50 +00: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);
|
2021-05-19 22:41:51 +00:00
|
|
|
bool timer_was_added = false;
|
2020-10-26 02:22:59 +00:00
|
|
|
{
|
2020-12-08 04:29:41 +00:00
|
|
|
switch (state()) {
|
|
|
|
case Thread::Stopped:
|
|
|
|
// It's possible that we were requested to be stopped!
|
|
|
|
break;
|
|
|
|
case Thread::Running:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_blocker == nullptr);
|
2020-12-08 04:29:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-12-08 04:29:41 +00:00
|
|
|
}
|
2020-08-02 22:59:01 +00:00
|
|
|
|
2021-03-30 08:59:38 +00:00
|
|
|
m_blocker = &blocker;
|
|
|
|
if (!blocker.should_block()) {
|
2020-10-26 02:22:59 +00:00
|
|
|
// Don't block if the wake condition is already met
|
2021-03-30 08:59:38 +00:00
|
|
|
blocker.not_blocking(false);
|
2020-11-15 18:58:19 +00:00
|
|
|
m_blocker = nullptr;
|
2020-12-12 03:19:45 +00:00
|
|
|
m_in_block = false;
|
2020-10-26 02:22:59 +00:00
|
|
|
return BlockResult::NotBlocked;
|
|
|
|
}
|
2019-07-21 10:14:58 +00:00
|
|
|
|
2021-03-30 08:59:38 +00:00
|
|
|
auto& block_timeout = blocker.override_timeout(timeout);
|
2020-11-15 18:58:19 +00:00
|
|
|
if (!block_timeout.is_infinite()) {
|
2020-12-08 04:29:41 +00:00
|
|
|
// Process::kill_all_threads may be called at any time, which will mark all
|
|
|
|
// threads to die. In that case
|
2021-05-19 22:41:51 +00:00
|
|
|
timer_was_added = TimerQueue::the().add_timer_without_id(*m_block_timer, block_timeout.clock_id(), block_timeout.absolute_time(), [&]() {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!Processor::current().in_irq());
|
|
|
|
VERIFY(!g_scheduler_lock.own_lock());
|
|
|
|
VERIFY(!m_block_lock.own_lock());
|
2020-11-15 18:58:19 +00:00
|
|
|
// NOTE: this may execute on the same or any other processor!
|
2020-12-08 04:29:41 +00:00
|
|
|
ScopedSpinLock scheduler_lock(g_scheduler_lock);
|
|
|
|
ScopedSpinLock block_lock(m_block_lock);
|
2021-01-03 23:58:50 +00:00
|
|
|
if (m_blocker && timeout_unblocked.exchange(true) == false)
|
2020-12-08 04:29:41 +00:00
|
|
|
unblock();
|
2020-11-15 18:58:19 +00:00
|
|
|
});
|
2021-05-19 22:41:51 +00:00
|
|
|
if (!timer_was_added) {
|
2020-11-15 18:58:19 +00:00
|
|
|
// Timeout is already in the past
|
2021-03-30 08:59:38 +00:00
|
|
|
blocker.not_blocking(true);
|
2020-11-15 18:58:19 +00:00
|
|
|
m_blocker = nullptr;
|
2020-12-12 03:19:45 +00:00
|
|
|
m_in_block = false;
|
2020-11-15 18:58:19 +00:00
|
|
|
return BlockResult::InterruptedByTimeout;
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 03:44:43 +00:00
|
|
|
|
2021-03-30 08:59:38 +00:00
|
|
|
blocker.begin_blocking({});
|
2020-11-29 23:05:27 +00:00
|
|
|
|
2020-10-26 02:22:59 +00:00
|
|
|
set_state(Thread::Blocked);
|
|
|
|
}
|
2020-09-26 03:44:43 +00:00
|
|
|
|
2021-01-23 18:08:34 +00:00
|
|
|
scheduler_lock.unlock();
|
2020-12-08 04:29:41 +00:00
|
|
|
block_lock.unlock();
|
2019-07-21 10:14:58 +00:00
|
|
|
|
2021-03-30 08:59:38 +00:00
|
|
|
dbgln_if(THREAD_DEBUG, "Thread {} blocking on {} ({}) -->", *this, &blocker, blocker.state_string());
|
2020-12-09 04:18:45 +00:00
|
|
|
bool did_timeout = false;
|
2020-12-14 23:36:22 +00:00
|
|
|
u32 lock_count_to_restore = 0;
|
2021-01-23 18:08:34 +00:00
|
|
|
auto previous_locked = unlock_process_if_locked(lock_count_to_restore);
|
2020-12-09 04:18:45 +00:00
|
|
|
for (;;) {
|
|
|
|
// Yield to the scheduler, and wait for us to resume unblocked.
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!g_scheduler_lock.own_lock());
|
|
|
|
VERIFY(Processor::current().in_critical());
|
2020-12-09 04:18:45 +00:00
|
|
|
yield_while_not_holding_big_lock();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(Processor::current().in_critical());
|
2020-12-08 04:29:41 +00:00
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
ScopedSpinLock block_lock2(m_block_lock);
|
|
|
|
if (should_be_stopped() || state() == Stopped) {
|
2021-01-08 23:42:44 +00:00
|
|
|
dbgln("Thread should be stopped, current state: {}", state_string());
|
2020-12-09 04:18:45 +00:00
|
|
|
set_state(Thread::Blocked);
|
|
|
|
continue;
|
2020-12-08 04:29:41 +00:00
|
|
|
}
|
2020-12-09 04:18:45 +00:00
|
|
|
if (m_blocker && !m_blocker->can_be_interrupted() && !m_should_die) {
|
|
|
|
block_lock2.unlock();
|
2021-01-17 09:02:26 +00:00
|
|
|
dbgln("Thread should not be unblocking, current state: {}", state_string());
|
2020-12-09 04:18:45 +00: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-03 23:58:50 +00:00
|
|
|
did_timeout |= timeout_unblocked.exchange(true);
|
2020-12-09 04:18:45 +00:00
|
|
|
if (m_blocker) {
|
|
|
|
// Remove ourselves...
|
2021-03-30 08:59:38 +00:00
|
|
|
VERIFY(m_blocker == &blocker);
|
2020-12-09 04:18:45 +00:00
|
|
|
m_blocker = nullptr;
|
|
|
|
}
|
2021-03-30 08:59:38 +00:00
|
|
|
dbgln_if(THREAD_DEBUG, "<-- Thread {} unblocked from {} ({})", *this, &blocker, blocker.state_string());
|
2020-12-12 03:19:45 +00:00
|
|
|
m_in_block = false;
|
2020-12-09 04:18:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-09-26 03:44:43 +00:00
|
|
|
|
2021-03-30 08:59:38 +00:00
|
|
|
if (blocker.was_interrupted_by_signal()) {
|
2021-01-23 18:08:34 +00:00
|
|
|
ScopedSpinLock scheduler_lock(g_scheduler_lock);
|
2020-12-09 04:18:45 +00:00
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
dispatch_one_pending_signal();
|
2020-11-15 18:58:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 20:02:54 +00:00
|
|
|
// Notify the blocker that we are no longer blocking. It may need
|
|
|
|
// to clean up now while we're still holding m_lock
|
2021-03-30 08:59:38 +00:00
|
|
|
auto result = blocker.end_blocking({}, did_timeout); // calls was_unblocked internally
|
2020-12-01 20:02:54 +00:00
|
|
|
|
2021-05-19 22:41:51 +00:00
|
|
|
if (timer_was_added && !did_timeout) {
|
2020-11-15 18:58:19 +00:00
|
|
|
// 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)
|
2021-05-19 22:41:51 +00:00
|
|
|
TimerQueue::the().cancel_timer(*m_block_timer);
|
2020-12-09 04:18:45 +00:00
|
|
|
}
|
2020-12-14 23:36:22 +00:00
|
|
|
if (previous_locked != LockMode::Unlocked) {
|
2020-12-09 04:18:45 +00: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-14 23:36:22 +00:00
|
|
|
relock_process(previous_locked, lock_count_to_restore);
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
return result;
|
2019-12-01 10:57:20 +00:00
|
|
|
}
|
2019-07-19 08:12:50 +00:00
|
|
|
|
2021-07-10 16:23:16 +00:00
|
|
|
u32 unblock_from_lock(Kernel::Lock&);
|
2020-11-29 23:05:27 +00:00
|
|
|
void unblock_from_blocker(Blocker&);
|
|
|
|
void unblock(u8 signal = 0);
|
2019-07-20 09:05:52 +00:00
|
|
|
|
2020-12-14 23:36:22 +00:00
|
|
|
template<class... Args>
|
|
|
|
Thread::BlockResult wait_on(WaitQueue& wait_queue, const Thread::BlockTimeout& timeout, Args&&... args)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(this == Thread::current());
|
2020-12-14 23:36:22 +00:00
|
|
|
return block<Thread::QueueBlocker>(timeout, wait_queue, forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2021-02-27 22:56:16 +00:00
|
|
|
BlockResult sleep(clockid_t, const Time&, Time* = nullptr);
|
|
|
|
BlockResult sleep(const Time& duration, Time* remaining_time = nullptr)
|
2020-12-01 23:53:47 +00:00
|
|
|
{
|
2020-12-04 05:12:50 +00:00
|
|
|
return sleep(CLOCK_MONOTONIC_COARSE, duration, remaining_time);
|
2020-12-01 23:53:47 +00:00
|
|
|
}
|
2021-02-27 22:56:16 +00:00
|
|
|
BlockResult sleep_until(clockid_t, const Time&);
|
|
|
|
BlockResult sleep_until(const Time& duration)
|
2020-12-01 23:53:47 +00:00
|
|
|
{
|
2020-12-04 05:12:50 +00:00
|
|
|
return sleep_until(CLOCK_MONOTONIC_COARSE, duration);
|
2020-12-01 23:53:47 +00:00
|
|
|
}
|
2020-11-15 18:58:19 +00: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 15:46:01 +00:00
|
|
|
// Tell this thread to unblock if needed,
|
|
|
|
// gracefully unwind the stack and die.
|
|
|
|
void set_should_die();
|
2020-12-26 09:47:08 +00: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 15:46:01 +00:00
|
|
|
void die_if_needed();
|
|
|
|
|
2020-11-17 03:51:34 +00:00
|
|
|
void exit(void* = nullptr);
|
|
|
|
|
2021-01-25 23:37:36 +00:00
|
|
|
bool tick();
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_ticks_left(u32 t) { m_ticks_left = t; }
|
|
|
|
u32 ticks_left() const { return m_ticks_left; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 kernel_stack_base() const { return m_kernel_stack_base; }
|
2019-09-04 02:31:38 +00:00
|
|
|
u32 kernel_stack_top() const { return m_kernel_stack_top; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2020-12-09 04:18:45 +00:00
|
|
|
void set_state(State, u8 = 0);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2020-12-26 09:47:08 +00:00
|
|
|
[[nodiscard]] bool is_initialized() const { return m_initialized; }
|
2020-06-27 19:42:28 +00:00
|
|
|
void set_initialized(bool initialized) { m_initialized = initialized; }
|
|
|
|
|
2019-10-07 09:22:50 +00:00
|
|
|
void send_urgent_signal_to_self(u8 signal);
|
2019-07-03 19:17:35 +00:00
|
|
|
void send_signal(u8 signal, Process* sender);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2020-09-09 02:37:15 +00: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();
|
|
|
|
|
2021-04-15 16:34:51 +00:00
|
|
|
KResultOr<u32> peek_debug_register(u32 register_index);
|
|
|
|
KResult poke_debug_register(u32 register_index, u32 data);
|
|
|
|
|
2019-08-06 17:43:07 +00:00
|
|
|
void set_dump_backtrace_on_finalization() { m_dump_backtrace_on_finalization = true; }
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
DispatchSignalResult dispatch_one_pending_signal();
|
|
|
|
DispatchSignalResult try_dispatch_one_pending_signal(u8 signal);
|
|
|
|
DispatchSignalResult dispatch_signal(u8 signal);
|
2020-12-08 04:29:41 +00:00
|
|
|
void check_dispatch_pending_signal();
|
2020-12-26 09:47:08 +00:00
|
|
|
[[nodiscard]] bool has_unmasked_pending_signals() const { return m_have_any_unmasked_pending_signals.load(AK::memory_order_consume); }
|
|
|
|
[[nodiscard]] bool should_ignore_signal(u8 signal) const;
|
|
|
|
[[nodiscard]] bool has_signal_handler(u8 signal) const;
|
2020-09-09 02:37:15 +00:00
|
|
|
u32 pending_signals() const;
|
2020-11-29 23:05:27 +00:00
|
|
|
u32 pending_signals_for_state() const;
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-03-27 14:27:45 +00:00
|
|
|
FPUState& fpu_state() { return *m_fpu_state; }
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2020-09-16 17:47:47 +00:00
|
|
|
KResult make_thread_specific_region(Badge<Process>);
|
2019-09-07 13:50:44 +00:00
|
|
|
|
2019-11-26 20:35:24 +00: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 16:36:06 +00: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;
|
|
|
|
}
|
|
|
|
|
2021-01-23 06:24:33 +00:00
|
|
|
void set_active(bool active) { m_is_active = active; }
|
2020-12-26 09:47:08 +00:00
|
|
|
|
2021-01-24 05:24:10 +00:00
|
|
|
u32 saved_critical() const { return m_saved_critical; }
|
|
|
|
void save_critical(u32 critical) { m_saved_critical = critical; }
|
|
|
|
|
2021-01-23 06:24:33 +00:00
|
|
|
[[nodiscard]] bool is_active() const { return m_is_active; }
|
2020-07-05 20:32:07 +00:00
|
|
|
|
2020-12-26 09:47:08 +00:00
|
|
|
[[nodiscard]] bool is_finalizable() const
|
2020-07-05 20:32:07 +00:00
|
|
|
{
|
2020-09-26 03:44:43 +00: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 04:29:41 +00:00
|
|
|
if (m_is_active.load(AK::memory_order_acquire))
|
2020-09-26 03:44:43 +00: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 20:32:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 14:53:35 +00:00
|
|
|
RefPtr<Thread> clone(Process&);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2021-05-16 09:36:52 +00:00
|
|
|
template<IteratorFunction<Thread&> Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
static IterationDecision for_each_in_state(State, Callback);
|
2021-05-16 09:36:52 +00:00
|
|
|
template<IteratorFunction<Thread&> Callback>
|
|
|
|
static IterationDecision for_each(Callback);
|
|
|
|
|
|
|
|
template<VoidFunction<Thread&> Callback>
|
|
|
|
static IterationDecision for_each_in_state(State, Callback);
|
|
|
|
template<VoidFunction<Thread&> Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
static IterationDecision for_each(Callback);
|
2019-03-23 21:03:17 +00:00
|
|
|
|
2019-10-20 16:11:40 +00:00
|
|
|
static constexpr u32 default_kernel_stack_size = 65536;
|
2021-02-12 18:17:09 +00:00
|
|
|
static constexpr u32 default_userspace_stack_size = 1 * MiB;
|
2019-10-20 16:11:40 +00:00
|
|
|
|
2020-12-04 05:12:50 +00:00
|
|
|
u32 ticks_in_user() const { return m_ticks_in_user; }
|
|
|
|
u32 ticks_in_kernel() const { return m_ticks_in_kernel; }
|
|
|
|
|
2021-01-25 20:19:34 +00:00
|
|
|
enum class PreviousMode : u8 {
|
|
|
|
KernelMode = 0,
|
|
|
|
UserMode
|
|
|
|
};
|
|
|
|
PreviousMode previous_mode() const { return m_previous_mode; }
|
|
|
|
void set_previous_mode(PreviousMode mode) { m_previous_mode = mode; }
|
|
|
|
TrapFrame*& current_trap() { return m_current_trap; }
|
|
|
|
|
2020-08-02 22:59:01 +00:00
|
|
|
RecursiveSpinLock& get_lock() const { return m_lock; }
|
|
|
|
|
2021-01-23 22:29:11 +00:00
|
|
|
#if LOCK_DEBUG
|
2021-04-24 22:17:02 +00:00
|
|
|
void holding_lock(Lock& lock, int refs_delta, const SourceLocation& location)
|
2020-12-01 02:04:36 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(refs_delta != 0);
|
2020-12-14 23:36:22 +00:00
|
|
|
m_holding_locks.fetch_add(refs_delta, AK::MemoryOrder::memory_order_relaxed);
|
2020-12-01 02:04:36 +00:00
|
|
|
ScopedSpinLock list_lock(m_holding_locks_lock);
|
2020-12-14 23:36:22 +00:00
|
|
|
if (refs_delta > 0) {
|
2020-12-01 02:04:36 +00: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-14 23:36:22 +00:00
|
|
|
info.count += refs_delta;
|
2020-12-01 02:04:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!have_existing)
|
2021-04-24 22:17:02 +00:00
|
|
|
m_holding_locks_list.append({ &lock, location, 1 });
|
2020-12-01 02:04:36 +00:00
|
|
|
} else {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(refs_delta < 0);
|
2020-12-01 02:04:36 +00: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) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(info.count >= (unsigned)-refs_delta);
|
2020-12-14 23:36:22 +00:00
|
|
|
info.count -= (unsigned)-refs_delta;
|
|
|
|
if (info.count == 0)
|
2020-12-01 02:04:36 +00:00
|
|
|
m_holding_locks_list.remove(i);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(found);
|
2020-12-01 02:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
u32 lock_count() const
|
|
|
|
{
|
|
|
|
return m_holding_locks.load(AK::MemoryOrder::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-07 17:13:51 +00:00
|
|
|
bool is_handling_page_fault() const
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-02-07 17:13:51 +00:00
|
|
|
return m_handling_page_fault;
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
2021-01-20 23:06:19 +00:00
|
|
|
void set_handling_page_fault(bool b) { m_handling_page_fault = b; }
|
2021-01-29 03:07:41 +00:00
|
|
|
void set_idle_thread() { m_is_idle_thread = true; }
|
|
|
|
bool is_idle_thread() const { return m_is_idle_thread; }
|
2021-01-20 23:06:19 +00:00
|
|
|
|
2021-05-18 09:26:11 +00:00
|
|
|
ALWAYS_INLINE u32 enter_profiler()
|
|
|
|
{
|
|
|
|
return m_nested_profiler_calls.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE u32 leave_profiler()
|
|
|
|
{
|
|
|
|
return m_nested_profiler_calls.fetch_sub(1, AK::MemoryOrder::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
2021-05-30 14:24:53 +00:00
|
|
|
bool is_profiling_suppressed() const { return m_is_profiling_suppressed; }
|
|
|
|
void set_profiling_suppressed() { m_is_profiling_suppressed = true; }
|
|
|
|
|
Kernel: Introduce the new ProcFS design
The new ProcFS design consists of two main parts:
1. The representative ProcFS class, which is derived from the FS class.
The ProcFS and its inodes are much more lean - merely 3 classes to
represent the common type of inodes - regular files, symbolic links and
directories. They're backed by a ProcFSExposedComponent object, which
is responsible for the functional operation behind the scenes.
2. The backend of the ProcFS - the ProcFSComponentsRegistrar class
and all derived classes from the ProcFSExposedComponent class. These
together form the entire backend and handle all the functions you can
expect from the ProcFS.
The ProcFSExposedComponent derived classes split to 3 types in the
manner of lifetime in the kernel:
1. Persistent objects - this category includes all basic objects, like
the root folder, /proc/bus folder, main blob files in the root folders,
etc. These objects are persistent and cannot die ever.
2. Semi-persistent objects - this category includes all PID folders,
and subdirectories to the PID folders. It also includes exposed objects
like the unveil JSON'ed blob. These object are persistent as long as the
the responsible process they represent is still alive.
3. Dynamic objects - this category includes files in the subdirectories
of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially,
these objects are always created dynamically and when no longer in need
after being used, they're deallocated.
Nevertheless, the new allocated backend objects and inodes try to use
the same InodeIndex if possible - this might change only when a thread
dies and a new thread is born with a new thread stack, or when a file
descriptor is closed and a new one within the same file descriptor
number is opened. This is needed to actually be able to do something
useful with these objects.
The new design assures that many ProcFS instances can be used at once,
with one backend for usage for all instances.
2021-06-12 01:23:58 +00:00
|
|
|
InodeIndex global_procfs_inode_index() const { return m_global_procfs_inode_index; }
|
2021-06-19 09:27:20 +00:00
|
|
|
|
2021-07-15 20:54:19 +00:00
|
|
|
String backtrace();
|
|
|
|
|
2019-07-19 11:04:42 +00:00
|
|
|
private:
|
2021-07-01 20:32:59 +00:00
|
|
|
Thread(NonnullRefPtr<Process>, NonnullOwnPtr<Region>, NonnullRefPtr<Timer>, FPUState*);
|
2021-02-07 17:13:51 +00:00
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
IntrusiveListNode<Thread> m_process_thread_list_node;
|
2021-01-22 23:56:08 +00:00
|
|
|
int m_runnable_priority { -1 };
|
2019-07-19 11:04:42 +00:00
|
|
|
|
2019-12-22 11:23:44 +00:00
|
|
|
friend class WaitQueue;
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
class JoinBlockCondition : public BlockCondition {
|
|
|
|
public:
|
|
|
|
void thread_did_exit(void* exit_value)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_thread_did_exit);
|
2020-11-29 23:05:27 +00:00
|
|
|
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
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_thread_did_exit);
|
2020-11-29 23:05:27 +00:00
|
|
|
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
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Blocker::Type::Join);
|
2020-11-29 23:05:27 +00:00
|
|
|
auto& blocker = static_cast<JoinBlocker&>(b);
|
|
|
|
|
|
|
|
// NOTE: m_lock is held already!
|
2020-12-01 02:04:36 +00:00
|
|
|
if (m_thread_did_exit) {
|
2020-11-29 23:05:27 +00:00
|
|
|
blocker.unblock(exit_value(), true);
|
2020-12-01 02:04:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void do_unblock_joiner()
|
|
|
|
{
|
2020-12-22 18:17:56 +00:00
|
|
|
do_unblock([&](Blocker& b, void*, bool&) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(b.blocker_type() == Blocker::Type::Join);
|
2020-11-29 23:05:27 +00:00
|
|
|
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-14 23:36:22 +00:00
|
|
|
LockMode unlock_process_if_locked(u32&);
|
|
|
|
void relock_process(LockMode, u32);
|
2020-02-18 12:44:27 +00:00
|
|
|
void reset_fpu_state();
|
|
|
|
|
2020-08-02 22:59:01 +00:00
|
|
|
mutable RecursiveSpinLock m_lock;
|
2020-12-08 04:29:41 +00:00
|
|
|
mutable RecursiveSpinLock m_block_lock;
|
2020-08-02 02:04:56 +00:00
|
|
|
NonnullRefPtr<Process> m_process;
|
2020-08-08 15:32:34 +00:00
|
|
|
ThreadID m_tid { -1 };
|
2021-06-26 17:57:16 +00:00
|
|
|
ThreadRegisters m_regs;
|
2021-04-15 16:29:00 +00:00
|
|
|
DebugRegisterState m_debug_register_state {};
|
2021-01-25 20:19:34 +00:00
|
|
|
TrapFrame* m_current_trap { nullptr };
|
2021-01-24 05:24:10 +00:00
|
|
|
u32 m_saved_critical { 1 };
|
2021-04-16 12:03:24 +00:00
|
|
|
IntrusiveListNode<Thread> m_ready_queue_node;
|
2020-06-28 04:36:15 +00:00
|
|
|
Atomic<u32> m_cpu { 0 };
|
2020-06-28 21:34:31 +00:00
|
|
|
u32 m_cpu_affinity { THREAD_AFFINITY_DEFAULT };
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 m_ticks_left { 0 };
|
|
|
|
u32 m_times_scheduled { 0 };
|
2020-12-04 05:12:50 +00:00
|
|
|
u32 m_ticks_in_user { 0 };
|
|
|
|
u32 m_ticks_in_kernel { 0 };
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 m_pending_signals { 0 };
|
|
|
|
u32 m_signal_mask { 0 };
|
|
|
|
u32 m_kernel_stack_base { 0 };
|
2019-09-04 02:31:38 +00:00
|
|
|
u32 m_kernel_stack_top { 0 };
|
2019-09-27 12:19:07 +00:00
|
|
|
OwnPtr<Region> m_kernel_stack_region;
|
2019-09-07 13:50:44 +00:00
|
|
|
VirtualAddress m_thread_specific_data;
|
2021-05-28 09:18:58 +00:00
|
|
|
Optional<Range> m_thread_specific_range;
|
2021-02-21 10:59:53 +00:00
|
|
|
Array<SignalActionData, NSIG> m_signal_action_data;
|
2019-09-09 03:58:42 +00:00
|
|
|
Blocker* m_blocker { nullptr };
|
2021-07-10 16:23:16 +00:00
|
|
|
Kernel::Lock* m_blocking_lock { nullptr };
|
|
|
|
u32 m_lock_requested_count { 0 };
|
|
|
|
IntrusiveListNode<Thread> m_blocked_threads_list_node;
|
2019-11-14 19:58:23 +00:00
|
|
|
|
2021-01-23 22:29:11 +00:00
|
|
|
#if LOCK_DEBUG
|
2020-12-01 02:04:36 +00:00
|
|
|
struct HoldingLockInfo {
|
|
|
|
Lock* lock;
|
2021-04-24 22:17:02 +00:00
|
|
|
SourceLocation source_location;
|
2020-12-01 02:04:36 +00:00
|
|
|
unsigned count;
|
|
|
|
};
|
|
|
|
Atomic<u32> m_holding_locks { 0 };
|
|
|
|
SpinLock<u8> m_holding_locks_lock;
|
|
|
|
Vector<HoldingLockInfo> m_holding_locks_list;
|
|
|
|
#endif
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
JoinBlockCondition m_join_condition;
|
2021-01-23 06:24:33 +00:00
|
|
|
Atomic<bool, AK::MemoryOrder::memory_order_relaxed> m_is_active { false };
|
2019-11-18 03:08:10 +00:00
|
|
|
bool m_is_joinable { true };
|
2021-01-20 23:06:19 +00:00
|
|
|
bool m_handling_page_fault { false };
|
2021-01-25 20:19:34 +00:00
|
|
|
PreviousMode m_previous_mode { PreviousMode::UserMode };
|
2019-11-14 19:58:23 +00:00
|
|
|
|
2019-11-26 20:35:24 +00:00
|
|
|
unsigned m_syscall_count { 0 };
|
|
|
|
unsigned m_inode_faults { 0 };
|
|
|
|
unsigned m_zero_faults { 0 };
|
|
|
|
unsigned m_cow_faults { 0 };
|
|
|
|
|
2019-12-01 16:36:06 +00: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 14:27:45 +00:00
|
|
|
FPUState* m_fpu_state { nullptr };
|
2019-04-20 17:29:48 +00:00
|
|
|
State m_state { Invalid };
|
2019-12-07 19:45:26 +00:00
|
|
|
String m_name;
|
2019-12-30 17:46:17 +00:00
|
|
|
u32 m_priority { THREAD_PRIORITY_NORMAL };
|
2020-01-27 19:47:10 +00:00
|
|
|
|
2020-03-01 14:14:17 +00:00
|
|
|
State m_stop_state { Invalid };
|
2020-01-27 19:47:10 +00:00
|
|
|
|
2019-08-06 17:43:07 +00: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 15:46:01 +00:00
|
|
|
bool m_should_die { false };
|
2020-07-04 23:37:36 +00:00
|
|
|
bool m_initialized { false };
|
2020-12-12 03:19:45 +00:00
|
|
|
bool m_in_block { false };
|
2021-01-29 03:07:41 +00:00
|
|
|
bool m_is_idle_thread { false };
|
2020-09-07 14:31:00 +00:00
|
|
|
Atomic<bool> m_have_any_unmasked_pending_signals { false };
|
2021-05-18 09:26:11 +00:00
|
|
|
Atomic<u32> m_nested_profiler_calls { 0 };
|
2019-07-19 07:57:35 +00:00
|
|
|
|
2021-05-19 22:41:51 +00:00
|
|
|
RefPtr<Timer> m_block_timer;
|
|
|
|
|
Kernel: Introduce the new ProcFS design
The new ProcFS design consists of two main parts:
1. The representative ProcFS class, which is derived from the FS class.
The ProcFS and its inodes are much more lean - merely 3 classes to
represent the common type of inodes - regular files, symbolic links and
directories. They're backed by a ProcFSExposedComponent object, which
is responsible for the functional operation behind the scenes.
2. The backend of the ProcFS - the ProcFSComponentsRegistrar class
and all derived classes from the ProcFSExposedComponent class. These
together form the entire backend and handle all the functions you can
expect from the ProcFS.
The ProcFSExposedComponent derived classes split to 3 types in the
manner of lifetime in the kernel:
1. Persistent objects - this category includes all basic objects, like
the root folder, /proc/bus folder, main blob files in the root folders,
etc. These objects are persistent and cannot die ever.
2. Semi-persistent objects - this category includes all PID folders,
and subdirectories to the PID folders. It also includes exposed objects
like the unveil JSON'ed blob. These object are persistent as long as the
the responsible process they represent is still alive.
3. Dynamic objects - this category includes files in the subdirectories
of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially,
these objects are always created dynamically and when no longer in need
after being used, they're deallocated.
Nevertheless, the new allocated backend objects and inodes try to use
the same InodeIndex if possible - this might change only when a thread
dies and a new thread is born with a new thread stack, or when a file
descriptor is closed and a new one within the same file descriptor
number is opened. This is needed to actually be able to do something
useful with these objects.
The new design assures that many ProcFS instances can be used at once,
with one backend for usage for all instances.
2021-06-12 01:23:58 +00:00
|
|
|
// Note: This is needed so when we generate thread stack inodes for ProcFS, we know that
|
|
|
|
// we assigned a global Inode index to it so we can use it later
|
|
|
|
InodeIndex m_global_procfs_inode_index;
|
|
|
|
|
2021-05-30 14:24:53 +00:00
|
|
|
bool m_is_profiling_suppressed { false };
|
|
|
|
|
2019-11-16 11:18:59 +00:00
|
|
|
void yield_without_holding_big_lock();
|
2020-12-08 04:29:41 +00:00
|
|
|
void yield_while_not_holding_big_lock();
|
2021-01-01 05:45:16 +00:00
|
|
|
void drop_thread_count(bool);
|
2019-03-23 21:03:17 +00:00
|
|
|
};
|
|
|
|
|
2021-03-07 11:01:11 +00:00
|
|
|
AK_ENUM_BITWISE_OPERATORS(Thread::FileBlocker::BlockFlags);
|
|
|
|
|
2021-05-16 09:36:52 +00:00
|
|
|
template<IteratorFunction<Thread&> Callback>
|
2019-07-19 10:16:00 +00:00
|
|
|
inline IterationDecision Thread::for_each(Callback callback)
|
2019-03-23 21:03:17 +00:00
|
|
|
{
|
2021-01-28 05:58:24 +00:00
|
|
|
ScopedSpinLock lock(g_tid_map_lock);
|
|
|
|
for (auto& it : *g_tid_map) {
|
|
|
|
IterationDecision decision = callback(*it.value);
|
|
|
|
if (decision != IterationDecision::Continue)
|
|
|
|
return decision;
|
2019-08-07 18:43:54 +00:00
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 09:36:52 +00:00
|
|
|
template<IteratorFunction<Thread&> Callback>
|
2021-01-28 05:58:24 +00:00
|
|
|
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
|
2019-08-07 18:43:54 +00:00
|
|
|
{
|
2021-01-28 05:58:24 +00:00
|
|
|
ScopedSpinLock lock(g_tid_map_lock);
|
|
|
|
for (auto& it : *g_tid_map) {
|
|
|
|
auto& thread = *it.value;
|
|
|
|
if (thread.state() != state)
|
|
|
|
continue;
|
|
|
|
IterationDecision decision = callback(thread);
|
|
|
|
if (decision != IterationDecision::Continue)
|
|
|
|
return decision;
|
2019-08-07 18:43:54 +00:00
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
2019-09-07 13:50:44 +00:00
|
|
|
|
2021-05-16 09:36:52 +00:00
|
|
|
template<VoidFunction<Thread&> Callback>
|
|
|
|
inline IterationDecision Thread::for_each(Callback callback)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(g_tid_map_lock);
|
|
|
|
for (auto& it : *g_tid_map)
|
|
|
|
callback(*it.value);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<VoidFunction<Thread&> Callback>
|
|
|
|
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
|
|
|
|
{
|
|
|
|
return for_each_in_state(state, [&](auto& thread) {
|
|
|
|
callback(thread);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|
2021-01-08 23:42:44 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Kernel::Thread> : AK::Formatter<FormatString> {
|
|
|
|
void format(FormatBuilder&, const Kernel::Thread&);
|
|
|
|
};
|