Atmosphere/libraries/libstratosphere/source/os/os_system_event.cpp

135 lines
6.7 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2018-2020 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/>.
*/
#include <stratosphere.hpp>
#include "impl/os_waitable_holder_impl.hpp"
2020-04-08 09:21:35 +00:00
#include "impl/os_inter_process_event.hpp"
#include "impl/os_timeout_helper.hpp"
namespace ams::os {
2020-04-08 09:21:35 +00:00
Result CreateSystemEvent(SystemEventType *event, EventClearMode clear_mode, bool inter_process) {
if (inter_process) {
2020-04-08 09:21:35 +00:00
R_TRY(impl::CreateInterProcessEvent(std::addressof(event->inter_process_event), clear_mode));
event->state = SystemEventType::State_InitializedAsInterProcessEvent;
} else {
2020-04-08 09:21:35 +00:00
InitializeEvent(std::addressof(event->event), false, clear_mode);
event->state = SystemEventType::State_InitializedAsEvent;
}
return ResultSuccess();
}
2020-04-08 09:21:35 +00:00
void DestroySystemEvent(SystemEventType *event) {
auto state = event->state;
event->state = SystemEventType::State_NotInitialized;
2020-04-08 09:21:35 +00:00
switch (state) {
case SystemEventType::State_InitializedAsInterProcessEvent: impl::DestroyInterProcessEvent(std::addressof(event->inter_process_event)); break;
case SystemEventType::State_InitializedAsEvent: FinalizeEvent(std::addressof(event->event)); break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
void AttachSystemEvent(SystemEventType *event, Handle read_handle, bool read_handle_managed, Handle write_handle, bool write_handle_managed, EventClearMode clear_mode) {
AMS_ASSERT(read_handle != svc::InvalidHandle || write_handle != svc::InvalidHandle);
impl::AttachInterProcessEvent(std::addressof(event->inter_process_event), read_handle, read_handle_managed, write_handle, write_handle_managed, clear_mode);
event->state = SystemEventType::State_InitializedAsInterProcessEvent;
}
2020-04-08 09:21:35 +00:00
void AttachReadableHandleToSystemEvent(SystemEventType *event, Handle read_handle, bool manage_read_handle, EventClearMode clear_mode) {
return AttachSystemEvent(event, read_handle, manage_read_handle, svc::InvalidHandle, false, clear_mode);
}
2020-04-08 09:21:35 +00:00
void AttachWritableHandleToSystemEvent(SystemEventType *event, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
return AttachSystemEvent(event, svc::InvalidHandle, false, write_handle, manage_write_handle, clear_mode);
}
2020-04-08 09:21:35 +00:00
Handle DetachReadableHandleOfSystemEvent(SystemEventType *event) {
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
return impl::DetachReadableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
}
2020-04-08 09:21:35 +00:00
Handle DetachWritableHandleOfSystemEvent(SystemEventType *event) {
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
return impl::DetachWritableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
}
2020-04-08 09:21:35 +00:00
Handle GetReadableHandleOfSystemEvent(const SystemEventType *event) {
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
return impl::GetReadableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
}
2020-04-08 09:21:35 +00:00
Handle GetWritableHandleOfSystemEvent(const SystemEventType *event) {
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
return impl::GetWritableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
}
2020-04-08 09:21:35 +00:00
void SignalSystemEvent(SystemEventType *event) {
switch (event->state) {
case SystemEventType::State_InitializedAsInterProcessEvent: return impl::SignalInterProcessEvent(std::addressof(event->inter_process_event));
case SystemEventType::State_InitializedAsEvent: return SignalEvent(std::addressof(event->event));
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
void WaitSystemEvent(SystemEventType *event) {
switch (event->state) {
case SystemEventType::State_InitializedAsInterProcessEvent: return impl::WaitInterProcessEvent(std::addressof(event->inter_process_event));
case SystemEventType::State_InitializedAsEvent: return WaitEvent(std::addressof(event->event));
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
bool TryWaitSystemEvent(SystemEventType *event) {
switch (event->state) {
case SystemEventType::State_InitializedAsInterProcessEvent: return impl::TryWaitInterProcessEvent(std::addressof(event->inter_process_event));
case SystemEventType::State_InitializedAsEvent: return TryWaitEvent(std::addressof(event->event));
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
bool TimedWaitSystemEvent(SystemEventType *event, TimeSpan timeout) {
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
switch (event->state) {
case SystemEventType::State_InitializedAsInterProcessEvent: return impl::TimedWaitInterProcessEvent(std::addressof(event->inter_process_event), timeout);
case SystemEventType::State_InitializedAsEvent: return TimedWaitEvent(std::addressof(event->event), timeout);
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
void ClearSystemEvent(SystemEventType *event) {
switch (event->state) {
case SystemEventType::State_InitializedAsInterProcessEvent: return impl::ClearInterProcessEvent(std::addressof(event->inter_process_event));
case SystemEventType::State_InitializedAsEvent: return ClearEvent(std::addressof(event->event));
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, SystemEventType *event) {
switch (event->state) {
case SystemEventType::State_InitializedAsInterProcessEvent:
util::ConstructAt(GetReference(waitable_holder->impl_storage).holder_of_inter_process_event_storage, std::addressof(event->inter_process_event));
2020-04-08 09:21:35 +00:00
break;
case SystemEventType::State_InitializedAsEvent:
util::ConstructAt(GetReference(waitable_holder->impl_storage).holder_of_event_storage, std::addressof(event->event));
2020-04-08 09:21:35 +00:00
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
2020-04-08 09:21:35 +00:00
}