Thread.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #pragma once
  2. #include <AK/Function.h>
  3. #include <AK/IntrusiveList.h>
  4. #include <AK/OwnPtr.h>
  5. #include <AK/RefPtr.h>
  6. #include <AK/String.h>
  7. #include <AK/Vector.h>
  8. #include <Kernel/Arch/i386/CPU.h>
  9. #include <Kernel/KResult.h>
  10. #include <Kernel/Scheduler.h>
  11. #include <Kernel/UnixTypes.h>
  12. #include <Kernel/VM/Region.h>
  13. #include <LibC/fd_set.h>
  14. class Alarm;
  15. class FileDescription;
  16. class Process;
  17. class ProcessInspectionHandle;
  18. class Region;
  19. class WaitQueue;
  20. enum class ShouldUnblockThread {
  21. No = 0,
  22. Yes
  23. };
  24. struct SignalActionData {
  25. VirtualAddress handler_or_sigaction;
  26. u32 mask { 0 };
  27. int flags { 0 };
  28. };
  29. struct ThreadSpecificData {
  30. ThreadSpecificData* self;
  31. };
  32. #define THREAD_PRIORITY_MIN 1
  33. #define THREAD_PRIORITY_LOW 10
  34. #define THREAD_PRIORITY_NORMAL 30
  35. #define THREAD_PRIORITY_HIGH 50
  36. #define THREAD_PRIORITY_MAX 99
  37. class Thread {
  38. friend class Process;
  39. friend class Scheduler;
  40. public:
  41. explicit Thread(Process&);
  42. ~Thread();
  43. static Thread* from_tid(int);
  44. static void initialize();
  45. static void finalize_dying_threads();
  46. static Vector<Thread*> all_threads();
  47. static bool is_thread(void*);
  48. int tid() const { return m_tid; }
  49. int pid() const;
  50. void set_priority(u32 p) { m_priority = p; }
  51. u32 priority() const { return m_priority; }
  52. void set_priority_boost(u32 boost) { m_priority_boost = boost; }
  53. u32 priority_boost() const { return m_priority_boost; }
  54. u32 effective_priority() const;
  55. void set_joinable(bool j) { m_is_joinable = j; }
  56. bool is_joinable() const { return m_is_joinable; }
  57. Process& process() { return m_process; }
  58. const Process& process() const { return m_process; }
  59. String backtrace(ProcessInspectionHandle&) const;
  60. Vector<u32> raw_backtrace(u32 ebp) const;
  61. const String& name() const { return m_name; }
  62. void set_name(StringView s) { m_name = s; }
  63. void finalize();
  64. enum State : u8 {
  65. Invalid = 0,
  66. Runnable,
  67. Running,
  68. Skip1SchedulerPass,
  69. Skip0SchedulerPasses,
  70. Dying,
  71. Dead,
  72. Stopped,
  73. Blocked,
  74. Queued,
  75. };
  76. class Blocker {
  77. public:
  78. virtual ~Blocker() {}
  79. virtual bool should_unblock(Thread&, time_t now_s, long us) = 0;
  80. virtual const char* state_string() const = 0;
  81. void set_interrupted_by_signal() { m_was_interrupted_while_blocked = true; }
  82. bool was_interrupted_by_signal() const { return m_was_interrupted_while_blocked; }
  83. private:
  84. bool m_was_interrupted_while_blocked { false };
  85. friend class Thread;
  86. };
  87. class JoinBlocker final : public Blocker {
  88. public:
  89. explicit JoinBlocker(Thread& joinee, void*& joinee_exit_value);
  90. virtual bool should_unblock(Thread&, time_t now_s, long us) override;
  91. virtual const char* state_string() const override { return "Joining"; }
  92. void set_joinee_exit_value(void* value) { m_joinee_exit_value = value; }
  93. private:
  94. Thread& m_joinee;
  95. void*& m_joinee_exit_value;
  96. };
  97. class FileDescriptionBlocker : public Blocker {
  98. public:
  99. const FileDescription& blocked_description() const;
  100. protected:
  101. explicit FileDescriptionBlocker(const FileDescription&);
  102. private:
  103. NonnullRefPtr<FileDescription> m_blocked_description;
  104. };
  105. class AcceptBlocker final : public FileDescriptionBlocker {
  106. public:
  107. explicit AcceptBlocker(const FileDescription&);
  108. virtual bool should_unblock(Thread&, time_t, long) override;
  109. virtual const char* state_string() const override { return "Accepting"; }
  110. };
  111. class ReceiveBlocker final : public FileDescriptionBlocker {
  112. public:
  113. explicit ReceiveBlocker(const FileDescription&);
  114. virtual bool should_unblock(Thread&, time_t, long) override;
  115. virtual const char* state_string() const override { return "Receiving"; }
  116. };
  117. class ConnectBlocker final : public FileDescriptionBlocker {
  118. public:
  119. explicit ConnectBlocker(const FileDescription&);
  120. virtual bool should_unblock(Thread&, time_t, long) override;
  121. virtual const char* state_string() const override { return "Connecting"; }
  122. };
  123. class WriteBlocker final : public FileDescriptionBlocker {
  124. public:
  125. explicit WriteBlocker(const FileDescription&);
  126. virtual bool should_unblock(Thread&, time_t, long) override;
  127. virtual const char* state_string() const override { return "Writing"; }
  128. };
  129. class ReadBlocker final : public FileDescriptionBlocker {
  130. public:
  131. explicit ReadBlocker(const FileDescription&);
  132. virtual bool should_unblock(Thread&, time_t, long) override;
  133. virtual const char* state_string() const override { return "Reading"; }
  134. };
  135. class ConditionBlocker final : public Blocker {
  136. public:
  137. ConditionBlocker(const char* state_string, Function<bool()>&& condition);
  138. virtual bool should_unblock(Thread&, time_t, long) override;
  139. virtual const char* state_string() const override { return m_state_string; }
  140. private:
  141. Function<bool()> m_block_until_condition;
  142. const char* m_state_string { nullptr };
  143. };
  144. class SleepBlocker final : public Blocker {
  145. public:
  146. explicit SleepBlocker(u64 wakeup_time);
  147. virtual bool should_unblock(Thread&, time_t, long) override;
  148. virtual const char* state_string() const override { return "Sleeping"; }
  149. private:
  150. u64 m_wakeup_time { 0 };
  151. };
  152. class SelectBlocker final : public Blocker {
  153. public:
  154. typedef Vector<int, FD_SETSIZE> FDVector;
  155. SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds);
  156. virtual bool should_unblock(Thread&, time_t, long) override;
  157. virtual const char* state_string() const override { return "Selecting"; }
  158. private:
  159. timeval m_select_timeout;
  160. bool m_select_has_timeout { false };
  161. const FDVector& m_select_read_fds;
  162. const FDVector& m_select_write_fds;
  163. const FDVector& m_select_exceptional_fds;
  164. };
  165. class WaitBlocker final : public Blocker {
  166. public:
  167. WaitBlocker(int wait_options, pid_t& waitee_pid);
  168. virtual bool should_unblock(Thread&, time_t, long) override;
  169. virtual const char* state_string() const override { return "Waiting"; }
  170. private:
  171. int m_wait_options { 0 };
  172. pid_t& m_waitee_pid;
  173. };
  174. class SemiPermanentBlocker final : public Blocker {
  175. public:
  176. enum class Reason {
  177. Signal,
  178. };
  179. SemiPermanentBlocker(Reason reason);
  180. virtual bool should_unblock(Thread&, time_t, long) override;
  181. virtual const char* state_string() const override
  182. {
  183. switch (m_reason) {
  184. case Reason::Signal:
  185. return "Signal";
  186. }
  187. ASSERT_NOT_REACHED();
  188. }
  189. private:
  190. Reason m_reason;
  191. };
  192. void did_schedule() { ++m_times_scheduled; }
  193. u32 times_scheduled() const { return m_times_scheduled; }
  194. bool is_stopped() const { return m_state == Stopped; }
  195. bool is_blocked() const { return m_state == Blocked; }
  196. bool in_kernel() const { return (m_tss.cs & 0x03) == 0; }
  197. u32 frame_ptr() const { return m_tss.ebp; }
  198. u32 stack_ptr() const { return m_tss.esp; }
  199. RegisterDump& get_register_dump_from_stack();
  200. u16 selector() const { return m_far_ptr.selector; }
  201. TSS32& tss() { return m_tss; }
  202. const TSS32& tss() const { return m_tss; }
  203. State state() const { return m_state; }
  204. const char* state_string() const;
  205. u32 ticks() const { return m_ticks; }
  206. VirtualAddress thread_specific_data() const { return m_thread_specific_data; }
  207. u64 sleep(u32 ticks);
  208. u64 sleep_until(u64 wakeup_time);
  209. enum class BlockResult {
  210. WokeNormally,
  211. InterruptedBySignal,
  212. };
  213. template<typename T, class... Args>
  214. [[nodiscard]] BlockResult block(Args&&... args)
  215. {
  216. // We should never be blocking a blocked (or otherwise non-active) thread.
  217. ASSERT(state() == Thread::Running);
  218. ASSERT(m_blocker == nullptr);
  219. T t(forward<Args>(args)...);
  220. m_blocker = &t;
  221. set_state(Thread::Blocked);
  222. // Yield to the scheduler, and wait for us to resume unblocked.
  223. yield_without_holding_big_lock();
  224. // We should no longer be blocked once we woke up
  225. ASSERT(state() != Thread::Blocked);
  226. // Remove ourselves...
  227. m_blocker = nullptr;
  228. if (t.was_interrupted_by_signal())
  229. return BlockResult::InterruptedBySignal;
  230. return BlockResult::WokeNormally;
  231. }
  232. [[nodiscard]] BlockResult block_until(const char* state_string, Function<bool()>&& condition)
  233. {
  234. return block<ConditionBlocker>(state_string, move(condition));
  235. }
  236. void wait_on(WaitQueue& queue, Thread* beneficiary = nullptr, const char* reason = nullptr);
  237. void wake_from_queue();
  238. void unblock();
  239. // Tell this thread to unblock if needed,
  240. // gracefully unwind the stack and die.
  241. void set_should_die();
  242. void die_if_needed();
  243. const FarPtr& far_ptr() const { return m_far_ptr; }
  244. bool tick();
  245. void set_ticks_left(u32 t) { m_ticks_left = t; }
  246. u32 ticks_left() const { return m_ticks_left; }
  247. u32 kernel_stack_base() const { return m_kernel_stack_base; }
  248. u32 kernel_stack_top() const { return m_kernel_stack_top; }
  249. void set_selector(u16 s) { m_far_ptr.selector = s; }
  250. void set_state(State);
  251. void send_urgent_signal_to_self(u8 signal);
  252. void send_signal(u8 signal, Process* sender);
  253. void consider_unblock(time_t now_sec, long now_usec);
  254. void set_dump_backtrace_on_finalization() { m_dump_backtrace_on_finalization = true; }
  255. ShouldUnblockThread dispatch_one_pending_signal();
  256. ShouldUnblockThread dispatch_signal(u8 signal);
  257. bool has_unmasked_pending_signals() const;
  258. void terminate_due_to_signal(u8 signal);
  259. bool should_ignore_signal(u8 signal) const;
  260. bool has_signal_handler(u8 signal) const;
  261. FPUState& fpu_state() { return *m_fpu_state; }
  262. bool has_used_fpu() const { return m_has_used_fpu; }
  263. void set_has_used_fpu(bool b) { m_has_used_fpu = b; }
  264. void set_default_signal_dispositions();
  265. void push_value_on_stack(u32);
  266. u32 make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment);
  267. void make_thread_specific_region(Badge<Process>);
  268. unsigned syscall_count() const { return m_syscall_count; }
  269. void did_syscall() { ++m_syscall_count; }
  270. unsigned inode_faults() const { return m_inode_faults; }
  271. void did_inode_fault() { ++m_inode_faults; }
  272. unsigned zero_faults() const { return m_zero_faults; }
  273. void did_zero_fault() { ++m_zero_faults; }
  274. unsigned cow_faults() const { return m_cow_faults; }
  275. void did_cow_fault() { ++m_cow_faults; }
  276. unsigned file_read_bytes() const { return m_file_read_bytes; }
  277. unsigned file_write_bytes() const { return m_file_write_bytes; }
  278. void did_file_read(unsigned bytes)
  279. {
  280. m_file_read_bytes += bytes;
  281. }
  282. void did_file_write(unsigned bytes)
  283. {
  284. m_file_write_bytes += bytes;
  285. }
  286. unsigned unix_socket_read_bytes() const { return m_unix_socket_read_bytes; }
  287. unsigned unix_socket_write_bytes() const { return m_unix_socket_write_bytes; }
  288. void did_unix_socket_read(unsigned bytes)
  289. {
  290. m_unix_socket_read_bytes += bytes;
  291. }
  292. void did_unix_socket_write(unsigned bytes)
  293. {
  294. m_unix_socket_write_bytes += bytes;
  295. }
  296. unsigned ipv4_socket_read_bytes() const { return m_ipv4_socket_read_bytes; }
  297. unsigned ipv4_socket_write_bytes() const { return m_ipv4_socket_write_bytes; }
  298. void did_ipv4_socket_read(unsigned bytes)
  299. {
  300. m_ipv4_socket_read_bytes += bytes;
  301. }
  302. void did_ipv4_socket_write(unsigned bytes)
  303. {
  304. m_ipv4_socket_write_bytes += bytes;
  305. }
  306. Thread* clone(Process&);
  307. template<typename Callback>
  308. static IterationDecision for_each_in_state(State, Callback);
  309. template<typename Callback>
  310. static IterationDecision for_each_living(Callback);
  311. template<typename Callback>
  312. static IterationDecision for_each(Callback);
  313. static bool is_runnable_state(Thread::State state)
  314. {
  315. return state == Thread::State::Running || state == Thread::State::Runnable;
  316. }
  317. static constexpr u32 default_kernel_stack_size = 65536;
  318. static constexpr u32 default_userspace_stack_size = 4 * MB;
  319. private:
  320. IntrusiveListNode m_runnable_list_node;
  321. IntrusiveListNode m_wait_queue_node;
  322. private:
  323. friend class SchedulerData;
  324. friend class WaitQueue;
  325. bool unlock_process_if_locked();
  326. void relock_process();
  327. String backtrace_impl() const;
  328. Process& m_process;
  329. int m_tid { -1 };
  330. TSS32 m_tss;
  331. FarPtr m_far_ptr;
  332. u32 m_ticks { 0 };
  333. u32 m_ticks_left { 0 };
  334. u32 m_times_scheduled { 0 };
  335. u32 m_pending_signals { 0 };
  336. u32 m_signal_mask { 0 };
  337. u32 m_kernel_stack_base { 0 };
  338. u32 m_kernel_stack_top { 0 };
  339. Region* m_userspace_stack_region { nullptr };
  340. OwnPtr<Region> m_kernel_stack_region;
  341. VirtualAddress m_thread_specific_data;
  342. SignalActionData m_signal_action_data[32];
  343. Blocker* m_blocker { nullptr };
  344. bool m_is_joinable { true };
  345. Thread* m_joiner { nullptr };
  346. Thread* m_joinee { nullptr };
  347. void* m_exit_value { nullptr };
  348. unsigned m_syscall_count { 0 };
  349. unsigned m_inode_faults { 0 };
  350. unsigned m_zero_faults { 0 };
  351. unsigned m_cow_faults { 0 };
  352. unsigned m_file_read_bytes { 0 };
  353. unsigned m_file_write_bytes { 0 };
  354. unsigned m_unix_socket_read_bytes { 0 };
  355. unsigned m_unix_socket_write_bytes { 0 };
  356. unsigned m_ipv4_socket_read_bytes { 0 };
  357. unsigned m_ipv4_socket_write_bytes { 0 };
  358. FPUState* m_fpu_state { nullptr };
  359. State m_state { Invalid };
  360. String m_name;
  361. u32 m_priority { THREAD_PRIORITY_NORMAL };
  362. u32 m_extra_priority { 0 };
  363. u32 m_priority_boost { 0 };
  364. bool m_has_used_fpu { false };
  365. bool m_dump_backtrace_on_finalization { false };
  366. bool m_should_die { false };
  367. void yield_without_holding_big_lock();
  368. };
  369. HashTable<Thread*>& thread_table();
  370. template<typename Callback>
  371. inline IterationDecision Thread::for_each_living(Callback callback)
  372. {
  373. ASSERT_INTERRUPTS_DISABLED();
  374. return Thread::for_each([callback](Thread& thread) -> IterationDecision {
  375. if (thread.state() != Thread::State::Dead && thread.state() != Thread::State::Dying)
  376. return callback(thread);
  377. return IterationDecision::Continue;
  378. });
  379. }
  380. template<typename Callback>
  381. inline IterationDecision Thread::for_each(Callback callback)
  382. {
  383. ASSERT_INTERRUPTS_DISABLED();
  384. auto ret = Scheduler::for_each_runnable(callback);
  385. if (ret == IterationDecision::Break)
  386. return ret;
  387. return Scheduler::for_each_nonrunnable(callback);
  388. }
  389. template<typename Callback>
  390. inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
  391. {
  392. ASSERT_INTERRUPTS_DISABLED();
  393. auto new_callback = [=](Thread& thread) -> IterationDecision {
  394. if (thread.state() == state)
  395. return callback(thread);
  396. return IterationDecision::Continue;
  397. };
  398. if (is_runnable_state(state))
  399. return Scheduler::for_each_runnable(new_callback);
  400. return Scheduler::for_each_nonrunnable(new_callback);
  401. }
  402. const LogStream& operator<<(const LogStream&, const Thread&);
  403. struct SchedulerData {
  404. typedef IntrusiveList<Thread, &Thread::m_runnable_list_node> ThreadList;
  405. ThreadList m_runnable_threads;
  406. ThreadList m_nonrunnable_threads;
  407. ThreadList& thread_list_for_state(Thread::State state)
  408. {
  409. if (Thread::is_runnable_state(state))
  410. return m_runnable_threads;
  411. return m_nonrunnable_threads;
  412. }
  413. };
  414. template<typename Callback>
  415. inline IterationDecision Scheduler::for_each_runnable(Callback callback)
  416. {
  417. ASSERT_INTERRUPTS_DISABLED();
  418. auto& tl = g_scheduler_data->m_runnable_threads;
  419. for (auto it = tl.begin(); it != tl.end();) {
  420. auto& thread = *it;
  421. it = ++it;
  422. if (callback(thread) == IterationDecision::Break)
  423. return IterationDecision::Break;
  424. }
  425. return IterationDecision::Continue;
  426. }
  427. template<typename Callback>
  428. inline IterationDecision Scheduler::for_each_nonrunnable(Callback callback)
  429. {
  430. ASSERT_INTERRUPTS_DISABLED();
  431. auto& tl = g_scheduler_data->m_nonrunnable_threads;
  432. for (auto it = tl.begin(); it != tl.end();) {
  433. auto& thread = *it;
  434. it = ++it;
  435. if (callback(thread) == IterationDecision::Break)
  436. return IterationDecision::Break;
  437. }
  438. return IterationDecision::Continue;
  439. }
  440. u16 thread_specific_selector();
  441. Descriptor& thread_specific_descriptor();