semaphore.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 { return "Semaphore"; }
  22. std::string GetName() const override { return name; }
  23. static const HandleType HANDLE_TYPE = HandleType::Semaphore;
  24. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  25. s32 max_count; ///< Maximum number of simultaneous holders the semaphore can have
  26. s32 available_count; ///< Number of free slots left in the semaphore
  27. std::string name; ///< Name of semaphore (optional)
  28. bool ShouldWait() override;
  29. void Acquire() override;
  30. /**
  31. * Releases a certain number of slots from a semaphore.
  32. * @param release_count The number of slots to release
  33. * @return The number of free slots the semaphore had before this call
  34. */
  35. ResultVal<s32> Release(s32 release_count);
  36. private:
  37. Semaphore();
  38. ~Semaphore() override;
  39. };
  40. } // namespace