/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: LSMutex.Linux.cpp Date: 2021-10-1 Date: 2022-4-4 Author: Reece ***/ #include #include "LSMutex.hpp" #include "LSFromFdNonblocking.hpp" namespace Aurora::Loop { LSMutex::LSMutex() { } bool LSMutex::OnTrigger(AuUInt handle) { AuUInt64 oldSemaphoreValue {}; auto ok = read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8; SysAssertDbg(oldSemaphoreValue == 1, "Double unlock caught"); return ok; } void LSMutex::Init() { handle = eventfd(1, EFD_NONBLOCK); } bool LSMutex::Unlock() { AuUInt64 plsNoOverflow {1}; return write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) == 8; } bool LSMutex::IsSignaled() { return IsSignaledFromNonblockingImpl(this, this, &LSMutex::IsSignaledNonblocking); } bool LSMutex::IsSignaledNonblocking() { AuUInt64 oldSemaphoreValue {}; auto ok = read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8; SysAssertDbg(!ok || oldSemaphoreValue == 1, "Double unlock caught"); return ok; } ELoopSource LSMutex::GetType() { return ELoopSource::eSourceMutex; } AUKN_SYM AuSPtr NewLSMutex() { auto mutex = AuMakeShared(); if (!mutex) { return {}; } if (!mutex->HasValidHandle()) { return {}; } return mutex; } }