thread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "common/common_types.h"
  6. #include "core/mem_map.h"
  7. #include "core/hle/kernel/kernel.h"
  8. #include "core/hle/result.h"
  9. enum ThreadPriority {
  10. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  11. THREADPRIO_DEFAULT = 16, ///< Default thread priority for userland apps
  12. THREADPRIO_LOW = 31, ///< Low range of thread priority for userland apps
  13. THREADPRIO_LOWEST = 63, ///< Thread priority max checked by svcCreateThread
  14. };
  15. enum ThreadProcessorId {
  16. THREADPROCESSORID_0 = 0xFFFFFFFE, ///< Enables core appcode
  17. THREADPROCESSORID_1 = 0xFFFFFFFD, ///< Enables core syscore
  18. THREADPROCESSORID_ALL = 0xFFFFFFFC, ///< Enables both cores
  19. };
  20. enum ThreadStatus {
  21. THREADSTATUS_RUNNING = 1,
  22. THREADSTATUS_READY = 2,
  23. THREADSTATUS_WAIT = 4,
  24. THREADSTATUS_SUSPEND = 8,
  25. THREADSTATUS_DORMANT = 16,
  26. THREADSTATUS_DEAD = 32,
  27. THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
  28. };
  29. enum WaitType {
  30. WAITTYPE_NONE,
  31. WAITTYPE_SLEEP,
  32. WAITTYPE_SEMA,
  33. WAITTYPE_EVENT,
  34. WAITTYPE_THREADEND,
  35. WAITTYPE_MUTEX,
  36. WAITTYPE_SYNCH,
  37. WAITTYPE_ARB,
  38. };
  39. namespace Kernel {
  40. /// Creates a new thread - wrapper for external user
  41. Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id,
  42. u32 stack_top, int stack_size=Kernel::DEFAULT_STACK_SIZE);
  43. /// Sets up the primary application thread
  44. Handle SetupMainThread(s32 priority, int stack_size=Kernel::DEFAULT_STACK_SIZE);
  45. /// Reschedules to the next available thread (call after current thread is suspended)
  46. void Reschedule();
  47. /// Stops the current thread
  48. ResultCode StopThread(Handle thread, const char* reason);
  49. /**
  50. * Retrieves the ID of the specified thread handle
  51. * @param thread_id Will contain the output thread id
  52. * @param handle Handle to the thread we want
  53. * @return Whether the function was successful or not
  54. */
  55. ResultCode GetThreadId(u32* thread_id, Handle handle);
  56. /// Resumes a thread from waiting by marking it as "ready"
  57. void ResumeThreadFromWait(Handle handle);
  58. /// Arbitrate the highest priority thread that is waiting
  59. Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address);
  60. /// Arbitrate all threads currently waiting...
  61. void ArbitrateAllThreads(u32 arbiter, u32 address);
  62. /// Gets the current thread handle
  63. Handle GetCurrentThreadHandle();
  64. /**
  65. * Puts the current thread in the wait state for the given type
  66. * @param wait_type Type of wait
  67. * @param wait_handle Handle of Kernel object that we are waiting on, defaults to current thread
  68. */
  69. void WaitCurrentThread(WaitType wait_type, Handle wait_handle=GetCurrentThreadHandle());
  70. /**
  71. * Puts the current thread in the wait state for the given type
  72. * @param wait_type Type of wait
  73. * @param wait_handle Handle of Kernel object that we are waiting on, defaults to current thread
  74. * @param wait_address Arbitration address used to resume from wait
  75. */
  76. void WaitCurrentThread(WaitType wait_type, Handle wait_handle, VAddr wait_address);
  77. /// Put current thread in a wait state - on WaitSynchronization
  78. void WaitThread_Synchronization();
  79. /// Get the priority of the thread specified by handle
  80. ResultVal<u32> GetThreadPriority(const Handle handle);
  81. /// Set the priority of the thread specified by handle
  82. ResultCode SetThreadPriority(Handle handle, s32 priority);
  83. /**
  84. * Sets up the idle thread, this is a thread that is intended to never execute instructions,
  85. * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
  86. * and will try to yield on every call.
  87. * @returns The handle of the idle thread
  88. */
  89. Handle SetupIdleThread();
  90. /// Whether the current thread is an idle thread
  91. bool IsIdleThread(Handle thread);
  92. /// Initialize threading
  93. void ThreadingInit();
  94. /// Shutdown threading
  95. void ThreadingShutdown();
  96. } // namespace