Atmosphere/libraries/libstratosphere/include/stratosphere/os/os_semaphore.hpp

72 lines
2.1 KiB
C++
Raw Normal View History

/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
2020-04-08 09:21:35 +00:00
#include <stratosphere/os/os_semaphore_types.hpp>
#include <stratosphere/os/os_semaphore_api.hpp>
namespace ams::os {
class Semaphore {
NON_COPYABLE(Semaphore);
NON_MOVEABLE(Semaphore);
private:
2021-10-10 07:14:06 +00:00
SemaphoreType m_sema;
public:
2020-04-08 09:21:35 +00:00
explicit Semaphore(s32 count, s32 max_count) {
2021-10-10 07:14:06 +00:00
InitializeSemaphore(std::addressof(m_sema), count, max_count);
2020-04-08 09:21:35 +00:00
}
2021-10-10 07:14:06 +00:00
~Semaphore() { FinalizeSemaphore(std::addressof(m_sema)); }
2020-04-08 09:21:35 +00:00
void Acquire() {
2021-10-10 07:14:06 +00:00
return os::AcquireSemaphore(std::addressof(m_sema));
2020-04-08 09:21:35 +00:00
}
bool TryAcquire() {
2021-10-10 07:14:06 +00:00
return os::TryAcquireSemaphore(std::addressof(m_sema));
2020-04-08 09:21:35 +00:00
}
2020-04-08 09:21:35 +00:00
bool TimedAcquire(TimeSpan timeout) {
2021-10-10 07:14:06 +00:00
return os::TimedAcquireSemaphore(std::addressof(m_sema), timeout);
2020-04-08 09:21:35 +00:00
}
void Release() {
2021-10-10 07:14:06 +00:00
return os::ReleaseSemaphore(std::addressof(m_sema));
2020-04-08 09:21:35 +00:00
}
void Release(s32 count) {
2021-10-10 07:14:06 +00:00
return os::ReleaseSemaphore(std::addressof(m_sema), count);
2020-04-08 09:21:35 +00:00
}
2020-04-08 09:21:35 +00:00
s32 GetCurrentCount() const {
2021-10-10 07:14:06 +00:00
return os::GetCurrentSemaphoreCount(std::addressof(m_sema));
2020-04-08 09:21:35 +00:00
}
operator SemaphoreType &() {
2021-10-10 07:14:06 +00:00
return m_sema;
2020-04-08 09:21:35 +00:00
}
operator const SemaphoreType &() const {
2021-10-10 07:14:06 +00:00
return m_sema;
2020-04-08 09:21:35 +00:00
}
2020-04-08 09:21:35 +00:00
SemaphoreType *GetBase() {
2021-10-10 07:14:06 +00:00
return std::addressof(m_sema);
}
};
}