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
|
|
|
|
|
2020-01-12 20:46:41 +03:00
|
|
|
#include <AK/Atomic.h>
|
2019-07-14 15:51:54 +03:00
|
|
|
#include <AK/Function.h>
|
2019-07-19 14:04:42 +03:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-04-20 20:23:45 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-10-20 19:11:40 +03:00
|
|
|
#include <AK/String.h>
|
2019-03-24 00:03:17 +03:00
|
|
|
#include <AK/Vector.h>
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/KResult.h>
|
2019-09-04 16:14:54 +03:00
|
|
|
#include <Kernel/Scheduler.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
#include <Kernel/VM/Region.h>
|
2019-07-19 10:04:12 +03:00
|
|
|
#include <LibC/fd_set.h>
|
2019-03-24 00:03:17 +03:00
|
|
|
|
|
|
|
class Alarm;
|
2019-06-07 10:36:51 +03:00
|
|
|
class FileDescription;
|
2019-03-24 00:03:17 +03:00
|
|
|
class Process;
|
2019-07-25 22:02:19 +03:00
|
|
|
class ProcessInspectionHandle;
|
2019-03-24 00:03:17 +03:00
|
|
|
class Region;
|
2019-12-01 13:57:20 +03:00
|
|
|
class WaitQueue;
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-06-07 18:13:23 +03:00
|
|
|
enum class ShouldUnblockThread {
|
2019-05-28 12:53:16 +03:00
|
|
|
No = 0,
|
|
|
|
Yes
|
|
|
|
};
|
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
|
|
|
|
2019-07-19 14:04:42 +03:00
|
|
|
class 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:
|
|
|
|
explicit Thread(Process&);
|
|
|
|
~Thread();
|
|
|
|
|
2019-12-30 21:23:13 +03:00
|
|
|
static Thread* from_tid(int);
|
2019-03-24 00:03:17 +03:00
|
|
|
static void initialize();
|
|
|
|
static void finalize_dying_threads();
|
|
|
|
|
2019-03-24 01:50:34 +03:00
|
|
|
static Vector<Thread*> all_threads();
|
2019-04-17 13:41:51 +03:00
|
|
|
static bool is_thread(void*);
|
2019-03-24 01:50:34 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
int tid() const { return m_tid; }
|
|
|
|
int pid() const;
|
|
|
|
|
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 21:23:13 +03:00
|
|
|
void set_priority_boost(u32 boost) { m_priority_boost = boost; }
|
|
|
|
u32 priority_boost() const { return m_priority_boost; }
|
|
|
|
|
2019-12-30 22:10:00 +03:00
|
|
|
u32 effective_priority() const;
|
2019-11-06 18:26:51 +03:00
|
|
|
|
2019-11-18 06:08:10 +03:00
|
|
|
void set_joinable(bool j) { m_is_joinable = j; }
|
|
|
|
bool is_joinable() const { return m_is_joinable; }
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
Process& process() { return m_process; }
|
|
|
|
const Process& process() const { return m_process; }
|
|
|
|
|
2019-07-25 22:02:19 +03:00
|
|
|
String backtrace(ProcessInspectionHandle&) const;
|
2019-12-11 22:36:56 +03:00
|
|
|
Vector<u32> raw_backtrace(u32 ebp) const;
|
2019-07-25 22:02:19 +03:00
|
|
|
|
2019-12-07 22:45:26 +03:00
|
|
|
const String& name() const { return m_name; }
|
|
|
|
void set_name(StringView s) { m_name = s; }
|
|
|
|
|
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,
|
|
|
|
Skip1SchedulerPass,
|
|
|
|
Skip0SchedulerPasses,
|
|
|
|
Dying,
|
|
|
|
Dead,
|
|
|
|
Stopped,
|
2019-07-19 10:37:34 +03:00
|
|
|
Blocked,
|
2019-12-01 17:54:47 +03:00
|
|
|
Queued,
|
2019-03-24 00:03:17 +03:00
|
|
|
};
|
|
|
|
|
2019-07-18 19:12:37 +03:00
|
|
|
class Blocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2019-07-18 19:12:37 +03:00
|
|
|
virtual ~Blocker() {}
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t now_s, long us) = 0;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const = 0;
|
2020-01-10 21:15:01 +03:00
|
|
|
void set_interrupted_by_death() { m_was_interrupted_by_death = true; }
|
|
|
|
bool was_interrupted_by_death() const { return m_was_interrupted_by_death; }
|
2019-07-20 12:05:52 +03:00
|
|
|
void set_interrupted_by_signal() { m_was_interrupted_while_blocked = true; }
|
|
|
|
bool was_interrupted_by_signal() const { return m_was_interrupted_while_blocked; }
|
2019-09-04 16:14:54 +03:00
|
|
|
|
2019-07-20 12:05:52 +03:00
|
|
|
private:
|
|
|
|
bool m_was_interrupted_while_blocked { false };
|
2020-01-10 21:15:01 +03:00
|
|
|
bool m_was_interrupted_by_death { false };
|
2019-07-21 13:14:58 +03:00
|
|
|
friend class Thread;
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-11-14 22:58:23 +03:00
|
|
|
class JoinBlocker final : public Blocker {
|
|
|
|
public:
|
2019-11-14 23:04:34 +03:00
|
|
|
explicit JoinBlocker(Thread& joinee, void*& joinee_exit_value);
|
2019-11-14 22:58:23 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t now_s, long us) override;
|
|
|
|
virtual const char* state_string() const override { return "Joining"; }
|
2019-11-14 23:04:34 +03:00
|
|
|
void set_joinee_exit_value(void* value) { m_joinee_exit_value = value; }
|
2019-11-14 22:58:23 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
Thread& m_joinee;
|
2019-11-14 23:04:34 +03:00
|
|
|
void*& m_joinee_exit_value;
|
2019-11-14 22:58:23 +03:00
|
|
|
};
|
|
|
|
|
2019-07-18 19:12:37 +03:00
|
|
|
class FileDescriptionBlocker : public Blocker {
|
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
|
|
|
|
2019-07-19 14:32:56 +03:00
|
|
|
protected:
|
|
|
|
explicit FileDescriptionBlocker(const FileDescription&);
|
|
|
|
|
2019-07-18 17:22:26 +03:00
|
|
|
private:
|
2019-07-19 14:19:47 +03:00
|
|
|
NonnullRefPtr<FileDescription> m_blocked_description;
|
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:
|
2019-07-19 14:19:47 +03:00
|
|
|
explicit AcceptBlocker(const FileDescription&);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
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 ReceiveBlocker final : public FileDescriptionBlocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2019-07-19 14:19:47 +03:00
|
|
|
explicit ReceiveBlocker(const FileDescription&);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Receiving"; }
|
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:
|
2019-07-19 14:19:47 +03:00
|
|
|
explicit ConnectBlocker(const FileDescription&);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
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:
|
2019-07-19 14:19:47 +03:00
|
|
|
explicit WriteBlocker(const FileDescription&);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Writing"; }
|
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:
|
2019-07-19 14:19:47 +03:00
|
|
|
explicit ReadBlocker(const FileDescription&);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Reading"; }
|
2019-07-18 17:22:26 +03:00
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class ConditionBlocker final : public Blocker {
|
2019-07-18 17:22:26 +03:00
|
|
|
public:
|
2019-07-19 14:19:47 +03:00
|
|
|
ConditionBlocker(const char* state_string, Function<bool()>&& condition);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return m_state_string; }
|
2019-07-18 17:22:26 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
Function<bool()> m_block_until_condition;
|
2019-07-19 14:19:47 +03:00
|
|
|
const char* m_state_string { nullptr };
|
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:
|
2019-07-19 14:19:47 +03:00
|
|
|
explicit SleepBlocker(u64 wakeup_time);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Sleeping"; }
|
2019-07-18 18:26:11 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
u64 m_wakeup_time { 0 };
|
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class SelectBlocker final : public Blocker {
|
2019-07-18 18:39:49 +03:00
|
|
|
public:
|
2019-07-19 10:04:12 +03:00
|
|
|
typedef Vector<int, FD_SETSIZE> FDVector;
|
|
|
|
SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds);
|
2019-07-18 18:39:49 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) 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:
|
|
|
|
timeval m_select_timeout;
|
|
|
|
bool m_select_has_timeout { false };
|
2019-07-19 10:04:12 +03:00
|
|
|
const FDVector& m_select_read_fds;
|
|
|
|
const FDVector& m_select_write_fds;
|
|
|
|
const FDVector& m_select_exceptional_fds;
|
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:
|
2019-07-18 19:12:37 +03:00
|
|
|
WaitBlocker(int wait_options, pid_t& waitee_pid);
|
2019-07-18 19:05:19 +03:00
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override { return "Waiting"; }
|
2019-07-18 19:05:19 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_wait_options { 0 };
|
|
|
|
pid_t& m_waitee_pid;
|
|
|
|
};
|
|
|
|
|
2019-07-19 14:19:47 +03:00
|
|
|
class SemiPermanentBlocker final : public Blocker {
|
2019-07-19 10:34:11 +03:00
|
|
|
public:
|
|
|
|
enum class Reason {
|
|
|
|
Signal,
|
|
|
|
};
|
|
|
|
|
|
|
|
SemiPermanentBlocker(Reason reason);
|
|
|
|
virtual bool should_unblock(Thread&, time_t, long) override;
|
2019-07-19 10:51:48 +03:00
|
|
|
virtual const char* state_string() const override
|
|
|
|
{
|
|
|
|
switch (m_reason) {
|
2019-07-19 14:19:47 +03:00
|
|
|
case Reason::Signal:
|
|
|
|
return "Signal";
|
2019-07-19 10:51:48 +03:00
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-07-19 10:34:11 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
Reason m_reason;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
bool is_stopped() const { return m_state == Stopped; }
|
2019-07-19 14:19:47 +03:00
|
|
|
bool is_blocked() const { return m_state == Blocked; }
|
2019-03-24 00:03:17 +03:00
|
|
|
bool in_kernel() const { return (m_tss.cs & 0x03) == 0; }
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 frame_ptr() const { return m_tss.ebp; }
|
|
|
|
u32 stack_ptr() const { return m_tss.esp; }
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-12-15 19:58:53 +03:00
|
|
|
RegisterDump& get_register_dump_from_stack();
|
2019-11-02 12:11:41 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 selector() const { return m_far_ptr.selector; }
|
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-07-03 22:17:35 +03:00
|
|
|
u32 ticks() const { return m_ticks; }
|
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; }
|
|
|
|
|
2019-07-18 18:26:11 +03:00
|
|
|
u64 sleep(u32 ticks);
|
2019-11-02 21:34:06 +03:00
|
|
|
u64 sleep_until(u64 wakeup_time);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-07-20 12:05:52 +03:00
|
|
|
enum class BlockResult {
|
|
|
|
WokeNormally,
|
|
|
|
InterruptedBySignal,
|
2020-01-10 21:15:01 +03:00
|
|
|
InterruptedByDeath,
|
2019-07-20 12:05:52 +03:00
|
|
|
};
|
|
|
|
|
2019-09-04 16:14:54 +03:00
|
|
|
template<typename T, class... Args>
|
2019-12-01 17:54:47 +03:00
|
|
|
[[nodiscard]] BlockResult block(Args&&... args)
|
2019-07-19 11:12:50 +03:00
|
|
|
{
|
2019-07-19 18:58:45 +03:00
|
|
|
// We should never be blocking a blocked (or otherwise non-active) thread.
|
|
|
|
ASSERT(state() == Thread::Running);
|
2019-09-09 06:58:42 +03:00
|
|
|
ASSERT(m_blocker == nullptr);
|
2019-07-21 13:14:58 +03:00
|
|
|
|
2019-12-01 14:45:51 +03:00
|
|
|
T t(forward<Args>(args)...);
|
2019-09-09 06:58:42 +03:00
|
|
|
m_blocker = &t;
|
2019-07-19 18:58:45 +03:00
|
|
|
set_state(Thread::Blocked);
|
2019-07-21 13:14:58 +03:00
|
|
|
|
|
|
|
// Yield to the scheduler, and wait for us to resume unblocked.
|
2019-12-01 17:54:47 +03:00
|
|
|
yield_without_holding_big_lock();
|
2019-07-21 13:14:58 +03:00
|
|
|
|
|
|
|
// We should no longer be blocked once we woke up
|
2019-07-19 18:58:45 +03:00
|
|
|
ASSERT(state() != Thread::Blocked);
|
2019-07-21 13:14:58 +03:00
|
|
|
|
|
|
|
// Remove ourselves...
|
2019-09-09 06:58:42 +03:00
|
|
|
m_blocker = nullptr;
|
2019-07-21 13:14:58 +03:00
|
|
|
|
2019-07-20 12:05:52 +03:00
|
|
|
if (t.was_interrupted_by_signal())
|
|
|
|
return BlockResult::InterruptedBySignal;
|
2019-07-21 13:14:58 +03:00
|
|
|
|
2020-01-10 21:15:01 +03:00
|
|
|
if (t.was_interrupted_by_death())
|
|
|
|
return BlockResult::InterruptedByDeath;
|
|
|
|
|
2019-07-20 12:05:52 +03:00
|
|
|
return BlockResult::WokeNormally;
|
2019-12-01 13:57:20 +03:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:54:47 +03:00
|
|
|
[[nodiscard]] BlockResult block_until(const char* state_string, Function<bool()>&& condition)
|
2019-12-01 13:57:20 +03:00
|
|
|
{
|
2019-12-01 17:54:47 +03:00
|
|
|
return block<ConditionBlocker>(state_string, move(condition));
|
2019-12-01 13:57:20 +03:00
|
|
|
}
|
2019-07-19 11:12:50 +03:00
|
|
|
|
2020-01-12 20:46:41 +03:00
|
|
|
void wait_on(WaitQueue& queue, Atomic<bool>* lock = nullptr, Thread* beneficiary = nullptr, const char* reason = nullptr);
|
2019-12-22 14:23:44 +03:00
|
|
|
void wake_from_queue();
|
2019-12-01 14:47:53 +03:00
|
|
|
|
2019-07-19 11:12:50 +03:00
|
|
|
void unblock();
|
2019-07-20 12:05:52 +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();
|
|
|
|
void die_if_needed();
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
const FarPtr& far_ptr() const { return m_far_ptr; }
|
|
|
|
|
|
|
|
bool tick();
|
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
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void set_selector(u16 s) { m_far_ptr.selector = s; }
|
2019-05-18 19:31:36 +03:00
|
|
|
void set_state(State);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
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-07-18 15:10:28 +03:00
|
|
|
void consider_unblock(time_t now_sec, long now_usec);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-08-06 20:43:07 +03:00
|
|
|
void set_dump_backtrace_on_finalization() { m_dump_backtrace_on_finalization = true; }
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
ShouldUnblockThread dispatch_one_pending_signal();
|
2019-07-03 22:17:35 +03:00
|
|
|
ShouldUnblockThread dispatch_signal(u8 signal);
|
2019-03-24 00:03:17 +03:00
|
|
|
bool has_unmasked_pending_signals() const;
|
2019-07-03 22:17:35 +03:00
|
|
|
void terminate_due_to_signal(u8 signal);
|
2019-07-08 19:59:48 +03:00
|
|
|
bool should_ignore_signal(u8 signal) const;
|
2019-10-07 12:22:50 +03:00
|
|
|
bool has_signal_handler(u8 signal) 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();
|
2019-07-03 22:17:35 +03:00
|
|
|
void push_value_on_stack(u32);
|
2019-12-19 01:03:23 +03:00
|
|
|
|
|
|
|
u32 make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment);
|
2019-03-24 00:03:17 +03:00
|
|
|
|
2019-09-07 16:50:44 +03:00
|
|
|
void make_thread_specific_region(Badge<Process>);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
Thread* clone(Process&);
|
|
|
|
|
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
|
|
|
|
2019-05-18 19:31:36 +03:00
|
|
|
static bool is_runnable_state(Thread::State state)
|
|
|
|
{
|
|
|
|
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;
|
2019-10-31 15:57:07 +03:00
|
|
|
static constexpr u32 default_userspace_stack_size = 4 * MB;
|
2019-10-20 19:11:40 +03:00
|
|
|
|
2019-07-19 14:04:42 +03:00
|
|
|
private:
|
|
|
|
IntrusiveListNode m_runnable_list_node;
|
2019-12-22 14:23:44 +03:00
|
|
|
IntrusiveListNode m_wait_queue_node;
|
2019-07-19 14:04:42 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
private:
|
2019-07-19 18:21:13 +03:00
|
|
|
friend class SchedulerData;
|
2019-12-22 14:23:44 +03:00
|
|
|
friend class WaitQueue;
|
2019-12-01 17:54:47 +03:00
|
|
|
bool unlock_process_if_locked();
|
|
|
|
void relock_process();
|
|
|
|
|
2019-08-06 20:43:07 +03:00
|
|
|
String backtrace_impl() const;
|
2019-03-24 00:03:17 +03:00
|
|
|
Process& m_process;
|
|
|
|
int m_tid { -1 };
|
|
|
|
TSS32 m_tss;
|
|
|
|
FarPtr m_far_ptr;
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 m_ticks { 0 };
|
|
|
|
u32 m_ticks_left { 0 };
|
|
|
|
u32 m_times_scheduled { 0 };
|
|
|
|
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
|
|
|
|
2019-11-18 06:08:10 +03:00
|
|
|
bool m_is_joinable { true };
|
2019-11-14 22:58:23 +03:00
|
|
|
Thread* m_joiner { nullptr };
|
|
|
|
Thread* m_joinee { nullptr };
|
|
|
|
void* m_exit_value { nullptr };
|
|
|
|
|
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 };
|
2019-12-30 21:23:13 +03:00
|
|
|
u32 m_priority_boost { 0 };
|
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 };
|
2019-07-19 10:57:35 +03:00
|
|
|
|
2019-11-16 14:18:59 +03:00
|
|
|
void yield_without_holding_big_lock();
|
2019-03-24 00:03:17 +03:00
|
|
|
};
|
|
|
|
|
2019-05-18 19:31:36 +03:00
|
|
|
HashTable<Thread*>& thread_table();
|
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();
|
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();
|
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;
|
|
|
|
|
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();
|
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();
|
|
|
|
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
|
|
|
|
|
|
|
u16 thread_specific_selector();
|
|
|
|
Descriptor& thread_specific_descriptor();
|