1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
|
/*
* 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.
*/
#pragma once
#include <AK/Function.h>
#include <AK/IntrusiveList.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <AK/Time.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <AK/Weakable.h>
#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Forward.h>
#include <Kernel/KResult.h>
#include <Kernel/Scheduler.h>
#include <Kernel/ThreadTracer.h>
#include <Kernel/TimerQueue.h>
#include <Kernel/UnixTypes.h>
#include <LibC/fd_set.h>
#include <LibELF/AuxiliaryVector.h>
namespace Kernel {
enum class ShouldUnblockThread {
No = 0,
Yes
};
struct SignalActionData {
VirtualAddress handler_or_sigaction;
u32 mask { 0 };
int flags { 0 };
};
struct ThreadSpecificData {
ThreadSpecificData* self;
};
#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
#define THREAD_AFFINITY_DEFAULT 0xffffffff
class Thread
: public RefCounted<Thread>
, public Weakable<Thread> {
AK_MAKE_NONCOPYABLE(Thread);
AK_MAKE_NONMOVABLE(Thread);
friend class Process;
friend class Scheduler;
public:
inline static Thread* current()
{
return Processor::current().current_thread();
}
explicit Thread(NonnullRefPtr<Process>);
~Thread();
static RefPtr<Thread> from_tid(ThreadID);
static void finalize_dying_threads();
ThreadID tid() const { return m_tid; }
ProcessID pid() const;
void set_priority(u32 p) { m_priority = p; }
u32 priority() const { return m_priority; }
void set_priority_boost(u32 boost) { m_priority_boost = boost; }
u32 priority_boost() const { return m_priority_boost; }
u32 effective_priority() const;
KResult try_join(Thread& joiner)
{
if (&joiner == this)
return KResult(-EDEADLK);
ScopedSpinLock lock(m_lock);
if (!m_is_joinable || state() == Dead)
return KResult(-EINVAL);
Thread* expected = nullptr;
if (!m_joiner.compare_exchange_strong(expected, &joiner, AK::memory_order_acq_rel))
return KResult(-EINVAL);
// 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;
}
void join_done()
{
// To avoid possible deadlocking, this function must not acquire
// m_lock. This deadlock could occur if the joiner times out
// almost at the same time as this thread, and calls into this
// function to clear the joiner.
m_joiner.store(nullptr, AK::memory_order_release);
}
void detach()
{
ScopedSpinLock lock(m_lock);
m_is_joinable = false;
}
bool is_joinable() const
{
ScopedSpinLock lock(m_lock);
return m_is_joinable;
}
Process& process() { return m_process; }
const Process& process() const { return m_process; }
String backtrace();
Vector<FlatPtr> raw_backtrace(FlatPtr ebp, FlatPtr eip) const;
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);
}
void finalize();
enum State : u8 {
Invalid = 0,
Runnable,
Running,
Dying,
Dead,
Stopped,
Blocked,
Queued,
};
class BlockResult {
public:
enum Type {
WokeNormally,
NotBlocked,
InterruptedBySignal,
InterruptedByDeath,
InterruptedByTimeout,
};
BlockResult() = delete;
BlockResult(Type type)
: m_type(type)
{
}
bool operator==(Type type) const
{
return m_type == type;
}
bool operator!=(Type type) const
{
return m_type != type;
}
bool was_interrupted() const
{
switch (m_type) {
case InterruptedBySignal:
case InterruptedByDeath:
return true;
default:
return false;
}
}
bool timed_out() const
{
return m_type == InterruptedByTimeout;
}
private:
Type m_type;
};
class BlockTimeout {
public:
BlockTimeout()
: m_infinite(true)
{
}
BlockTimeout(std::nullptr_t)
: m_infinite(true)
{
}
explicit BlockTimeout(bool is_absolute, const timeval* time, const timespec* start_time = nullptr)
: m_infinite(!time)
{
if (!m_infinite) {
if (time->tv_sec > 0 || time->tv_usec > 0) {
timeval_to_timespec(*time, m_time);
m_should_block = true;
}
m_start_time = start_time ? *start_time : TimeManagement::the().monotonic_time();
if (!is_absolute)
timespec_add(m_time, m_start_time, m_time);
}
}
explicit BlockTimeout(bool is_absolute, const timespec* time, const timespec* start_time = nullptr)
: m_infinite(!time)
{
if (!m_infinite) {
if (time->tv_sec > 0 || time->tv_nsec > 0) {
m_time = *time;
m_should_block = true;
}
m_start_time = start_time ? *start_time : TimeManagement::the().monotonic_time();
if (!is_absolute)
timespec_add(m_time, m_start_time, m_time);
}
}
const timespec& absolute_time() const { return m_time; }
const timespec* start_time() const { return !m_infinite ? &m_start_time : nullptr; }
bool is_infinite() const { return m_infinite; }
bool should_block() const { return m_infinite || m_should_block; };
private:
timespec m_time { 0, 0 };
timespec m_start_time { 0, 0 };
bool m_infinite { false };
bool m_should_block { false };
};
class Blocker {
public:
virtual ~Blocker() { }
virtual bool should_unblock(Thread&) = 0;
virtual const char* state_string() const = 0;
virtual bool is_reason_signal() const { return false; }
virtual const BlockTimeout& override_timeout(const BlockTimeout& timeout) { return timeout; }
virtual void was_unblocked() { }
void set_interrupted_by_death()
{
ScopedSpinLock lock(m_lock);
m_was_interrupted_by_death = true;
}
void set_interrupted_by_signal()
{
ScopedSpinLock lock(m_lock);
m_was_interrupted_while_blocked = true;
}
virtual Thread::BlockResult block_result(bool did_timeout)
{
ScopedSpinLock lock(m_lock);
if (m_was_interrupted_by_death)
return Thread::BlockResult::InterruptedByDeath;
if (m_was_interrupted_while_blocked)
return Thread::BlockResult::InterruptedBySignal;
if (did_timeout)
return Thread::BlockResult::InterruptedByTimeout;
return Thread::BlockResult::WokeNormally;
}
protected:
mutable RecursiveSpinLock m_lock;
private:
bool m_was_interrupted_while_blocked { false };
bool m_was_interrupted_by_death { false };
friend class Thread;
};
class JoinBlocker final : public Blocker {
public:
explicit JoinBlocker(Thread& joinee, KResult& try_join_result, void*& joinee_exit_value);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Joining"; }
virtual void was_unblocked() override;
void joinee_exited(void* value);
private:
Thread* m_joinee;
void*& m_joinee_exit_value;
bool m_join_error { false };
};
class FileDescriptionBlocker : public Blocker {
public:
const FileDescription& blocked_description() const;
protected:
explicit FileDescriptionBlocker(const FileDescription&);
private:
NonnullRefPtr<FileDescription> m_blocked_description;
};
class AcceptBlocker final : public FileDescriptionBlocker {
public:
explicit AcceptBlocker(const FileDescription&);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Accepting"; }
};
class ConnectBlocker final : public FileDescriptionBlocker {
public:
explicit ConnectBlocker(const FileDescription&);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Connecting"; }
};
class WriteBlocker final : public FileDescriptionBlocker {
public:
explicit WriteBlocker(const FileDescription&);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Writing"; }
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
private:
BlockTimeout m_timeout;
};
class ReadBlocker final : public FileDescriptionBlocker {
public:
explicit ReadBlocker(const FileDescription&);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Reading"; }
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
private:
BlockTimeout m_timeout;
};
class ConditionBlocker final : public Blocker {
public:
ConditionBlocker(const char* state_string, Function<bool()>&& condition);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return m_state_string; }
private:
Function<bool()> m_block_until_condition;
const char* m_state_string { nullptr };
};
class SleepBlocker final : public Blocker {
public:
explicit SleepBlocker(const BlockTimeout&, timespec* = nullptr);
virtual bool should_unblock(Thread&) override { return false; }
virtual const char* state_string() const override { return "Sleeping"; }
virtual const BlockTimeout& override_timeout(const BlockTimeout&) override;
virtual void was_unblocked() override;
virtual Thread::BlockResult block_result(bool) override;
private:
BlockTimeout m_deadline;
timespec* m_remaining;
};
class SelectBlocker final : public Blocker {
public:
typedef Vector<int, FD_SETSIZE> FDVector;
SelectBlocker(const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Selecting"; }
private:
const FDVector& m_select_read_fds;
const FDVector& m_select_write_fds;
const FDVector& m_select_exceptional_fds;
};
class WaitBlocker final : public Blocker {
public:
WaitBlocker(int wait_options, ProcessID& waitee_pid);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override { return "Waiting"; }
private:
int m_wait_options { 0 };
ProcessID& m_waitee_pid;
};
class SemiPermanentBlocker final : public Blocker {
public:
enum class Reason {
Signal,
};
SemiPermanentBlocker(Reason reason);
virtual bool should_unblock(Thread&) override;
virtual const char* state_string() const override
{
switch (m_reason) {
case Reason::Signal:
return "Signal";
}
ASSERT_NOT_REACHED();
}
virtual bool is_reason_signal() const override { return m_reason == Reason::Signal; }
private:
Reason m_reason;
};
void did_schedule() { ++m_times_scheduled; }
u32 times_scheduled() const { return m_times_scheduled; }
void resume_from_stopped();
bool is_stopped() const { return m_state == Stopped; }
bool is_blocked() const { return m_state == Blocked; }
bool has_blocker() const
{
ASSERT(m_lock.own_lock());
return m_blocker != nullptr;
}
const Blocker& blocker() const;
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); }
u32 affinity() const { return m_cpu_affinity; }
void set_affinity(u32 affinity) { m_cpu_affinity = affinity; }
u32 stack_ptr() const { return m_tss.esp; }
RegisterState& get_register_dump_from_stack();
TSS32& tss() { return m_tss; }
const TSS32& tss() const { return m_tss; }
State state() const { return m_state; }
const char* state_string() const;
u32 ticks() const { return m_ticks; }
VirtualAddress thread_specific_data() const { return m_thread_specific_data; }
size_t thread_specific_region_size() const { return m_thread_specific_region_size; }
template<typename T, class... Args>
[[nodiscard]] BlockResult block(const BlockTimeout& timeout, Args&&... args)
{
T t(forward<Args>(args)...);
bool did_timeout = false;
RefPtr<Timer> timer;
{
ScopedSpinLock scheduler_lock(g_scheduler_lock);
ScopedSpinLock lock(m_lock);
// We should never be blocking a blocked (or otherwise non-active) thread.
ASSERT(state() == Thread::Running);
ASSERT(m_blocker == nullptr);
m_blocker = &t;
if (t.should_unblock(*this)) {
// Don't block if the wake condition is already met
t.was_unblocked();
m_blocker = nullptr;
return BlockResult::NotBlocked;
}
auto& block_timeout = t.override_timeout(timeout);
if (!block_timeout.is_infinite()) {
m_blocker_timeout = timer = TimerQueue::the().add_timer_without_id(block_timeout.absolute_time(), [&]() {
// NOTE: this may execute on the same or any other processor!
ScopedSpinLock scheduler_lock(g_scheduler_lock);
ScopedSpinLock lock(m_lock);
if (m_blocker) {
m_blocker_timeout = nullptr;
unblock();
}
});
if (!m_blocker_timeout) {
// Timeout is already in the past
t.was_unblocked();
m_blocker = nullptr;
return BlockResult::InterruptedByTimeout;
}
} else {
m_blocker_timeout = nullptr;
}
set_state(Thread::Blocked);
}
// Yield to the scheduler, and wait for us to resume unblocked.
yield_without_holding_big_lock();
{
ScopedSpinLock scheduler_lock(g_scheduler_lock);
ScopedSpinLock lock(m_lock);
// We should no longer be blocked once we woke up
ASSERT(state() != Thread::Blocked);
// Remove ourselves...
m_blocker = nullptr;
if (timer && !m_blocker_timeout)
did_timeout = true;
}
if (timer && !did_timeout) {
// Cancel the timer while not holding any locks. This allows
// the timer function to complete before we remove it
// (e.g. if it's on another processor)
TimerQueue::the().cancel_timer(timer.release_nonnull());
}
// Notify the blocker that we are no longer blocking. It may need
// to clean up now while we're still holding m_lock
t.was_unblocked();
return t.block_result(did_timeout);
}
[[nodiscard]] BlockResult block_until(const char* state_string, Function<bool()>&& condition)
{
return block<ConditionBlocker>(nullptr, state_string, move(condition));
}
BlockResult wait_on(WaitQueue& queue, const char* reason, const BlockTimeout& = nullptr, Atomic<bool>* lock = nullptr, RefPtr<Thread> beneficiary = {});
void wake_from_queue();
void unblock();
BlockResult sleep(const timespec&, timespec* = nullptr);
BlockResult sleep_until(const timespec&);
// Tell this thread to unblock if needed,
// gracefully unwind the stack and die.
void set_should_die();
bool should_die() const { return m_should_die; }
void die_if_needed();
void exit(void* = nullptr);
bool tick();
void set_ticks_left(u32 t) { m_ticks_left = t; }
u32 ticks_left() const { return m_ticks_left; }
u32 kernel_stack_base() const { return m_kernel_stack_base; }
u32 kernel_stack_top() const { return m_kernel_stack_top; }
void set_state(State);
bool is_initialized() const { return m_initialized; }
void set_initialized(bool initialized) { m_initialized = initialized; }
void send_urgent_signal_to_self(u8 signal);
void send_signal(u8 signal, Process* sender);
void consider_unblock();
u32 update_signal_mask(u32 signal_mask);
u32 signal_mask_block(sigset_t signal_set, bool block);
u32 signal_mask() const;
void clear_signals();
void set_dump_backtrace_on_finalization() { m_dump_backtrace_on_finalization = true; }
ShouldUnblockThread dispatch_one_pending_signal();
ShouldUnblockThread dispatch_signal(u8 signal);
bool has_unmasked_pending_signals() const { return m_have_any_unmasked_pending_signals.load(AK::memory_order_consume); }
void terminate_due_to_signal(u8 signal);
bool should_ignore_signal(u8 signal) const;
bool has_signal_handler(u8 signal) const;
bool has_pending_signal(u8 signal) const;
u32 pending_signals() const;
FPUState& fpu_state() { return *m_fpu_state; }
void set_default_signal_dispositions();
bool push_value_on_stack(FlatPtr);
KResultOr<u32> make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment, Vector<AuxiliaryValue>);
KResult make_thread_specific_region(Badge<Process>);
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; }
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;
}
const char* wait_reason() const
{
return m_wait_reason;
}
void set_active(bool active)
{
m_is_active.store(active, AK::memory_order_release);
}
bool is_finalizable() const
{
// 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.
if (m_is_active.load(AK::memory_order_consume))
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;
}
RefPtr<Thread> clone(Process&);
template<typename Callback>
static IterationDecision for_each_in_state(State, Callback);
template<typename Callback>
static IterationDecision for_each_living(Callback);
template<typename Callback>
static IterationDecision for_each(Callback);
static bool is_runnable_state(Thread::State state)
{
return state == Thread::State::Running || state == Thread::State::Runnable;
}
static constexpr u32 default_kernel_stack_size = 65536;
static constexpr u32 default_userspace_stack_size = 4 * MiB;
ThreadTracer* tracer() { return m_tracer.ptr(); }
void start_tracing_from(ProcessID tracer);
void stop_tracing();
void tracer_trap(const RegisterState&);
RecursiveSpinLock& get_lock() const { return m_lock; }
private:
IntrusiveListNode m_runnable_list_node;
IntrusiveListNode m_wait_queue_node;
private:
friend struct SchedulerData;
friend class WaitQueue;
bool unlock_process_if_locked();
void relock_process(bool did_unlock);
String backtrace_impl();
void reset_fpu_state();
mutable RecursiveSpinLock m_lock;
NonnullRefPtr<Process> m_process;
ThreadID m_tid { -1 };
TSS32 m_tss;
Atomic<u32> m_cpu { 0 };
u32 m_cpu_affinity { THREAD_AFFINITY_DEFAULT };
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 };
u32 m_kernel_stack_top { 0 };
OwnPtr<Region> m_kernel_stack_region;
VirtualAddress m_thread_specific_data;
size_t m_thread_specific_region_size { 0 };
SignalActionData m_signal_action_data[32];
Blocker* m_blocker { nullptr };
RefPtr<Timer> m_blocker_timeout;
const char* m_wait_reason { nullptr };
WaitQueue* m_queue { nullptr };
Atomic<bool> m_is_active { false };
bool m_is_joinable { true };
Atomic<Thread*> m_joiner { nullptr };
void* m_exit_value { nullptr };
unsigned m_syscall_count { 0 };
unsigned m_inode_faults { 0 };
unsigned m_zero_faults { 0 };
unsigned m_cow_faults { 0 };
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 };
FPUState* m_fpu_state { nullptr };
State m_state { Invalid };
String m_name;
u32 m_priority { THREAD_PRIORITY_NORMAL };
u32 m_extra_priority { 0 };
u32 m_priority_boost { 0 };
u8 m_stop_signal { 0 };
State m_stop_state { Invalid };
bool m_dump_backtrace_on_finalization { false };
bool m_should_die { false };
bool m_initialized { false };
Atomic<bool> m_have_any_unmasked_pending_signals { false };
OwnPtr<ThreadTracer> m_tracer;
void yield_without_holding_big_lock();
void update_state_for_thread(Thread::State previous_state);
};
template<typename Callback>
inline IterationDecision Thread::for_each_living(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
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;
});
}
template<typename Callback>
inline IterationDecision Thread::for_each(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
ScopedSpinLock lock(g_scheduler_lock);
auto ret = Scheduler::for_each_runnable(callback);
if (ret == IterationDecision::Break)
return ret;
return Scheduler::for_each_nonrunnable(callback);
}
template<typename Callback>
inline IterationDecision Thread::for_each_in_state(State state, Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
ScopedSpinLock lock(g_scheduler_lock);
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);
}
const LogStream& operator<<(const LogStream&, const Thread&);
struct SchedulerData {
typedef IntrusiveList<Thread, &Thread::m_runnable_list_node> ThreadList;
ThreadList m_runnable_threads;
ThreadList m_nonrunnable_threads;
bool has_thread(Thread& thread) const
{
return m_runnable_threads.contains(thread) || m_nonrunnable_threads.contains(thread);
}
ThreadList& thread_list_for_state(Thread::State state)
{
if (Thread::is_runnable_state(state))
return m_runnable_threads;
return m_nonrunnable_threads;
}
};
template<typename Callback>
inline IterationDecision Scheduler::for_each_runnable(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
ASSERT(g_scheduler_lock.own_lock());
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;
}
return IterationDecision::Continue;
}
template<typename Callback>
inline IterationDecision Scheduler::for_each_nonrunnable(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
ASSERT(g_scheduler_lock.own_lock());
auto& tl = g_scheduler_data->m_nonrunnable_threads;
for (auto it = tl.begin(); it != tl.end();) {
auto& thread = *it;
it = ++it;
if (callback(thread) == IterationDecision::Break)
return IterationDecision::Break;
}
return IterationDecision::Continue;
}
}
|