#pragma once #include #include #include "include/ztypes.h" #include "object/kernel_object.h" #include "object/thread.h" class Mutex; template <> struct KernelObjectTag { static const uint64_t type = KernelObject::MUTEX; }; class Mutex : public KernelObject { public: uint64_t TypeTag() override { return KernelObject::MUTEX; } static uint64_t DefaultPermissions() { return kZionPerm_Lock | kZionPerm_Release; } static glcr::RefPtr Create(); // Attempts to lock the mutex, suspends the current thread // until the lock is acquired. void Lock(); // Releases the mutex and activates the next thread in the // blocked queue if it exists. void Release(); private: uint8_t lock_ = 0; glcr::IntrusiveList blocked_threads_; Mutex() {} }; class MutexHolder { public: MutexHolder(const glcr::RefPtr& mutex) : mutex_(mutex) { mutex_->Lock(); } ~MutexHolder() { mutex_->Release(); } MutexHolder(MutexHolder&) = delete; MutexHolder(MutexHolder&&) = delete; private: glcr::RefPtr mutex_; };