forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locks.jl
176 lines (149 loc) · 4.24 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# This file is a part of Julia. License is MIT: http://julialang.org/license
import Base: _uv_hook_close, unsafe_convert
export SpinLock, Mutex,
init_lock!, destroy_lock!, lock!, trylock!, unlock!
##########################################
# Atomic Locks
##########################################
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.
immutable 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
ccall(:jl_cpu_pause, Void, ())
# Temporary solution before we have gc transition support in codegen.
ccall(:jl_gc_safepoint, Void, ())
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
ccall(:jl_cpu_wake, Void, ())
return 0
end
# Recursive test-and-test-and-set lock. Slower.
immutable RecursiveTatasLock <: AbstractLock
ownertid::Atomic{Int16}
handle::Atomic{Int}
RecursiveTatasLock() = new(Atomic{Int16}(0), Atomic{Int}(0))
end
typealias RecursiveSpinLock RecursiveTatasLock
function lock!(l::RecursiveTatasLock)
if l.ownertid[] == threadid()
l.handle[] += 1
return 0
end
while true
if l.handle[] == 0
if atomic_cas!(l.handle, 0, 1) == 0
l.ownertid[] = threadid()
return 0
end
end
ccall(:jl_cpu_pause, Void, ())
# Temporary solution before we have gc transition support in codegen.
ccall(:jl_gc_safepoint, Void, ())
end
end
function trylock!(l::RecursiveTatasLock)
if l.ownertid[] == threadid()
l.handle[] += 1
return 0
end
if l.handle[] == 0
if atomic_cas!(l.handle, 0, 1) == 0
l.ownertid[] = threadid()
return 0
end
return 1
end
return 1
end
function unlock!(l::RecursiveTatasLock)
if l.ownertid[] != threadid()
return 1
end
if l.handle[] == 1
l.ownertid[] = 0
l.handle[] = 0
ccall(:jl_cpu_wake, Void, ())
else
l.handle[] -= 1
end
return 0
end
##########################################
# System Mutexes
##########################################
# 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::Ptr{Void}
function Mutex()
m = new(zero(Int16), Libc.malloc(UV_MUTEX_SIZE))
ccall(:uv_mutex_init, Void, (Ptr{Void},), m.handle)
finalizer(m, _uv_hook_close)
return m
end
end
unsafe_convert(::Type{Ptr{Void}}, m::Mutex) = m.handle
function _uv_hook_close(x::Mutex)
h = x.handle
x.handle = C_NULL
ccall(:uv_mutex_destroy, Void, (Ptr{Void},), h)
Libc.free(h)
nothing
end
function lock!(m::Mutex)
if m.ownertid == threadid()
return 0
end
# Temporary solution before we have gc transition support in codegen.
# This could mess up gc state when we add codegen support.
gc_state = ccall(:jl_gc_safe_enter, Int8, ())
ccall(:uv_mutex_lock, Void, (Ptr{Void},), m)
ccall(:jl_gc_safe_leave, Void, (Int8,), gc_state)
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)
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)
return 0
end