thread.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2014 Citra Emulator Project / PPSSPP Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <string>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "core/core.h"
  9. #include "core/mem_map.h"
  10. #include "core/hle/kernel/kernel.h"
  11. #include "core/hle/result.h"
  12. enum ThreadPriority {
  13. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  14. THREADPRIO_DEFAULT = 16, ///< Default thread priority for userland apps
  15. THREADPRIO_LOW = 31, ///< Low range of thread priority for userland apps
  16. THREADPRIO_LOWEST = 63, ///< Thread priority max checked by svcCreateThread
  17. };
  18. enum ThreadProcessorId {
  19. THREADPROCESSORID_0 = 0xFFFFFFFE, ///< Enables core appcode
  20. THREADPROCESSORID_1 = 0xFFFFFFFD, ///< Enables core syscore
  21. THREADPROCESSORID_ALL = 0xFFFFFFFC, ///< Enables both cores
  22. };
  23. enum ThreadStatus {
  24. THREADSTATUS_RUNNING = 1,
  25. THREADSTATUS_READY = 2,
  26. THREADSTATUS_WAIT = 4,
  27. THREADSTATUS_SUSPEND = 8,
  28. THREADSTATUS_DORMANT = 16,
  29. THREADSTATUS_DEAD = 32,
  30. THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
  31. };
  32. enum WaitType {
  33. WAITTYPE_NONE,
  34. WAITTYPE_SLEEP,
  35. WAITTYPE_SEMA,
  36. WAITTYPE_EVENT,
  37. WAITTYPE_THREADEND,
  38. WAITTYPE_MUTEX,
  39. WAITTYPE_SYNCH,
  40. WAITTYPE_ARB,
  41. WAITTYPE_TIMER,
  42. };
  43. namespace Kernel {
  44. class Thread : public WaitObject {
  45. public:
  46. static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority,
  47. u32 arg, s32 processor_id, VAddr stack_top, u32 stack_size);
  48. std::string GetName() const override { return name; }
  49. std::string GetTypeName() const override { return "Thread"; }
  50. static const HandleType HANDLE_TYPE = HandleType::Thread;
  51. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  52. inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
  53. inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
  54. inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
  55. inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
  56. inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
  57. inline bool IsIdle() const { return idle; }
  58. ResultVal<bool> Wait(bool wait_thread) override;
  59. ResultVal<bool> Acquire() override;
  60. s32 GetPriority() const { return current_priority; }
  61. void SetPriority(s32 priority);
  62. u32 GetThreadId() const { return thread_id; }
  63. void Stop(const char* reason);
  64. /**
  65. * Release an object from the thread's wait list
  66. * @param wait_object WaitObject to release from the thread's wait list
  67. */
  68. void ReleaseFromWait(WaitObject* wait_object);
  69. /// Resumes a thread from waiting by marking it as "ready"
  70. void ResumeFromWait();
  71. /**
  72. * Sets the waiting mode of the thread
  73. * @param wait_all If true, wait for all objects, otherwise just wait for the first one
  74. */
  75. void SetWaitAll(bool wait_all) { this->wait_all = wait_all; }
  76. /**
  77. * Sets the output values after the thread awakens from WaitSynchronization
  78. * @param return_val Value returned
  79. * @param out_val Value to set to the output parameter
  80. */
  81. void SetReturnValue(ResultCode return_val, s32 out_val);
  82. Core::ThreadContext context;
  83. u32 thread_id;
  84. u32 status;
  85. u32 entry_point;
  86. u32 stack_top;
  87. u32 stack_size;
  88. s32 initial_priority;
  89. s32 current_priority;
  90. s32 processor_id;
  91. WaitType wait_type;
  92. std::vector<SharedPtr<WaitObject>> wait_objects;
  93. VAddr wait_address;
  94. std::string name;
  95. /// Whether this thread is intended to never actually be executed, i.e. always idle
  96. bool idle = false;
  97. private:
  98. bool wait_all = false;
  99. Thread() = default;
  100. };
  101. /// Sets up the primary application thread
  102. SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size);
  103. /// Reschedules to the next available thread (call after current thread is suspended)
  104. void Reschedule();
  105. /// Arbitrate the highest priority thread that is waiting
  106. Thread* ArbitrateHighestPriorityThread(WaitObject* arbiter, u32 address);
  107. /// Arbitrate all threads currently waiting...
  108. void ArbitrateAllThreads(WaitObject* arbiter, u32 address);
  109. /// Gets the current thread
  110. Thread* GetCurrentThread();
  111. /**
  112. * Waits the current thread for the given type
  113. * @param wait_type Type of wait
  114. */
  115. void WaitCurrentThread(WaitType wait_type);
  116. /**
  117. * Waits the current thread from a WaitSynchronization call
  118. * @param wait_type Type of wait
  119. * @param wait_object Kernel object that we are waiting on
  120. * @param index Index of calling object (for WaitSynchronizationN only)
  121. */
  122. void WaitCurrentThread_WaitSynchronization(WaitType wait_type, WaitObject* wait_object, unsigned index=0);
  123. /**
  124. * Waits the current thread from an ArbitrateAddress call
  125. * @param wait_object Kernel object that we are waiting on
  126. * @param wait_address Arbitration address used to resume from wait
  127. */
  128. void WaitCurrentThread_ArbitrateAddress(WaitObject* wait_object, VAddr wait_address);
  129. /**
  130. * Schedules an event to wake up the specified thread after the specified delay.
  131. * @param handle The thread handle.
  132. * @param nanoseconds The time this thread will be allowed to sleep for.
  133. */
  134. void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds);
  135. /**
  136. * Sets up the idle thread, this is a thread that is intended to never execute instructions,
  137. * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
  138. * and will try to yield on every call.
  139. * @returns The handle of the idle thread
  140. */
  141. Handle SetupIdleThread();
  142. /// Initialize threading
  143. void ThreadingInit();
  144. /// Shutdown threading
  145. void ThreadingShutdown();
  146. } // namespace