The Pedigree Project 0.1
advisory-lock-table.h
1/* Copyright (c) 2026, Pedigree Developers. */
2#ifndef POSIX_ADVISORY_LOCK_TABLE_H
3#define POSIX_ADVISORY_LOCK_TABLE_H
4
5#include <stddef.h>
6#include <stdint.h>
7
8namespace PosixAdvisory {
9constexpr size_t MaximumGrants = 4096;
10constexpr size_t MaximumWaiters = 256;
11constexpr int64_t LastOffset = INT64_MAX;
12
13enum class OwnerKind { Process, OpenDescription };
14enum class Namespace { Record, Flock };
15enum class Type { Read, Write, Unlock };
16enum class Result { Success, Conflict, Full };
17enum class RangeResult { Success, Invalid, Overflow };
18
19struct Range {
20 int64_t first = 0;
21 int64_t last = LastOffset;
22};
23
24struct Grant {
25 uintptr_t inode = 0;
26 uint64_t owner = 0;
27 int32_t pid = 0;
28 OwnerKind kind = OwnerKind::Process;
29 Namespace name = Namespace::Record;
30 Type type = Type::Unlock;
31 Range range;
32};
33
34RangeResult normalise(int whence, int64_t start, int64_t length, uint64_t position,
35 uint64_t fileSize, Range& result);
36
38class Table {
39 public:
40 Table(Grant* current, Grant* scratch, size_t capacity);
41 bool conflict(const Grant& request, Grant& result) const;
42 bool query(const Grant& request, Grant& result) const;
43 Result apply(const Grant& request);
44 bool removeOwner(uint64_t owner, uintptr_t inode = 0);
45 bool wouldDeadlock(const Grant& request, const Grant* const* waiters, size_t waiterCount) const;
46 size_t count() const {
47 return m_Count;
48 }
49 const Grant& at(size_t index) const {
50 return m_Current[index];
51 }
52
53 private:
54 bool append(const Grant& grant, size_t& count);
55 Grant* m_Current;
56 Grant* m_Scratch;
57 const size_t m_Capacity;
58 size_t m_Count = 0;
59};
60} // namespace PosixAdvisory
61#endif