2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2021-10-01 02:00:47 +00:00
|
|
|
#include "impl/os_multiple_wait_holder_impl.hpp"
|
2020-04-08 09:21:35 +00:00
|
|
|
#include "impl/os_inter_process_event.hpp"
|
|
|
|
#include "impl/os_timeout_helper.hpp"
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::os {
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
Result CreateSystemEvent(SystemEventType *event, EventClearMode clear_mode, bool inter_process) {
|
2019-12-09 11:57:37 +00:00
|
|
|
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;
|
2019-12-09 11:57:37 +00:00
|
|
|
} else {
|
2020-04-08 09:21:35 +00:00
|
|
|
InitializeEvent(std::addressof(event->event), false, clear_mode);
|
|
|
|
event->state = SystemEventType::State_InitializedAsEvent;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
void DestroySystemEvent(SystemEventType *event) {
|
|
|
|
auto state = event->state;
|
|
|
|
event->state = SystemEventType::State_NotInitialized;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
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();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
void AttachSystemEvent(SystemEventType *event, NativeHandle read_handle, bool read_handle_managed, NativeHandle write_handle, bool write_handle_managed, EventClearMode clear_mode) {
|
2022-03-06 20:08:20 +00:00
|
|
|
AMS_ASSERT(read_handle != os::InvalidNativeHandle || write_handle != os::InvalidNativeHandle);
|
2020-04-08 09:21:35 +00:00
|
|
|
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;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
void AttachReadableHandleToSystemEvent(SystemEventType *event, NativeHandle read_handle, bool manage_read_handle, EventClearMode clear_mode) {
|
2022-03-06 20:08:20 +00:00
|
|
|
return AttachSystemEvent(event, read_handle, manage_read_handle, os::InvalidNativeHandle, false, clear_mode);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
void AttachWritableHandleToSystemEvent(SystemEventType *event, NativeHandle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
|
2022-03-06 20:08:20 +00:00
|
|
|
return AttachSystemEvent(event, os::InvalidNativeHandle, false, write_handle, manage_write_handle, clear_mode);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
NativeHandle DetachReadableHandleOfSystemEvent(SystemEventType *event) {
|
2020-04-08 09:21:35 +00:00
|
|
|
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
|
|
|
|
return impl::DetachReadableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
NativeHandle DetachWritableHandleOfSystemEvent(SystemEventType *event) {
|
2020-04-08 09:21:35 +00:00
|
|
|
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
|
|
|
|
return impl::DetachWritableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
NativeHandle GetReadableHandleOfSystemEvent(const SystemEventType *event) {
|
2020-04-08 09:21:35 +00:00
|
|
|
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
|
|
|
|
return impl::GetReadableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
NativeHandle GetWritableHandleOfSystemEvent(const SystemEventType *event) {
|
2020-04-08 09:21:35 +00:00
|
|
|
AMS_ASSERT(event->state == SystemEventType::State_InitializedAsInterProcessEvent);
|
|
|
|
return impl::GetWritableHandleOfInterProcessEvent(std::addressof(event->inter_process_event));
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
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));
|
2019-12-09 11:57:37 +00:00
|
|
|
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));
|
2019-12-09 11:57:37 +00:00
|
|
|
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));
|
2019-12-09 11:57:37 +00:00
|
|
|
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);
|
2019-12-09 11:57:37 +00:00
|
|
|
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));
|
2019-12-09 11:57:37 +00:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void InitializeMultiWaitHolder(MultiWaitHolderType *multi_wait_holder, SystemEventType *event) {
|
2020-04-08 09:21:35 +00:00
|
|
|
switch (event->state) {
|
|
|
|
case SystemEventType::State_InitializedAsInterProcessEvent:
|
2021-10-01 02:00:47 +00:00
|
|
|
util::ConstructAt(GetReference(multi_wait_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:
|
2021-10-01 02:00:47 +00:00
|
|
|
util::ConstructAt(GetReference(multi_wait_holder->impl_storage).holder_of_event_storage, std::addressof(event->event));
|
2020-04-08 09:21:35 +00:00
|
|
|
break;
|
2019-12-09 11:57:37 +00:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|