1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-22 10:22:08 +00:00
Atmosphere/libraries/libstratosphere/source/os/os_event.cpp

172 lines
5.6 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>
2020-04-08 10:21:35 +01:00
#include "impl/os_timeout_helper.hpp"
#include "impl/os_waitable_object_list.hpp"
2020-04-08 10:21:35 +01:00
#include "impl/os_waitable_holder_impl.hpp"
namespace ams::os {
2020-04-08 10:21:35 +01:00
namespace {
ALWAYS_INLINE u64 GetBroadcastCounterUnsafe(EventType *event) {
const u64 upper = event->broadcast_counter_high;
return (upper << BITSIZEOF(event->broadcast_counter_low)) | event->broadcast_counter_low;
}
ALWAYS_INLINE void IncrementBroadcastCounterUnsafe(EventType *event) {
if ((++event->broadcast_counter_low) == 0) {
++event->broadcast_counter_high;
}
}
}
2020-04-08 10:21:35 +01:00
void InitializeEvent(EventType *event, bool signaled, EventClearMode clear_mode) {
/* Initialize internal variables. */
util::ConstructAt(event->cs_event);
util::ConstructAt(event->cv_signaled);
2020-04-08 10:21:35 +01:00
/* Initialize the waitable object list. */
util::ConstructAt(event->waitable_object_list_storage);
2020-04-08 10:21:35 +01:00
/* Initialize member variables. */
event->signaled = signaled;
event->initially_signaled = signaled;
event->clear_mode = static_cast<u8>(clear_mode);
event->broadcast_counter_low = 0;
event->broadcast_counter_high = 0;
/* Mark initialized. */
event->state = EventType::State_Initialized;
}
2020-04-08 10:21:35 +01:00
void FinalizeEvent(EventType *event) {
AMS_ASSERT(event->state == EventType::State_Initialized);
/* Mark uninitialized. */
event->state = EventType::State_NotInitialized;
/* Destroy objects. */
util::DestroyAt(event->waitable_object_list_storage);
util::DestroyAt(event->cv_signaled);
util::DestroyAt(event->cs_event);
2020-04-08 10:21:35 +01:00
}
void SignalEvent(EventType *event) {
AMS_ASSERT(event->state == EventType::State_Initialized);
std::scoped_lock lk(GetReference(event->cs_event));
/* If we're already signaled, nothing more to do. */
2020-04-08 10:21:35 +01:00
if (event->signaled) {
return;
}
2020-04-08 10:21:35 +01:00
event->signaled = true;
/* Signal! */
2020-04-08 10:21:35 +01:00
if (event->clear_mode == EventClearMode_ManualClear) {
/* If we're manual clear, increment counter and wake all. */
2020-04-08 10:21:35 +01:00
IncrementBroadcastCounterUnsafe(event);
GetReference(event->cv_signaled).Broadcast();
} else {
/* If we're auto clear, signal one thread, which will clear. */
GetReference(event->cv_signaled).Signal();
}
/* Wake up whatever manager, if any. */
2020-04-08 10:21:35 +01:00
GetReference(event->waitable_object_list_storage).SignalAllThreads();
}
2020-04-08 10:21:35 +01:00
void WaitEvent(EventType *event) {
AMS_ASSERT(event->state == EventType::State_Initialized);
2020-04-08 10:21:35 +01:00
std::scoped_lock lk(GetReference(event->cs_event));
2020-04-08 10:21:35 +01:00
const auto cur_counter = GetBroadcastCounterUnsafe(event);
while (!event->signaled) {
if (cur_counter != GetBroadcastCounterUnsafe(event)) {
break;
}
2020-04-08 10:21:35 +01:00
GetReference(event->cv_signaled).Wait(GetPointer(event->cs_event));
}
2020-04-08 10:21:35 +01:00
if (event->clear_mode == EventClearMode_AutoClear) {
event->signaled = false;
}
}
2020-04-08 10:21:35 +01:00
bool TryWaitEvent(EventType *event) {
AMS_ASSERT(event->state == EventType::State_Initialized);
std::scoped_lock lk(GetReference(event->cs_event));
2020-04-08 10:21:35 +01:00
const bool signaled = event->signaled;
if (event->clear_mode == EventClearMode_AutoClear) {
event->signaled = false;
}
2020-04-08 10:21:35 +01:00
return signaled;
}
2020-04-08 10:21:35 +01:00
bool TimedWaitEvent(EventType *event, TimeSpan timeout) {
AMS_ASSERT(event->state == EventType::State_Initialized);
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
2020-04-08 10:21:35 +01:00
{
impl::TimeoutHelper timeout_helper(timeout);
std::scoped_lock lk(GetReference(event->cs_event));
const auto cur_counter = GetBroadcastCounterUnsafe(event);
while (!event->signaled) {
if (cur_counter != GetBroadcastCounterUnsafe(event)) {
break;
}
auto wait_res = GetReference(event->cv_signaled).TimedWait(GetPointer(event->cs_event), timeout_helper);
if (wait_res == ConditionVariableStatus::TimedOut) {
return false;
}
}
2020-04-08 10:21:35 +01:00
if (event->clear_mode == EventClearMode_AutoClear) {
event->signaled = false;
}
}
return true;
}
2020-04-08 10:21:35 +01:00
void ClearEvent(EventType *event) {
AMS_ASSERT(event->state == EventType::State_Initialized);
std::scoped_lock lk(GetReference(event->cs_event));
/* Clear the signaled state. */
event->signaled = false;
}
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, EventType *event) {
AMS_ASSERT(event->state == EventType::State_Initialized);
util::ConstructAt(GetReference(waitable_holder->impl_storage).holder_of_event_storage, event);
2020-04-08 10:21:35 +01:00
waitable_holder->user_data = 0;
}
}