forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locks.jl
138 lines (116 loc) · 3.13 KB
/
locks.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# This file is a part of Julia. License is MIT: http:https://julialang.org/license
export SpinLock, Mutex, init_lock!, destroy_lock!, lock!, trylock!, unlock!
abstract AbstractLock
# Test-and-test-and-set spin locks are quickest up to about 30ish
# contending threads. If you have more contention than that, perhaps
# a lock is the wrong way to synchronize.
type TatasLock <: AbstractLock
handle::Atomic{Int}
TatasLock() = new(Atomic{Int}(0))
end
typealias SpinLock TatasLock
function lock!(l::TatasLock)
while true
if l.handle[] == 0
p = atomic_xchg!(l.handle, 1)
if p == 0
return 0
end
end
# TODO: pause
end
end
function trylock!(l::TatasLock)
if l.handle[] == 0
return atomic_xchg!(l.handle, 1)
end
return 1
end
function unlock!(l::TatasLock)
l.handle[] = 0
return 0
end
# Recursive test-and-test-and-set lock. Slower.
type RecursiveTatasLock <: AbstractLock
ownertid::Atomic{Int16}
handle::Atomic{Int}
RecursiveTatasLock() = new(0, Atomic{Int}(0))
end
typealias RecursiveSpinLock RecursiveTatasLock
function lock!(l::RecursiveTatasLock)
if l.ownertid[] == threadid()
return 0
end
while true
if l.handle[] == 0
p = atomic_xchg!(l.handle, 1)
if p == 0
l.ownertid[] = threadid()
return 0
end
end
# TODO: pause
end
end
function trylock!(l::RecursiveTatasLock)
if l.ownertid[] == threadid()
return 0
end
if l.handle[] == 0
p = atomic_xchg!(l.handle, 1)
if p == 0
l.ownertid[] = threadid()
end
return p
end
return 1
end
function unlock!(l::RecursiveTatasLock)
if l.ownertid[] != threadid()
return 1
end
l.ownertid[] = 0
l.handle[] = 0
return 0
end
# These are mutexes from libuv, which abstract pthread mutexes and
# Windows critical sections. We're doing some error checking (and
# paying for it in overhead), but regardless, in some situations,
# passing a bad parameter will cause an abort.
# TODO: how defensive to get, and how to turn it off?
# TODO: how to catch an abort?
const UV_MUTEX_SIZE = ccall(:jl_sizeof_uv_mutex, Cint, ())
type Mutex <: AbstractLock
ownertid::Int16
handle::Array{Int8}
Mutex() = (m = new(zero(Int16), zeros(Int8, UV_MUTEX_SIZE));
ccall(:uv_mutex_init, Void, (Ptr{Void},), m.handle);
finalizer(m, (x -> ccall(:uv_mutex_destroy, Void, (Ptr{Void},), x.handle)));
m)
end
function lock!(m::Mutex)
if m.ownertid == threadid()
return 0
end
ccall(:uv_mutex_lock, Void, (Ptr{Void},), m.handle)
m.ownertid = threadid()
return 0
end
function trylock!(m::Mutex)
if m.ownertid == threadid()
return 0
end
r = ccall(:uv_mutex_trylock, Cint, (Ptr{Void},), m.handle)
if r == 0
m.ownertid = threadid()
end
return r
end
function unlock!(m::Mutex)
if m.ownertid != threadid()
return Base.UV_EPERM
end
m.ownertid = 0
ccall(:uv_mutex_unlock, Void, (Ptr{Void},), m.handle)
return 0
end