mutex.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2014 Citra Emulator 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 "common/common_types.h"
  7. #include "common/swap.h"
  8. #include "core/hle/kernel/kernel.h"
  9. #include "core/hle/kernel/wait_object.h"
  10. #include "core/hle/result.h"
  11. namespace Kernel {
  12. class Thread;
  13. class Mutex final : public WaitObject {
  14. public:
  15. /**
  16. * Creates a mutex.
  17. * @param holding_thread Specifies a thread already holding the mutex. If not nullptr, this
  18. * thread will acquire the mutex.
  19. * @param guest_addr Address of the object tracking the mutex in guest memory. If specified,
  20. * this mutex will update the guest object when its state changes.
  21. * @param name Optional name of mutex
  22. * @return Pointer to new Mutex object
  23. */
  24. static SharedPtr<Mutex> Create(SharedPtr<Kernel::Thread> holding_thread, VAddr guest_addr = 0,
  25. std::string name = "Unknown");
  26. std::string GetTypeName() const override {
  27. return "Mutex";
  28. }
  29. std::string GetName() const override {
  30. return name;
  31. }
  32. static const HandleType HANDLE_TYPE = HandleType::Mutex;
  33. HandleType GetHandleType() const override {
  34. return HANDLE_TYPE;
  35. }
  36. u32 priority; ///< The priority of the mutex, used for priority inheritance.
  37. std::string name; ///< Name of mutex (optional)
  38. VAddr guest_addr; ///< Address of the guest mutex value
  39. /**
  40. * Elevate the mutex priority to the best priority
  41. * among the priorities of all its waiting threads.
  42. */
  43. void UpdatePriority();
  44. bool ShouldWait(Thread* thread) const override;
  45. void Acquire(Thread* thread) override;
  46. void AddWaitingThread(SharedPtr<Thread> thread) override;
  47. void RemoveWaitingThread(Thread* thread) override;
  48. /**
  49. * Attempts to release the mutex from the specified thread.
  50. * @param thread Thread that wants to release the mutex.
  51. * @returns The result code of the operation.
  52. */
  53. ResultCode Release(Thread* thread);
  54. /// Gets the handle to the holding process stored in the guest state.
  55. Handle GetOwnerHandle() const;
  56. /// Gets the Thread pointed to by the owner handle
  57. SharedPtr<Thread> GetHoldingThread() const;
  58. /// Sets the holding process handle in the guest state.
  59. void SetHoldingThread(SharedPtr<Thread> thread);
  60. /// Returns the has_waiters bit in the guest state.
  61. bool GetHasWaiters() const;
  62. /// Sets the has_waiters bit in the guest state.
  63. void SetHasWaiters(bool has_waiters);
  64. private:
  65. Mutex();
  66. ~Mutex() override;
  67. /// Object in guest memory used to track the mutex state
  68. union GuestState {
  69. u32_le raw;
  70. /// Handle of the thread that currently holds the mutex, 0 if available
  71. BitField<0, 30, u32_le> holding_thread_handle;
  72. /// 1 when there are threads waiting for this mutex, otherwise 0
  73. BitField<30, 1, u32_le> has_waiters;
  74. };
  75. static_assert(sizeof(GuestState) == 4, "GuestState size is incorrect");
  76. };
  77. /**
  78. * Releases all the mutexes held by the specified thread
  79. * @param thread Thread that is holding the mutexes
  80. */
  81. void ReleaseThreadMutexes(Thread* thread);
  82. } // namespace Kernel