semaphore.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <queue>
  6. #include <string>
  7. #include "common/common_types.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 Semaphore final : public WaitObject {
  13. public:
  14. /**
  15. * Creates a semaphore.
  16. * @param guest_addr Address of the object tracking the semaphore in guest memory. If specified,
  17. * this semaphore will update the guest object when its state changes.
  18. * @param mutex_addr Optional address of a guest mutex associated with this semaphore, used by
  19. * the OS for implementing events.
  20. * @param name Optional name of semaphore.
  21. * @return The created semaphore.
  22. */
  23. static ResultVal<SharedPtr<Semaphore>> Create(VAddr guest_addr, VAddr mutex_addr = 0,
  24. std::string name = "Unknown");
  25. std::string GetTypeName() const override {
  26. return "Semaphore";
  27. }
  28. std::string GetName() const override {
  29. return name;
  30. }
  31. static const HandleType HANDLE_TYPE = HandleType::Semaphore;
  32. HandleType GetHandleType() const override {
  33. return HANDLE_TYPE;
  34. }
  35. s32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
  36. s32 available_count; ///< Number of free slots left in the semaphore
  37. std::string name; ///< Name of semaphore (optional)
  38. VAddr guest_addr; ///< Address of the guest semaphore value
  39. VAddr mutex_addr; ///< (optional) Address of guest mutex value associated with this semaphore,
  40. ///< used for implementing events
  41. bool ShouldWait(Thread* thread) const override;
  42. void Acquire(Thread* thread) override;
  43. /**
  44. * Releases a certain number of slots from a semaphore.
  45. * @param release_count The number of slots to release
  46. * @return The number of free slots the semaphore had before this call
  47. */
  48. ResultVal<s32> Release(s32 release_count);
  49. private:
  50. Semaphore();
  51. ~Semaphore() override;
  52. /// Updates the state of the object tracking this semaphore in guest memory
  53. void UpdateGuestState();
  54. };
  55. } // namespace Kernel