thread.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. ResultVal<bool> Wait() override;
  48. ResultVal<bool> 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 output values after the thread awakens from WaitSynchronization
  62. * @param return_val Value returned
  63. * @param out_val Value to set to the output parameter
  64. */
  65. void SetReturnValue(ResultCode return_val, s32 out_val);
  66. Core::ThreadContext context;
  67. u32 thread_id;
  68. u32 status;
  69. u32 entry_point;
  70. u32 stack_top;
  71. u32 stack_size;
  72. s32 initial_priority;
  73. s32 current_priority;
  74. s32 processor_id;
  75. std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
  76. VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
  77. bool wait_all; ///< True if the thread is waiting on all objects before resuming
  78. std::string name;
  79. /// Whether this thread is intended to never actually be executed, i.e. always idle
  80. bool idle = false;
  81. private:
  82. Thread() = default;
  83. };
  84. /// Sets up the primary application thread
  85. SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size);
  86. /// Reschedules to the next available thread (call after current thread is suspended)
  87. void Reschedule();
  88. /// Arbitrate the highest priority thread that is waiting
  89. Thread* ArbitrateHighestPriorityThread(u32 address);
  90. /// Arbitrate all threads currently waiting...
  91. void ArbitrateAllThreads(u32 address);
  92. /// Gets the current thread
  93. Thread* GetCurrentThread();
  94. /// Waits the current thread on a sleep
  95. void WaitCurrentThread_Sleep();
  96. /**
  97. * Waits the current thread from a WaitSynchronization call
  98. * @param wait_object Kernel object that we are waiting on
  99. * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
  100. */
  101. void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all=false);
  102. /**
  103. * Waits the current thread from an ArbitrateAddress call
  104. * @param wait_address Arbitration address used to resume from wait
  105. */
  106. void WaitCurrentThread_ArbitrateAddress(VAddr wait_address);
  107. /**
  108. * Schedules an event to wake up the specified thread after the specified delay.
  109. * @param handle The thread handle.
  110. * @param nanoseconds The time this thread will be allowed to sleep for.
  111. */
  112. void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds);
  113. /**
  114. * Sets up the idle thread, this is a thread that is intended to never execute instructions,
  115. * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
  116. * and will try to yield on every call.
  117. * @returns The handle of the idle thread
  118. */
  119. Handle SetupIdleThread();
  120. /// Initialize threading
  121. void ThreadingInit();
  122. /// Shutdown threading
  123. void ThreadingShutdown();
  124. } // namespace