semaphore.h 1.7 KB

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