summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/thread.h
diff options
context:
space:
mode:
authorGravatar Kevin Hartman2015-01-25 22:56:17 -0800
committerGravatar Kevin Hartman2015-02-09 21:47:12 -0800
commit5fcbfc06eb247c0a4c1db201ac9050d80d4e4020 (patch)
tree4cdac59449b025c7a07e8655f29eda2fbcb423fe /src/core/hle/kernel/thread.h
parentMerge pull request #551 from bunnei/mutex-fixes (diff)
downloadyuzu-5fcbfc06eb247c0a4c1db201ac9050d80d4e4020.tar.gz
yuzu-5fcbfc06eb247c0a4c1db201ac9050d80d4e4020.tar.xz
yuzu-5fcbfc06eb247c0a4c1db201ac9050d80d4e4020.zip
Scheduler refactor Pt. 1
* Simplifies scheduling logic, specifically regarding thread status. It should be much clearer which statuses are valid for a thread at any given point in the system. * Removes dead code from thread.cpp. * Moves the implementation of resetting a ThreadContext to the corresponding core's implementation. Other changes: * Fixed comments in arm interfaces. * Updated comments in thread.cpp * Removed confusing, useless, functions like MakeReady() and ChangeStatus() from thread.cpp. * Removed stack_size from Thread. In the CTR kernel, the thread's stack would be allocated before thread creation.
Diffstat (limited to 'src/core/hle/kernel/thread.h')
-rw-r--r--src/core/hle/kernel/thread.h115
1 files changed, 82 insertions, 33 deletions
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index 633bb7c98..cfd073a70 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -31,13 +31,13 @@ enum ThreadProcessorId {
31}; 31};
32 32
33enum ThreadStatus { 33enum ThreadStatus {
34 THREADSTATUS_RUNNING = 1, 34 THREADSTATUS_RUNNING, ///< Currently running
35 THREADSTATUS_READY = 2, 35 THREADSTATUS_READY, ///< Ready to run
36 THREADSTATUS_WAIT = 4, 36 THREADSTATUS_WAIT_ARB, ///< Waiting on an address arbiter
37 THREADSTATUS_SUSPEND = 8, 37 THREADSTATUS_WAIT_SLEEP, ///< Waiting due to a SleepThread SVC
38 THREADSTATUS_DORMANT = 16, 38 THREADSTATUS_WAIT_SYNCH, ///< Waiting due to a WaitSynchronization SVC
39 THREADSTATUS_DEAD = 32, 39 THREADSTATUS_DORMANT, ///< Created but not yet made ready
40 THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND 40 THREADSTATUS_DEAD ///< Run to completion, or forcefully terminated
41}; 41};
42 42
43namespace Kernel { 43namespace Kernel {
@@ -46,8 +46,19 @@ class Mutex;
46 46
47class Thread final : public WaitObject { 47class Thread final : public WaitObject {
48public: 48public:
49 /**
50 * Creates and returns a new thread. The new thread is immediately scheduled
51 * @param name The friendly name desired for the thread
52 * @param entry_point The address at which the thread should start execution
53 * @param priority The thread's priority
54 * @param arg User data to pass to the thread
55 * @param processor_id The ID(s) of the processors on which the thread is desired to be run
56 * @param stack_top The address of the thread's stack top
57 * @param stack_size The size of the thread's stack
58 * @return A shared pointer to the newly created thread
59 */
49 static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority, 60 static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority,
50 u32 arg, s32 processor_id, VAddr stack_top, u32 stack_size); 61 u32 arg, s32 processor_id, VAddr stack_top);
51 62
52 std::string GetName() const override { return name; } 63 std::string GetName() const override { return name; }
53 std::string GetTypeName() const override { return "Thread"; } 64 std::string GetTypeName() const override { return "Thread"; }
@@ -55,22 +66,32 @@ public:
55 static const HandleType HANDLE_TYPE = HandleType::Thread; 66 static const HandleType HANDLE_TYPE = HandleType::Thread;
56 HandleType GetHandleType() const override { return HANDLE_TYPE; } 67 HandleType GetHandleType() const override { return HANDLE_TYPE; }
57 68
58 inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
59 inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
60 inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
61 inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
62 inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
63 inline bool IsIdle() const { return idle; }
64
65 bool ShouldWait() override; 69 bool ShouldWait() override;
66 void Acquire() override; 70 void Acquire() override;
67 71
72 /**
73 * Checks if the thread is an idle (stub) thread
74 * @return True if the thread is an idle (stub) thread, false otherwise
75 */
76 inline bool IsIdle() const { return idle; }
77
78 /**
79 * Gets the thread's current priority
80 * @return The current thread's priority
81 */
68 s32 GetPriority() const { return current_priority; } 82 s32 GetPriority() const { return current_priority; }
83
84 /**
85 * Sets the thread's current priority
86 * @param priority The new priority
87 */
69 void SetPriority(s32 priority); 88 void SetPriority(s32 priority);
70 89
90 /**
91 * Gets the thread's thread ID
92 * @return The thread's ID
93 */
71 u32 GetThreadId() const { return thread_id; } 94 u32 GetThreadId() const { return thread_id; }
72
73 void Stop(const char* reason);
74 95
75 /** 96 /**
76 * Release an acquired wait object 97 * Release an acquired wait object
@@ -78,12 +99,14 @@ public:
78 */ 99 */
79 void ReleaseWaitObject(WaitObject* wait_object); 100 void ReleaseWaitObject(WaitObject* wait_object);
80 101
81 /// Resumes a thread from waiting by marking it as "ready" 102 /**
103 * Resumes a thread from waiting
104 */
82 void ResumeFromWait(); 105 void ResumeFromWait();
83 106
84 /** 107 /**
85 * Schedules an event to wake up the specified thread after the specified delay. 108 * Schedules an event to wake up the specified thread after the specified delay
86 * @param nanoseconds The time this thread will be allowed to sleep for. 109 * @param nanoseconds The time this thread will be allowed to sleep for
87 */ 110 */
88 void WakeAfterDelay(s64 nanoseconds); 111 void WakeAfterDelay(s64 nanoseconds);
89 112
@@ -99,6 +122,11 @@ public:
99 */ 122 */
100 void SetWaitSynchronizationOutput(s32 output); 123 void SetWaitSynchronizationOutput(s32 output);
101 124
125 /**
126 * Stops a thread, invalidating it from further use
127 */
128 void Stop();
129
102 Core::ThreadContext context; 130 Core::ThreadContext context;
103 131
104 u32 thread_id; 132 u32 thread_id;
@@ -106,7 +134,6 @@ public:
106 u32 status; 134 u32 status;
107 u32 entry_point; 135 u32 entry_point;
108 u32 stack_top; 136 u32 stack_top;
109 u32 stack_size;
110 137
111 s32 initial_priority; 138 s32 initial_priority;
112 s32 current_priority; 139 s32 current_priority;
@@ -136,31 +163,49 @@ private:
136 163
137extern SharedPtr<Thread> g_main_thread; 164extern SharedPtr<Thread> g_main_thread;
138 165
139/// Sets up the primary application thread 166/**
140SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size); 167 * Sets up the primary application thread
168 * @param stack_size The size of the thread's stack
169 * @param entry_point The address at which the thread should start execution
170 * @param priority The priority to give the main thread
171 * @return A shared pointer to the main thread
172 */
173SharedPtr<Thread> SetupMainThread(u32 stack_size, u32 entry_point, s32 priority);
141 174
142/// Reschedules to the next available thread (call after current thread is suspended) 175/**
176 * Reschedules to the next available thread (call after current thread is suspended)
177 */
143void Reschedule(); 178void Reschedule();
144 179
145/// Arbitrate the highest priority thread that is waiting 180/**
181 * Arbitrate the highest priority thread that is waiting
182 * @param address The address for which waiting threads should be arbitrated
183 */
146Thread* ArbitrateHighestPriorityThread(u32 address); 184Thread* ArbitrateHighestPriorityThread(u32 address);
147 185
148/// Arbitrate all threads currently waiting... 186/**
187 * Arbitrate all threads currently waiting.
188 * @param address The address for which waiting threads should be arbitrated
189 */
149void ArbitrateAllThreads(u32 address); 190void ArbitrateAllThreads(u32 address);
150 191
151/// Gets the current thread 192/**
193 * Gets the current thread
194 */
152Thread* GetCurrentThread(); 195Thread* GetCurrentThread();
153 196
154/// Waits the current thread on a sleep 197/**
198 * Waits the current thread on a sleep
199 */
155void WaitCurrentThread_Sleep(); 200void WaitCurrentThread_Sleep();
156 201
157/** 202/**
158 * Waits the current thread from a WaitSynchronization call 203 * Waits the current thread from a WaitSynchronization call
159 * @param wait_object Kernel object that we are waiting on 204 * @param wait_objects Kernel objects that we are waiting on
160 * @param wait_set_output If true, set the output parameter on thread wakeup (for WaitSynchronizationN only) 205 * @param wait_set_output If true, set the output parameter on thread wakeup (for WaitSynchronizationN only)
161 * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only) 206 * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
162 */ 207 */
163void WaitCurrentThread_WaitSynchronization(SharedPtr<WaitObject> wait_object, bool wait_set_output, bool wait_all); 208void WaitCurrentThread_WaitSynchronization(std::vector<SharedPtr<WaitObject>> wait_objects, bool wait_set_output, bool wait_all);
164 209
165/** 210/**
166 * Waits the current thread from an ArbitrateAddress call 211 * Waits the current thread from an ArbitrateAddress call
@@ -172,14 +217,18 @@ void WaitCurrentThread_ArbitrateAddress(VAddr wait_address);
172 * Sets up the idle thread, this is a thread that is intended to never execute instructions, 217 * Sets up the idle thread, this is a thread that is intended to never execute instructions,
173 * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue 218 * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
174 * and will try to yield on every call. 219 * and will try to yield on every call.
175 * @returns The handle of the idle thread 220 * @return The handle of the idle thread
176 */ 221 */
177SharedPtr<Thread> SetupIdleThread(); 222SharedPtr<Thread> SetupIdleThread();
178 223
179/// Initialize threading 224/**
225 * Initialize threading
226 */
180void ThreadingInit(); 227void ThreadingInit();
181 228
182/// Shutdown threading 229/**
230 * Shutdown threading
231 */
183void ThreadingShutdown(); 232void ThreadingShutdown();
184 233
185} // namespace 234} // namespace