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

152 lines
5.8 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/>.
*/
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_waitable_object_list.hpp"
namespace ams::os::impl {
namespace {
2020-04-08 09:21:35 +00:00
inline void SetupInterProcessEventType(InterProcessEventType *event, Handle read_handle, bool read_handle_managed, Handle write_handle, bool write_handle_managed, EventClearMode clear_mode) {
/* 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. */
new (GetPointer(event->waitable_object_list_storage)) impl::WaitableObjectList;
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) {
Handle 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);
return ResultSuccess();
}
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 != svc::InvalidHandle) {
impl::InterProcessEventImpl::Close(event->readable_handle);
}
event->is_readable_handle_managed = false;
}
if (event->is_writable_handle_managed) {
if (event->writable_handle != svc::InvalidHandle) {
impl::InterProcessEventImpl::Close(event->writable_handle);
}
event->is_writable_handle_managed = false;
}
/* Destroy the waitlist. */
GetReference(event->waitable_object_list_storage).~WaitableObjectList();
}
2020-04-08 09:21:35 +00:00
void AttachInterProcessEvent(InterProcessEventType *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);
return SetupInterProcessEventType(event, read_handle, read_handle_managed, write_handle, write_handle_managed, clear_mode);
}
2020-04-08 09:21:35 +00:00
Handle DetachReadableHandleOfInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
const Handle handle = event->readable_handle;
event->readable_handle = svc::InvalidHandle;
event->is_readable_handle_managed = false;
return handle;
}
2020-04-08 09:21:35 +00:00
Handle DetachWritableHandleOfInterProcessEvent(InterProcessEventType *event) {
AMS_ASSERT(event->state == InterProcessEventType::State_Initialized);
const Handle handle = event->writable_handle;
2020-04-08 09:21:35 +00:00
event->writable_handle = svc::InvalidHandle;
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 == svc::InvalidHandle) {
handle = event->writable_handle;
}
2020-04-08 09:21:35 +00:00
return impl::InterProcessEventImpl::Clear(handle);
}
2020-04-08 09:21:35 +00:00
Handle GetReadableHandleOfInterProcessEvent(const InterProcessEventType *event) {
AMS_ASSERT(event->state != InterProcessEventType::State_NotInitialized);
return event->readable_handle;
}
2020-04-08 09:21:35 +00:00
Handle GetWritableHandleOfInterProcessEvent(const InterProcessEventType *event) {
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
}