mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 15:07:45 +00:00
LibCore: Add LockFile, a filesystem based mutex
This API wraps flock(2) and also handles the file creation and deletion when the LockFile goes out of scope.
This commit is contained in:
parent
7da12f0faf
commit
fdcfd2816e
3 changed files with 90 additions and 0 deletions
32
Userland/Libraries/LibCore/LockFile.h
Normal file
32
Userland/Libraries/LibCore/LockFile.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Peter Elliott <pelliott@ualberta.ca>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Core {
|
||||
|
||||
class LockFile {
|
||||
public:
|
||||
enum class Type {
|
||||
Exclusive,
|
||||
Shared
|
||||
};
|
||||
|
||||
LockFile(LockFile const& other) = delete;
|
||||
LockFile(char const* filename, Type type = Type::Exclusive);
|
||||
~LockFile();
|
||||
|
||||
bool is_held() const;
|
||||
int error_code() const { return m_errno; }
|
||||
void release();
|
||||
|
||||
private:
|
||||
int m_fd { -1 };
|
||||
int m_errno { 0 };
|
||||
char const* m_filename { nullptr };
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue