Atmosphere/libraries/libstratosphere/source/os/impl/os_inter_process_event.cpp

153 lines
5.9 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/>.
*/
2020-04-08 09:21:35 +00:00
#include <stratosphere.hpp>
#include "os_inter_process_event.hpp"
2020-04-08 09:21:35 +00:00
#include "os_inter_process_event_impl.hpp"
#include "os_multiple_wait_object_list.hpp"
namespace ams::os::impl {
namespace {
inline void SetupInterProcessEventType(InterProcessEventType *event, NativeHandle read_handle, bool read_handle_managed, NativeHandle write_handle, bool write_handle_managed, EventClearMode clear_mode) {
2020-04-08 09:21:35 +00:00
/* Set handles. */
event->readable_handle = read_handle;
event->is_readable_handle_managed = read_handle_managed;
event->writable_handle = write_handle;
event->is_writable_handle_managed = write_handle_managed;
2020-04-08 09:21:35 +00:00
/* Set auto clear. */
event->auto_clear = (clear_mode == EventClearMode_AutoClear);
2020-04-08 09:21:35 +00:00
/* Create the waitlist node. */
util::ConstructAt(event->multi_wait_object_list_storage);
2020-04-08 09:21:35 +00:00
/* Set state. */
event->state = InterProcessEventType::State_Initialized;
}
}
2020-04-08 09:21:35 +00:00
Result CreateInterProcessEvent(InterProcessEventType *event, EventClearMode clear_mode) {
NativeHandle rh, wh;
2020-04-08 09:21:35 +00:00
R_TRY(impl::InterProcessEventImpl::Create(std::addressof(wh), std::addressof(rh)));
SetupInterProcessEventType(event, rh, true, wh, true, clear_mode);
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
2020-04-08 09:21:35 +00:00
void DestroyInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
/* Clear the state. */
event->state = InterProcessEventType::State_NotInitialized;
/* Close handles if required. */
if (event->is_readable_handle_managed) {
if (event->readable_handle != os::InvalidNativeHandle) {
2020-04-08 09:21:35 +00:00
impl::InterProcessEventImpl::Close(event->readable_handle);
}
event->is_readable_handle_managed = false;
}
if (event->is_writable_handle_managed) {
if (event->writable_handle != os::InvalidNativeHandle) {
2020-04-08 09:21:35 +00:00
impl::InterProcessEventImpl::Close(event->writable_handle);
}
event->is_writable_handle_managed = false;
}
/* Destroy the waitlist. */
util::DestroyAt(event->multi_wait_object_list_storage);
}
void AttachInterProcessEvent(InterProcessEventType *event, NativeHandle read_handle, bool read_handle_managed, NativeHandle write_handle, bool write_handle_managed, EventClearMode clear_mode) {
AMS_ASSERT(read_handle != os::InvalidNativeHandle || write_handle != os::InvalidNativeHandle);
2020-04-08 09:21:35 +00:00
return SetupInterProcessEventType(event, read_handle, read_handle_managed, write_handle, write_handle_managed, clear_mode);
}
NativeHandle DetachReadableHandleOfInterProcessEvent(InterProcessEventType *event) {
2020-04-08 09:21:35 +00:00
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
const NativeHandle handle = event->readable_handle;
2020-04-08 09:21:35 +00:00
event->readable_handle = os::InvalidNativeHandle;
2020-04-08 09:21:35 +00:00
event->is_readable_handle_managed = false;
return handle;
}
NativeHandle DetachWritableHandleOfInterProcessEvent(InterProcessEventType *event) {
2020-04-08 09:21:35 +00:00
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
const NativeHandle handle = event->writable_handle;
event->writable_handle = os::InvalidNativeHandle;
2020-04-08 09:21:35 +00:00
event->is_writable_handle_managed = false;
return handle;
}
2020-04-08 09:21:35 +00:00
void WaitInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
return impl::InterProcessEventImpl::Wait(event->readable_handle, event->auto_clear);
}
2020-04-08 09:21:35 +00:00
bool TryWaitInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
return impl::InterProcessEventImpl::TryWait(event->readable_handle, event->auto_clear);
}
2020-04-08 09:21:35 +00:00
bool TimedWaitInterProcessEvent(InterProcessEventType *event, TimeSpan timeout) {
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
return impl::InterProcessEventImpl::TimedWait(event->readable_handle, event->auto_clear, timeout);
}
2020-04-08 09:21:35 +00:00
void SignalInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state != InterProcessEventType::State_NotInitialized);
return impl::InterProcessEventImpl::Signal(event->writable_handle);
}
2020-04-08 09:21:35 +00:00
void ClearInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state != InterProcessEventType::State_NotInitialized);
auto handle = event->readable_handle;
if (handle == os::InvalidNativeHandle) {
2020-04-08 09:21:35 +00:00
handle = event->writable_handle;
}
2020-04-08 09:21:35 +00:00
return impl::InterProcessEventImpl::Clear(handle);
}
NativeHandle GetReadableHandleOfInterProcessEvent(const InterProcessEventType *event) {
2020-04-08 09:21:35 +00:00
AMS_ASSERT(event->state != InterProcessEventType::State_NotInitialized);
return event->readable_handle;
}
NativeHandle GetWritableHandleOfInterProcessEvent(const InterProcessEventType *event) {
2020-04-08 09:21:35 +00:00
AMS_ASSERT(event->state != InterProcessEventType::State_NotInitialized);
2020-04-08 09:21:35 +00:00
return event->writable_handle;
}
2020-04-08 09:21:35 +00:00
}