2019-12-09 12:57:37 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 12:57:37 +01: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/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2020-04-08 11:21:35 +02:00
|
|
|
#include "impl/os_timeout_helper.hpp"
|
2021-10-01 04:00:47 +02:00
|
|
|
#include "impl/os_multiple_wait_object_list.hpp"
|
|
|
|
#include "impl/os_multiple_wait_holder_impl.hpp"
|
2021-09-29 21:24:18 +02:00
|
|
|
#include "impl/os_message_queue_helper.hpp"
|
2019-12-09 12:57:37 +01:00
|
|
|
|
|
|
|
namespace ams::os {
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
namespace {
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
using MessageQueueHelper = impl::MessageQueueHelper<MessageQueueType>;
|
2019-12-09 12:57:37 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void InitializeMessageQueue(MessageQueueType *mq, uintptr_t *buffer, size_t count) {
|
|
|
|
AMS_ASSERT(buffer != nullptr);
|
|
|
|
AMS_ASSERT(count >= 1);
|
|
|
|
|
|
|
|
/* Setup objects. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::ConstructAt(mq->cs_queue);
|
|
|
|
util::ConstructAt(mq->cv_not_full);
|
|
|
|
util::ConstructAt(mq->cv_not_empty);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Setup wait lists. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::ConstructAt(mq->waitlist_not_empty);
|
|
|
|
util::ConstructAt(mq->waitlist_not_full);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Set member variables. */
|
|
|
|
mq->buffer = buffer;
|
|
|
|
mq->capacity = static_cast<s32>(count);
|
|
|
|
mq->count = 0;
|
|
|
|
mq->offset = 0;
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Mark initialized. */
|
|
|
|
mq->state = MessageQueueType::State_Initialized;
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
void FinalizeMessageQueue(MessageQueueType *mq) {
|
2022-03-06 21:08:20 +01:00
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
AMS_ASSERT(GetReference(mq->waitlist_not_empty).IsEmpty());
|
|
|
|
AMS_ASSERT(GetReference(mq->waitlist_not_full).IsEmpty());
|
|
|
|
|
|
|
|
/* Mark uninitialized. */
|
|
|
|
mq->state = MessageQueueType::State_NotInitialized;
|
|
|
|
|
|
|
|
/* Destroy wait lists. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::DestroyAt(mq->waitlist_not_empty);
|
|
|
|
util::DestroyAt(mq->waitlist_not_full);
|
2020-04-08 11:21:35 +02:00
|
|
|
|
|
|
|
/* Destroy objects. */
|
2021-03-22 04:30:40 +01:00
|
|
|
util::DestroyAt(mq->cv_not_empty);
|
|
|
|
util::DestroyAt(mq->cv_not_full);
|
|
|
|
util::DestroyAt(mq->cs_queue);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Sending (FIFO functionality) */
|
|
|
|
void SendMessageQueue(MessageQueueType *mq, uintptr_t data) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait sendable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueFull(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_full).Wait(GetPointer(mq->cs_queue));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
MessageQueueHelper::EnqueueUnsafe(mq, data);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_empty).SignalAllThreads();
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TrySendMessageQueue(MessageQueueType *mq, uintptr_t data) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Acquire mutex, check sendable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
if (MessageQueueHelper::IsMessageQueueFull(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
MessageQueueHelper::EnqueueUnsafe(mq, data);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_empty).SignalAllThreads();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TimedSendMessageQueue(MessageQueueType *mq, uintptr_t data, TimeSpan timeout) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait sendable. */
|
|
|
|
impl::TimeoutHelper timeout_helper(timeout);
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueFull(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
if (timeout_helper.TimedOut()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GetReference(mq->cv_not_full).TimedWait(GetPointer(mq->cs_queue), timeout_helper);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Send, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
MessageQueueHelper::EnqueueUnsafe(mq, data);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_empty).SignalAllThreads();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
/* Jamming (LIFO functionality) */
|
|
|
|
void JamMessageQueue(MessageQueueType *mq, uintptr_t data) {
|
2020-04-08 11:21:35 +02:00
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait sendable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueFull(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_full).Wait(GetPointer(mq->cs_queue));
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Send, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
MessageQueueHelper::JamUnsafe(mq, data);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_empty).SignalAllThreads();
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
bool TryJamMessageQueue(MessageQueueType *mq, uintptr_t data) {
|
2020-04-08 11:21:35 +02:00
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Acquire mutex, check sendable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
if (MessageQueueHelper::IsMessageQueueFull(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
MessageQueueHelper::JamUnsafe(mq, data);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_empty).SignalAllThreads();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
bool TimedJamMessageQueue(MessageQueueType *mq, uintptr_t data, TimeSpan timeout) {
|
2020-04-08 11:21:35 +02:00
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait sendable. */
|
|
|
|
impl::TimeoutHelper timeout_helper(timeout);
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueFull(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
if (timeout_helper.TimedOut()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GetReference(mq->cv_not_full).TimedWait(GetPointer(mq->cs_queue), timeout_helper);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Send, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
MessageQueueHelper::JamUnsafe(mq, data);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_empty).SignalAllThreads();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Receive functionality */
|
|
|
|
void ReceiveMessageQueue(uintptr_t *out, MessageQueueType *mq) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait receivable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueEmpty(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Wait(GetPointer(mq->cs_queue));
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Receive, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
*out = MessageQueueHelper::DequeueUnsafe(mq);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_full).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_full).SignalAllThreads();
|
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TryReceiveMessageQueue(uintptr_t *out, MessageQueueType *mq) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Acquire mutex, check receivable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
if (MessageQueueHelper::IsMessageQueueEmpty(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
*out = MessageQueueHelper::DequeueUnsafe(mq);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_full).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_full).SignalAllThreads();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TimedReceiveMessageQueue(uintptr_t *out, MessageQueueType *mq, TimeSpan timeout) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait receivable. */
|
|
|
|
impl::TimeoutHelper timeout_helper(timeout);
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueEmpty(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
if (timeout_helper.TimedOut()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GetReference(mq->cv_not_empty).TimedWait(GetPointer(mq->cs_queue), timeout_helper);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Receive, signal. */
|
2021-09-29 21:24:18 +02:00
|
|
|
*out = MessageQueueHelper::DequeueUnsafe(mq);
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_full).Broadcast();
|
|
|
|
GetReference(mq->waitlist_not_full).SignalAllThreads();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Peek functionality */
|
|
|
|
void PeekMessageQueue(uintptr_t *out, const MessageQueueType *mq) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait receivable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueEmpty(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
GetReference(mq->cv_not_empty).Wait(GetPointer(mq->cs_queue));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Peek. */
|
2021-09-29 21:24:18 +02:00
|
|
|
*out = MessageQueueHelper::PeekUnsafe(mq);
|
2020-04-08 11:21:35 +02:00
|
|
|
}
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TryPeekMessageQueue(uintptr_t *out, const MessageQueueType *mq) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, check receivable. */
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
if (MessageQueueHelper::IsMessageQueueEmpty(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Peek. */
|
2021-09-29 21:24:18 +02:00
|
|
|
*out = MessageQueueHelper::PeekUnsafe(mq);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
bool TimedPeekMessageQueue(uintptr_t *out, const MessageQueueType *mq, TimeSpan timeout) {
|
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
{
|
|
|
|
/* Acquire mutex, wait receivable. */
|
|
|
|
impl::TimeoutHelper timeout_helper(timeout);
|
|
|
|
std::scoped_lock lk(GetReference(mq->cs_queue));
|
|
|
|
|
2021-09-29 21:24:18 +02:00
|
|
|
while (MessageQueueHelper::IsMessageQueueEmpty(mq)) {
|
2020-04-08 11:21:35 +02:00
|
|
|
if (timeout_helper.TimedOut()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GetReference(mq->cv_not_empty).TimedWait(GetPointer(mq->cs_queue), timeout_helper);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Peek. */
|
2021-09-29 21:24:18 +02:00
|
|
|
*out = MessageQueueHelper::PeekUnsafe(mq);
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:00:47 +02:00
|
|
|
void InitializeMultiWaitHolder(MultiWaitHolderType *multi_wait_holder, MessageQueueType *mq, MessageQueueWaitType type) {
|
2021-04-10 10:58:26 +02:00
|
|
|
AMS_ASSERT(mq->state == MessageQueueType::State_Initialized);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MessageQueueWaitType::ForNotFull:
|
2021-10-01 04:00:47 +02:00
|
|
|
util::ConstructAt(GetReference(multi_wait_holder->impl_storage).holder_of_mq_for_not_full_storage, mq);
|
2021-04-10 10:58:26 +02:00
|
|
|
break;
|
|
|
|
case MessageQueueWaitType::ForNotEmpty:
|
2021-10-01 04:00:47 +02:00
|
|
|
util::ConstructAt(GetReference(multi_wait_holder->impl_storage).holder_of_mq_for_not_empty_storage, mq);
|
2021-04-10 10:58:26 +02:00
|
|
|
break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:00:47 +02:00
|
|
|
multi_wait_holder->user_data = 0;
|
2021-04-10 10:58:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|