thread.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. namespace Kernel {
  33. class Thread : public WaitObject {
  34. public:
  35. static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority,
  36. u32 arg, s32 processor_id, VAddr stack_top, u32 stack_size);
  37. std::string GetName() const override { return name; }
  38. std::string GetTypeName() const override { return "Thread"; }
  39. static const HandleType HANDLE_TYPE = HandleType::Thread;
  40. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  41. inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
  42. inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
  43. inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
  44. inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
  45. inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
  46. inline bool IsIdle() const { return idle; }
  47. bool ShouldWait() override;
  48. void Acquire() override;
  49. s32 GetPriority() const { return current_priority; }
  50. void SetPriority(s32 priority);
  51. u32 GetThreadId() const { return thread_id; }
  52. void Stop(const char* reason);
  53. /**
  54. * Release an acquired wait object
  55. * @param wait_object WaitObject to release
  56. */
  57. void ReleaseWaitObject(WaitObject* wait_object);
  58. /// Resumes a thread from waiting by marking it as "ready"
  59. void ResumeFromWait();
  60. /**
  61. * Sets the result after the thread awakens (from either WaitSynchronization SVC)
  62. * @param result Value to set to the returned result
  63. */
  64. void SetWaitSynchronizationResult(ResultCode result);
  65. /**
  66. * Sets the output parameter value after the thread awakens (from WaitSynchronizationN SVC only)
  67. * @param output Value to set to the output parameter
  68. */
  69. void SetWaitSynchronizationOutput(s32 output);
  70. Core::ThreadContext context;
  71. u32 thread_id;
  72. u32 status;
  73. u32 entry_point;
  74. u32 stack_top;
  75. u32 stack_size;
  76. s32 initial_priority;
  77. s32 current_priority;
  78. s32 processor_id;
  79. std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
  80. VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
  81. bool wait_all; ///< True if the thread is waiting on all objects before resuming
  82. bool wait_set_output; ///< True if the output parameter should be set on thread wakeup
  83. std::string name;
  84. /// Whether this thread is intended to never actually be executed, i.e. always idle
  85. bool idle = false;
  86. private:
  87. Thread() = default;
  88. };
  89. /// Sets up the primary application thread
  90. SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size);
  91. /// Reschedules to the next available thread (call after current thread is suspended)
  92. void Reschedule();
  93. /// Arbitrate the highest priority thread that is waiting
  94. Thread* ArbitrateHighestPriorityThread(u32 address);
  95. /// Arbitrate all threads currently waiting...
  96. void ArbitrateAllThreads(u32 address);
  97. /// Gets the current thread
  98. Thread* GetCurrentThread();
  99. /// Waits the current thread on a sleep
  100. void WaitCurrentThread_Sleep();
  101. /**
  102. * Waits the current thread from a WaitSynchronization call
  103. * @param wait_object Kernel object that we are waiting on
  104. * @param wait_set_output If true, set the output parameter on thread wakeup (for WaitSynchronizationN only)
  105. * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
  106. */
  107. void WaitCurrentThread_WaitSynchronization(SharedPtr<WaitObject> wait_object, bool wait_set_output, bool wait_all);
  108. /**
  109. * Waits the current thread from an ArbitrateAddress call
  110. * @param wait_address Arbitration address used to resume from wait
  111. */
  112. void WaitCurrentThread_ArbitrateAddress(VAddr wait_address);
  113. /**
  114. * Schedules an event to wake up the specified thread after the specified delay.
  115. * @param handle The thread handle.
  116. * @param nanoseconds The time this thread will be allowed to sleep for.
  117. */
  118. void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds);
  119. /**
  120. * Sets up the idle thread, this is a thread that is intended to never execute instructions,
  121. * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
  122. * and will try to yield on every call.
  123. * @returns The handle of the idle thread
  124. */
  125. Handle SetupIdleThread();
  126. /// Initialize threading
  127. void ThreadingInit();
  128. /// Shutdown threading
  129. void ThreadingShutdown();
  130. } // namespace