thread.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <boost/container/flat_set.hpp>
  8. #include "common/common_types.h"
  9. #include "core/core.h"
  10. #include "core/mem_map.h"
  11. #include "core/hle/kernel/kernel.h"
  12. #include "core/hle/result.h"
  13. enum ThreadPriority {
  14. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  15. THREADPRIO_DEFAULT = 16, ///< Default thread priority for userland apps
  16. THREADPRIO_LOW = 31, ///< Low range of thread priority for userland apps
  17. THREADPRIO_LOWEST = 63, ///< Thread priority max checked by svcCreateThread
  18. };
  19. enum ThreadProcessorId {
  20. THREADPROCESSORID_0 = 0xFFFFFFFE, ///< Enables core appcode
  21. THREADPROCESSORID_1 = 0xFFFFFFFD, ///< Enables core syscore
  22. THREADPROCESSORID_ALL = 0xFFFFFFFC, ///< Enables both cores
  23. };
  24. enum ThreadStatus {
  25. THREADSTATUS_RUNNING = 1,
  26. THREADSTATUS_READY = 2,
  27. THREADSTATUS_WAIT = 4,
  28. THREADSTATUS_SUSPEND = 8,
  29. THREADSTATUS_DORMANT = 16,
  30. THREADSTATUS_DEAD = 32,
  31. THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
  32. };
  33. namespace Kernel {
  34. class Mutex;
  35. class Thread final : public WaitObject {
  36. public:
  37. static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority,
  38. u32 arg, s32 processor_id, VAddr stack_top, u32 stack_size);
  39. std::string GetName() const override { return name; }
  40. std::string GetTypeName() const override { return "Thread"; }
  41. static const HandleType HANDLE_TYPE = HandleType::Thread;
  42. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  43. inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
  44. inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
  45. inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
  46. inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
  47. inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
  48. inline bool IsIdle() const { return idle; }
  49. bool ShouldWait() override;
  50. void Acquire() override;
  51. s32 GetPriority() const { return current_priority; }
  52. void SetPriority(s32 priority);
  53. u32 GetThreadId() const { return thread_id; }
  54. void Stop(const char* reason);
  55. /**
  56. * Release an acquired wait object
  57. * @param wait_object WaitObject to release
  58. */
  59. void ReleaseWaitObject(WaitObject* wait_object);
  60. /// Resumes a thread from waiting by marking it as "ready"
  61. void ResumeFromWait();
  62. /**
  63. * Schedules an event to wake up the specified thread after the specified delay.
  64. * @param nanoseconds The time this thread will be allowed to sleep for.
  65. */
  66. void WakeAfterDelay(s64 nanoseconds);
  67. /**
  68. * Sets the result after the thread awakens (from either WaitSynchronization SVC)
  69. * @param result Value to set to the returned result
  70. */
  71. void SetWaitSynchronizationResult(ResultCode result);
  72. /**
  73. * Sets the output parameter value after the thread awakens (from WaitSynchronizationN SVC only)
  74. * @param output Value to set to the output parameter
  75. */
  76. void SetWaitSynchronizationOutput(s32 output);
  77. Core::ThreadContext context;
  78. u32 thread_id;
  79. u32 status;
  80. u32 entry_point;
  81. u32 stack_top;
  82. u32 stack_size;
  83. s32 initial_priority;
  84. s32 current_priority;
  85. s32 processor_id;
  86. /// Mutexes currently held by this thread, which will be released when it exits.
  87. boost::container::flat_set<SharedPtr<Mutex>> held_mutexes;
  88. std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
  89. VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
  90. bool wait_all; ///< True if the thread is waiting on all objects before resuming
  91. bool wait_set_output; ///< True if the output parameter should be set on thread wakeup
  92. std::string name;
  93. /// Whether this thread is intended to never actually be executed, i.e. always idle
  94. bool idle = false;
  95. private:
  96. Thread();
  97. ~Thread() override;
  98. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  99. Handle callback_handle;
  100. };
  101. extern SharedPtr<Thread> g_main_thread;
  102. /// Sets up the primary application thread
  103. SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size);
  104. /// Reschedules to the next available thread (call after current thread is suspended)
  105. void Reschedule();
  106. /// Arbitrate the highest priority thread that is waiting
  107. Thread* ArbitrateHighestPriorityThread(u32 address);
  108. /// Arbitrate all threads currently waiting...
  109. void ArbitrateAllThreads(u32 address);
  110. /// Gets the current thread
  111. Thread* GetCurrentThread();
  112. /// Waits the current thread on a sleep
  113. void WaitCurrentThread_Sleep();
  114. /**
  115. * Waits the current thread from a WaitSynchronization call
  116. * @param wait_object Kernel object that we are waiting on
  117. * @param wait_set_output If true, set the output parameter on thread wakeup (for WaitSynchronizationN only)
  118. * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
  119. */
  120. void WaitCurrentThread_WaitSynchronization(SharedPtr<WaitObject> wait_object, bool wait_set_output, bool wait_all);
  121. /**
  122. * Waits the current thread from an ArbitrateAddress call
  123. * @param wait_address Arbitration address used to resume from wait
  124. */
  125. void WaitCurrentThread_ArbitrateAddress(VAddr wait_address);
  126. /**
  127. * Sets up the idle thread, this is a thread that is intended to never execute instructions,
  128. * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
  129. * and will try to yield on every call.
  130. * @returns The handle of the idle thread
  131. */
  132. SharedPtr<Thread> SetupIdleThread();
  133. /// Initialize threading
  134. void ThreadingInit();
  135. /// Shutdown threading
  136. void ThreadingShutdown();
  137. } // namespace