semaphore.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. namespace Kernel {
  10. class Semaphore final : public WaitObject {
  11. public:
  12. /**
  13. * Creates a semaphore.
  14. * @param initial_count Number of slots reserved for other threads
  15. * @param max_count Maximum number of slots the semaphore can have
  16. * @param name Optional name of semaphore
  17. * @return The created semaphore
  18. */
  19. static ResultVal<SharedPtr<Semaphore>> Create(s32 initial_count, s32 max_count,
  20. std::string name = "Unknown");
  21. std::string GetTypeName() const override {
  22. return "Semaphore";
  23. }
  24. std::string GetName() const override {
  25. return name;
  26. }
  27. static const HandleType HANDLE_TYPE = HandleType::Semaphore;
  28. HandleType GetHandleType() const override {
  29. return HANDLE_TYPE;
  30. }
  31. s32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
  32. s32 available_count; ///< Number of free slots left in the semaphore
  33. std::string name; ///< Name of semaphore (optional)
  34. bool ShouldWait(Thread* thread) const override;
  35. void Acquire(Thread* thread) override;
  36. /**
  37. * Releases a certain number of slots from a semaphore.
  38. * @param release_count The number of slots to release
  39. * @return The number of free slots the semaphore had before this call
  40. */
  41. ResultVal<s32> Release(s32 release_count);
  42. private:
  43. Semaphore();
  44. ~Semaphore() override;
  45. };
  46. } // namespace