mirror of https://git.suyu.dev/suyu/suyu
Merge pull request #370 from Subv/sync_primitives
Kernel: Reworked the new kernel synchronization primitives.merge-requests/60/head
commit
0214351f4f
@ -1,64 +0,0 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "core/hle/kernel/condition_variable.h"
|
||||
#include "core/hle/kernel/errors.h"
|
||||
#include "core/hle/kernel/kernel.h"
|
||||
#include "core/hle/kernel/object_address_table.h"
|
||||
#include "core/hle/kernel/thread.h"
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
ConditionVariable::ConditionVariable() {}
|
||||
ConditionVariable::~ConditionVariable() {}
|
||||
|
||||
ResultVal<SharedPtr<ConditionVariable>> ConditionVariable::Create(VAddr guest_addr,
|
||||
std::string name) {
|
||||
SharedPtr<ConditionVariable> condition_variable(new ConditionVariable);
|
||||
|
||||
condition_variable->name = std::move(name);
|
||||
condition_variable->guest_addr = guest_addr;
|
||||
condition_variable->mutex_addr = 0;
|
||||
|
||||
// Condition variables are referenced by guest address, so track this in the kernel
|
||||
g_object_address_table.Insert(guest_addr, condition_variable);
|
||||
|
||||
return MakeResult<SharedPtr<ConditionVariable>>(std::move(condition_variable));
|
||||
}
|
||||
|
||||
bool ConditionVariable::ShouldWait(Thread* thread) const {
|
||||
return GetAvailableCount() <= 0;
|
||||
}
|
||||
|
||||
void ConditionVariable::Acquire(Thread* thread) {
|
||||
if (GetAvailableCount() <= 0)
|
||||
return;
|
||||
|
||||
SetAvailableCount(GetAvailableCount() - 1);
|
||||
}
|
||||
|
||||
ResultCode ConditionVariable::Release(s32 target) {
|
||||
if (target == -1) {
|
||||
// When -1, wake up all waiting threads
|
||||
SetAvailableCount(static_cast<s32>(GetWaitingThreads().size()));
|
||||
WakeupAllWaitingThreads();
|
||||
} else {
|
||||
// Otherwise, wake up just a single thread
|
||||
SetAvailableCount(target);
|
||||
WakeupWaitingThread(GetHighestPriorityReadyThread());
|
||||
}
|
||||
|
||||
return RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
s32 ConditionVariable::GetAvailableCount() const {
|
||||
return Memory::Read32(guest_addr);
|
||||
}
|
||||
|
||||
void ConditionVariable::SetAvailableCount(s32 value) const {
|
||||
Memory::Write32(guest_addr, value);
|
||||
}
|
||||
|
||||
} // namespace Kernel
|
@ -1,63 +0,0 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include "common/common_types.h"
|
||||
#include "core/hle/kernel/kernel.h"
|
||||
#include "core/hle/kernel/wait_object.h"
|
||||
#include "core/hle/result.h"
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class ConditionVariable final : public WaitObject {
|
||||
public:
|
||||
/**
|
||||
* Creates a condition variable.
|
||||
* @param guest_addr Address of the object tracking the condition variable in guest memory. If
|
||||
* specified, this condition variable will update the guest object when its state changes.
|
||||
* @param name Optional name of condition variable.
|
||||
* @return The created condition variable.
|
||||
*/
|
||||
static ResultVal<SharedPtr<ConditionVariable>> Create(VAddr guest_addr,
|
||||
std::string name = "Unknown");
|
||||
|
||||
std::string GetTypeName() const override {
|
||||
return "ConditionVariable";
|
||||
}
|
||||
std::string GetName() const override {
|
||||
return name;
|
||||
}
|
||||
|
||||
static const HandleType HANDLE_TYPE = HandleType::ConditionVariable;
|
||||
HandleType GetHandleType() const override {
|
||||
return HANDLE_TYPE;
|
||||
}
|
||||
|
||||
s32 GetAvailableCount() const;
|
||||
void SetAvailableCount(s32 value) const;
|
||||
|
||||
std::string name; ///< Name of condition variable (optional)
|
||||
VAddr guest_addr; ///< Address of the guest condition variable value
|
||||
VAddr mutex_addr; ///< (optional) Address of guest mutex value associated with this condition
|
||||
///< variable, used for implementing events
|
||||
|
||||
bool ShouldWait(Thread* thread) const override;
|
||||
void Acquire(Thread* thread) override;
|
||||
|
||||
/**
|
||||
* Releases a slot from a condition variable.
|
||||
* @param target The number of threads to wakeup, -1 is all.
|
||||
* @return ResultCode indicating if the operation succeeded.
|
||||
*/
|
||||
ResultCode Release(s32 target);
|
||||
|
||||
private:
|
||||
ConditionVariable();
|
||||
~ConditionVariable() override;
|
||||
};
|
||||
|
||||
} // namespace Kernel
|
Loading…
Reference in New Issue