-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathlocked-object.jl
57 lines (54 loc) · 1.47 KB
/
locked-object.jl
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
# Copied from CUDA.jl
struct LockedObject{T}
lock::ReentrantLock
payload::T
end
LockedObject(payload) = LockedObject(Threads.ReentrantLock(), payload)
function Base.lock(f, x::LockedObject)
lock(x.lock)
try
return f(x.payload)
finally
unlock(x.lock)
end
end
Base.lock(x::LockedObject) = lock(x.lock)
Base.trylock(x::LockedObject) = trylock(x.lock)
Base.unlock(x::LockedObject) = unlock(x.lock)
payload(x::LockedObject) = x.payload
# TODO: Move these back to MemPool
macro safe_lock1(l, o, ex)
quote
temp = $(esc(l))
lock(temp)
MemPool.enable_finalizers(false)
try
$(esc(o)) = $payload(temp)
$(esc(ex))
finally
unlock(temp)
MemPool.enable_finalizers(true)
end
end
end
# If we actually want to acquire a lock from a finalizer, we can't cause a task
# switch. As a NonReentrantLock can only be taken by another thread that should
# be running, and not a concurrent task we'd need to switch to, we can safely
# spin.
macro safe_lock_spin1(l, o, ex)
quote
temp = $(esc(l))
while !trylock(temp)
# we can't yield here
GC.safepoint()
end
MemPool.enable_finalizers(false) # retains compatibility with non-finalizer callers
try
$(esc(o)) = $payload(temp)
$(esc(ex))
finally
unlock(temp)
MemPool.enable_finalizers(true)
end
end
end