2017-05-30 00:45:30 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-11-14 06:23:04 +01:00
|
|
|
#include <atomic>
|
2019-12-08 04:09:20 +01:00
|
|
|
#include <memory>
|
2017-05-30 00:45:30 +02:00
|
|
|
#include <vector>
|
2019-12-08 04:09:20 +01:00
|
|
|
|
2018-08-02 04:40:00 +02:00
|
|
|
#include "core/hle/kernel/object.h"
|
2017-05-30 00:45:30 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2018-08-28 18:30:33 +02:00
|
|
|
class KernelCore;
|
2020-02-25 21:38:33 +01:00
|
|
|
class Synchronization;
|
2017-05-30 00:45:30 +02:00
|
|
|
class Thread;
|
|
|
|
|
|
|
|
/// Class that represents a Kernel object that a thread can be waiting on
|
2020-02-11 15:46:25 +01:00
|
|
|
class SynchronizationObject : public Object {
|
2017-05-30 00:45:30 +02:00
|
|
|
public:
|
2020-02-11 15:46:25 +01:00
|
|
|
explicit SynchronizationObject(KernelCore& kernel);
|
|
|
|
~SynchronizationObject() override;
|
2018-08-28 18:30:33 +02:00
|
|
|
|
2017-05-30 00:45:30 +02:00
|
|
|
/**
|
|
|
|
* Check if the specified thread should wait until the object is available
|
|
|
|
* @param thread The thread about which we're deciding.
|
|
|
|
* @return True if the current thread should wait due to this object being unavailable
|
|
|
|
*/
|
2019-04-02 00:19:42 +02:00
|
|
|
virtual bool ShouldWait(const Thread* thread) const = 0;
|
2017-05-30 00:45:30 +02:00
|
|
|
|
|
|
|
/// Acquire/lock the object for the specified thread if it is available
|
|
|
|
virtual void Acquire(Thread* thread) = 0;
|
|
|
|
|
2020-02-11 22:36:39 +01:00
|
|
|
/// Signal this object
|
|
|
|
virtual void Signal();
|
|
|
|
|
|
|
|
virtual bool IsSignaled() const {
|
|
|
|
return is_signaled;
|
|
|
|
}
|
|
|
|
|
2017-05-30 00:45:30 +02:00
|
|
|
/**
|
|
|
|
* Add a thread to wait on this object
|
|
|
|
* @param thread Pointer to thread to add
|
|
|
|
*/
|
2019-11-25 02:15:51 +01:00
|
|
|
void AddWaitingThread(std::shared_ptr<Thread> thread);
|
2017-05-30 00:45:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a thread from waiting on this object (e.g. if it was resumed already)
|
|
|
|
* @param thread Pointer to thread to remove
|
|
|
|
*/
|
2019-11-25 02:15:51 +01:00
|
|
|
void RemoveWaitingThread(std::shared_ptr<Thread> thread);
|
2017-05-30 00:45:30 +02:00
|
|
|
|
|
|
|
/// Get a const reference to the waiting threads list for debug use
|
2019-11-25 02:15:51 +01:00
|
|
|
const std::vector<std::shared_ptr<Thread>>& GetWaitingThreads() const;
|
2017-05-30 00:45:30 +02:00
|
|
|
|
2020-02-27 03:26:53 +01:00
|
|
|
void ClearWaitingThreads();
|
|
|
|
|
2020-02-11 22:36:39 +01:00
|
|
|
protected:
|
2020-11-14 06:23:04 +01:00
|
|
|
std::atomic_bool is_signaled{}; // Tells if this sync object is signaled
|
2020-02-11 22:36:39 +01:00
|
|
|
|
2017-05-30 00:45:30 +02:00
|
|
|
private:
|
|
|
|
/// Threads waiting for this object to become available
|
2019-11-25 02:15:51 +01:00
|
|
|
std::vector<std::shared_ptr<Thread>> waiting_threads;
|
2017-05-30 00:45:30 +02:00
|
|
|
};
|
|
|
|
|
2020-02-11 15:46:25 +01:00
|
|
|
// Specialization of DynamicObjectCast for SynchronizationObjects
|
2017-05-30 00:45:30 +02:00
|
|
|
template <>
|
2020-02-11 15:46:25 +01:00
|
|
|
inline std::shared_ptr<SynchronizationObject> DynamicObjectCast<SynchronizationObject>(
|
|
|
|
std::shared_ptr<Object> object) {
|
2017-05-30 00:45:30 +02:00
|
|
|
if (object != nullptr && object->IsWaitable()) {
|
2020-02-11 15:46:25 +01:00
|
|
|
return std::static_pointer_cast<SynchronizationObject>(object);
|
2017-05-30 00:45:30 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|