os/dd: primitive fixes

This commit is contained in:
Michael Scire
2019-12-03 19:26:43 -08:00
committed by SciresM
parent b1a9e8d0df
commit ad64cb5212
18 changed files with 276 additions and 65 deletions

View File

@@ -31,7 +31,7 @@ namespace ams::map {
uintptr_t cur_base = 0;
AddressSpaceInfo address_space;
R_TRY(GetProcessAddressSpaceInfo(&address_space, CUR_PROCESS_HANDLE));
R_TRY(GetProcessAddressSpaceInfo(&address_space, dd::GetCurrentProcessHandle()));
cur_base = address_space.aslr_base;
do {
@@ -57,7 +57,7 @@ namespace ams::map {
uintptr_t cur_base = 0, cur_end = 0;
AddressSpaceInfo address_space;
R_TRY(GetProcessAddressSpaceInfo(&address_space, CUR_PROCESS_HANDLE));
R_TRY(GetProcessAddressSpaceInfo(&address_space, dd::GetCurrentProcessHandle()));
cur_base = address_space.aslr_base;
cur_end = cur_base + size;

View File

@@ -25,13 +25,25 @@ namespace ams::os::impl {
private:
MessageQueue *message_queue;
private:
TriBool IsSignaledImpl() const {
constexpr inline TriBool IsSignaledImpl() const {
if constexpr (WaitKind == MessageQueueWaitKind::ForNotEmpty) {
/* ForNotEmpty. */
return this->message_queue->IsEmpty() ? TriBool::False : TriBool::True;
} else /* if constexpr (WaitKind == MessageQueueWaitKind::ForNotFull) */ {
} else if constexpr (WaitKind == MessageQueueWaitKind::ForNotFull) {
/* ForNotFull */
return this->message_queue->IsFull() ? TriBool::False : TriBool::True;
} else {
static_assert(WaitKind != WaitKind);
}
}
constexpr inline WaitableObjectList &GetObjectList() const {
if constexpr (WaitKind == MessageQueueWaitKind::ForNotEmpty) {
return GetReference(this->message_queue->waitlist_not_empty);
} else if constexpr (WaitKind == MessageQueueWaitKind::ForNotFull) {
return GetReference(this->message_queue->waitlist_not_full);
} else {
static_assert(WaitKind != WaitKind);
}
}
public:
@@ -46,14 +58,14 @@ namespace ams::os::impl {
virtual TriBool LinkToObjectList() override {
std::scoped_lock lk(this->message_queue->queue_lock);
GetReference(this->message_queue->waitable_object_list_storage).LinkWaitableHolder(*this);
this->GetObjectList().LinkWaitableHolder(*this);
return this->IsSignaledImpl();
}
virtual void UnlinkFromObjectList() override {
std::scoped_lock lk(this->message_queue->queue_lock);
GetReference(this->message_queue->waitable_object_list_storage).UnlinkWaitableHolder(*this);
this->GetObjectList().UnlinkWaitableHolder(*this);
}
};

View File

@@ -31,7 +31,7 @@ namespace ams::os::impl {
}
}
void WakeupAllThreads() {
void BroadcastAllThreads() {
for (WaitableHolderBase &holder_base : this->object_list) {
holder_base.GetManager()->SignalAndWakeupThread(nullptr);
}

View File

@@ -90,7 +90,7 @@ namespace ams::os {
if (this->counter != cur_counter) {
break;
}
if (R_FAILED(this->cv.TimedWait(&this->lock, timeout_helper.NsUntilTimeout()))) {
if (this->cv.TimedWait(&this->lock, timeout_helper.NsUntilTimeout()) == ConditionVariableStatus::TimedOut) {
return false;
}
}

View File

@@ -17,12 +17,14 @@
namespace ams::os {
MessageQueue::MessageQueue(std::unique_ptr<uintptr_t[]> buf, size_t c): buffer(std::move(buf)), capacity(c) {
new (GetPointer(this->waitable_object_list_storage)) impl::WaitableObjectList();
MessageQueue::MessageQueue(std::unique_ptr<uintptr_t[]> buf, size_t c): buffer(std::move(buf)), capacity(c), count(0), offset(0) {
new (GetPointer(this->waitlist_not_empty)) impl::WaitableObjectList();
new (GetPointer(this->waitlist_not_full)) impl::WaitableObjectList();
}
MessageQueue::~MessageQueue() {
GetReference(this->waitable_object_list_storage).~WaitableObjectList();
GetReference(this->waitlist_not_empty).~WaitableObjectList();
GetReference(this->waitlist_not_full).~WaitableObjectList();
}
void MessageQueue::SendInternal(uintptr_t data) {
@@ -53,7 +55,7 @@ namespace ams::os {
return data;
}
uintptr_t MessageQueue::PeekInternal() {
inline uintptr_t MessageQueue::PeekInternal() {
/* Ensure we don't corrupt the queue, but this should never happen. */
AMS_ASSERT(this->count > 0);
@@ -70,7 +72,8 @@ namespace ams::os {
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
this->cv_not_empty.Broadcast();
GetReference(this->waitlist_not_empty).SignalAllThreads();
}
bool MessageQueue::TrySend(uintptr_t data) {
@@ -81,7 +84,8 @@ namespace ams::os {
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
this->cv_not_empty.Broadcast();
GetReference(this->waitlist_not_empty).SignalAllThreads();
return true;
}
@@ -94,12 +98,13 @@ namespace ams::os {
return false;
}
this->cv_not_full.TimedWait(&this->queue_lock, timeout);
this->cv_not_full.TimedWait(&this->queue_lock, timeout_helper.NsUntilTimeout());
}
/* Send, signal. */
this->SendInternal(data);
this->cv_not_empty.WakeAll();
this->cv_not_empty.Broadcast();
GetReference(this->waitlist_not_empty).SignalAllThreads();
return true;
}
@@ -113,7 +118,8 @@ namespace ams::os {
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
this->cv_not_empty.Broadcast();
GetReference(this->waitlist_not_empty).SignalAllThreads();
}
bool MessageQueue::TrySendNext(uintptr_t data) {
@@ -124,7 +130,8 @@ namespace ams::os {
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
this->cv_not_empty.Broadcast();
GetReference(this->waitlist_not_empty).SignalAllThreads();
return true;
}
@@ -137,12 +144,13 @@ namespace ams::os {
return false;
}
this->cv_not_full.TimedWait(&this->queue_lock, timeout);
this->cv_not_full.TimedWait(&this->queue_lock, timeout_helper.NsUntilTimeout());
}
/* Send, signal. */
this->SendNextInternal(data);
this->cv_not_empty.WakeAll();
this->cv_not_empty.Broadcast();
GetReference(this->waitlist_not_empty).SignalAllThreads();
return true;
}
@@ -156,8 +164,10 @@ namespace ams::os {
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
this->cv_not_full.Broadcast();
GetReference(this->waitlist_not_full).SignalAllThreads();
}
bool MessageQueue::TryReceive(uintptr_t *out) {
/* Acquire mutex, wait receivable. */
std::scoped_lock lock(this->queue_lock);
@@ -168,7 +178,8 @@ namespace ams::os {
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
this->cv_not_full.Broadcast();
GetReference(this->waitlist_not_full).SignalAllThreads();
return true;
}
@@ -181,12 +192,13 @@ namespace ams::os {
return false;
}
this->cv_not_empty.TimedWait(&this->queue_lock, timeout);
this->cv_not_empty.TimedWait(&this->queue_lock, timeout_helper.NsUntilTimeout());
}
/* Receive, signal. */
*out = this->ReceiveInternal();
this->cv_not_full.WakeAll();
this->cv_not_full.Broadcast();
GetReference(this->waitlist_not_full).SignalAllThreads();
return true;
}
@@ -224,7 +236,7 @@ namespace ams::os {
return false;
}
this->cv_not_empty.TimedWait(&this->queue_lock, timeout);
this->cv_not_empty.TimedWait(&this->queue_lock, timeout_helper.NsUntilTimeout());
}
/* Peek. */

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2018-2019 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>
namespace ams {
namespace {
constexpr inline ::Handle GetCurrentProcessHandleImpl() {
return CUR_PROCESS_HANDLE;
}
}
namespace os {
::Handle __attribute__((const)) GetCurrentProcessHandle() {
return GetCurrentProcessHandleImpl();
}
}
namespace dd {
::Handle __attribute__((const)) GetCurrentProcessHandle() {
return GetCurrentProcessHandleImpl();
}
}
}