-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocks.lua
More file actions
108 lines (77 loc) · 2.12 KB
/
locks.lua
File metadata and controls
108 lines (77 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
--- Locks to help you lock out resources, and await unlocking.
---@generic T
---@class FIFOQueue<T>: {queue: T[]}
local function FIFOQueue()
local queue = {queue = {}}
function queue.push(value)
table.insert(queue.queue, value)
end
function queue.push_first(value)
table.insert(queue.queue, 1, value)
end
function queue.pop()
return table.remove(queue.queue, 1)
end
function queue.is_empty()
return #queue.queue == 0
end
return queue
end
---@class Lock
---@field waiting FIFOQueue<string> The queue of tasks waiting for the lock to be unlocked. This is a queue of events to queue to resume the waiting task.
---@field locked boolean Whether the lock is currently locked.
local lock = {}
lock.__index = lock
local last_id = 0
--- Generates an ID for a lock.
---@return string
local function generate_id()
last_id = last_id + 1
return "lock_" .. tostring(last_id)
end
--- Create a new lock.
---@return Lock
function lock.new()
return setmetatable({
waiting = FIFOQueue(),
locked = false,
}, lock)
end
--- Lock the lock. If the lock is already locked, fails.
---@return boolean locked Whether the lock was successfully locked.
function lock:lock()
if self.locked then
return false
end
self.locked = true
return true
end
--- Lock the lock. If the lock is already locked, waits in the queue until it is unlocked.
--- This function should be preferred over polling `lock:lock()` until it returns `true`.
function lock:await_lock()
if self.locked then
local event = generate_id()
self.waiting.push(event)
os.pullEvent(event)
end
self.locked = true
end
--- Lock the lock. Inserts the new ID at the front of the queue.
--- This method is rude to other tasks, but useful for things that require priority.
function lock:await_lock_priority()
if self.locked then
local event = generate_id()
self.waiting.push_first(event)
os.pullEvent(event)
end
self.locked = true
end
--- Unlock the lock.
function lock:unlock()
self.locked = false
if not self.waiting.is_empty() then
local event = self.waiting.pop()
os.queueEvent(event)
end
end
return lock