2017-05-29 22:45:30 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-12-06 15:18:39 +00:00
|
|
|
#include <functional>
|
2019-03-23 21:09:02 +00:00
|
|
|
#include <memory>
|
2017-05-29 22:45:30 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
|
|
|
#include "common/common_types.h"
|
2018-08-02 02:40:00 +00:00
|
|
|
#include "core/hle/kernel/object.h"
|
2017-05-29 22:45:30 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class Thread;
|
|
|
|
|
|
|
|
/// Class that represents a Kernel object that a thread can be waiting on
|
|
|
|
class WaitObject : public Object {
|
|
|
|
public:
|
2018-10-13 21:24:51 +00:00
|
|
|
using Object::Object;
|
|
|
|
|
2017-05-29 22:45:30 +00: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
|
|
|
|
*/
|
|
|
|
virtual bool ShouldWait(Thread* thread) const = 0;
|
|
|
|
|
|
|
|
/// Acquire/lock the object for the specified thread if it is available
|
|
|
|
virtual void Acquire(Thread* thread) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a thread to wait on this object
|
|
|
|
* @param thread Pointer to thread to add
|
|
|
|
*/
|
2019-03-23 20:04:19 +00:00
|
|
|
virtual void AddWaitingThread(std::shared_ptr<Thread> thread);
|
2017-05-29 22:45:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a thread from waiting on this object (e.g. if it was resumed already)
|
|
|
|
* @param thread Pointer to thread to remove
|
|
|
|
*/
|
|
|
|
virtual void RemoveWaitingThread(Thread* thread);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wake up all threads waiting on this object that can be awoken, in priority order,
|
|
|
|
* and set the synchronization result and output of the thread.
|
|
|
|
*/
|
|
|
|
virtual void WakeupAllWaitingThreads();
|
|
|
|
|
|
|
|
/// Obtains the highest priority thread that is ready to run from this object's waiting list.
|
2019-04-17 10:44:31 +00:00
|
|
|
std::shared_ptr<Thread> GetHighestPriorityReadyThread() const;
|
2017-05-29 22:45:30 +00:00
|
|
|
|
|
|
|
/// Get a const reference to the waiting threads list for debug use
|
2019-03-23 20:04:19 +00:00
|
|
|
const std::vector<std::shared_ptr<Thread>>& GetWaitingThreads() const;
|
2017-05-29 22:45:30 +00:00
|
|
|
|
2018-12-06 15:18:39 +00:00
|
|
|
/// Sets a callback which is called when the object becomes available
|
|
|
|
void SetHLENotifier(std::function<void()> callback);
|
|
|
|
|
2017-05-29 22:45:30 +00:00
|
|
|
private:
|
|
|
|
/// Threads waiting for this object to become available
|
2019-03-23 20:04:19 +00:00
|
|
|
std::vector<std::shared_ptr<Thread>> waiting_threads;
|
2018-12-06 15:18:39 +00:00
|
|
|
|
|
|
|
/// Function to call when this object becomes available
|
|
|
|
std::function<void()> hle_notifier;
|
2017-05-29 22:45:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization of DynamicObjectCast for WaitObjects
|
|
|
|
template <>
|
2019-03-23 20:04:19 +00:00
|
|
|
inline std::shared_ptr<WaitObject> DynamicObjectCast<WaitObject>(std::shared_ptr<Object> object) {
|
2017-05-29 22:45:30 +00:00
|
|
|
if (object != nullptr && object->IsWaitable()) {
|
2019-03-23 20:04:19 +00:00
|
|
|
return std::static_pointer_cast<WaitObject>(object);
|
2017-05-29 22:45:30 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|