semaphore.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // TODO(Subv): This is actually a Condition Variable.
  13. class Semaphore final : public WaitObject {
  14. public:
  15. /**
  16. * Creates a semaphore.
  17. * @param guest_addr Address of the object tracking the semaphore in guest memory. If specified,
  18. * this semaphore will update the guest object when its state changes.
  19. * @param mutex_addr Optional address of a guest mutex associated with this semaphore, used by
  20. * the OS for implementing events.
  21. * @param name Optional name of semaphore.
  22. * @return The created semaphore.
  23. */
  24. static ResultVal<SharedPtr<Semaphore>> Create(VAddr guest_addr, VAddr mutex_addr = 0,
  25. std::string name = "Unknown");
  26. std::string GetTypeName() const override {
  27. return "Semaphore";
  28. }
  29. std::string GetName() const override {
  30. return name;
  31. }
  32. static const HandleType HANDLE_TYPE = HandleType::Semaphore;
  33. HandleType GetHandleType() const override {
  34. return HANDLE_TYPE;
  35. }
  36. s32 GetAvailableCount() const;
  37. void SetAvailableCount(s32 value) const;
  38. std::string name; ///< Name of semaphore (optional)
  39. VAddr guest_addr; ///< Address of the guest semaphore value
  40. VAddr mutex_addr; ///< (optional) Address of guest mutex value associated with this semaphore,
  41. ///< used for implementing events
  42. bool ShouldWait(Thread* thread) const override;
  43. void Acquire(Thread* thread) override;
  44. /**
  45. * Releases a slot from a semaphore.
  46. * @param target The number of threads to wakeup, -1 is all.
  47. * @return ResultCode indicating if the operation succeeded.
  48. */
  49. ResultCode Release(s32 target);
  50. private:
  51. Semaphore();
  52. ~Semaphore() override;
  53. };
  54. } // namespace Kernel