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