-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpthread_test.lua
293 lines (253 loc) · 6.3 KB
/
pthread_test.lua
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
local pthread = require'pthread'
local lua = require'luastate'
local ffi = require'ffi'
local glue = require'glue'
local pp = require'pp'
io.stdout:setvbuf'no'
--helpers
local function addr(cdata)
return tonumber(ffi.cast('intptr_t', ffi.cast('void*', cdata)))
end
local function ptr(ctype, p)
return ffi.cast(ctype, ffi.cast('void*', p))
end
--globals
local function test_priority_range()
local pr0 = pthread.min_priority()
local pr1 = pthread.max_priority()
print('priority range: ', pr0, pr1)
assert(pr1 >= pr0)
end
--threads
--test pthread_create(), pthread_join()
--create a new Lua state and a new thread, and run a worker function
--in that state and thread.
local function create_thread(worker, args, attrs)
local state = lua.open()
state:openlibs()
state:push(function(worker, args)
local ffi = require'ffi'
local function pass(...)
_G.retvals = {n = select('#', ...), ...}
end
local function wrapper()
pass(worker(args))
end
local wrapper_cb = ffi.cast('void *(*)(void *)', wrapper)
return tonumber(ffi.cast('intptr_t', wrapper_cb))
end)
local wrapper_cb_ptr = ffi.cast('void *', state:call(worker, args))
local thread = pthread.new(wrapper_cb_ptr, attrs)
local function join()
local status = thread:join()
state:getglobal'retvals'
local t = state:get(-1) or {n = 0}
state:close()
return status, unpack(t, 1, t.n)
end
return join, thread
end
--test pthread_self(), pthread_equal()
local function test_thread_self_equal()
local join, th1 = create_thread(function()
local pthread = require'pthread'
local ffi = require'ffi'
local th = pthread.self()
return ffi.string(th, ffi.sizeof(th))
end)
local _, ths = join()
local th2 = ffi.new'pthread_t'
ffi.copy(th2, ths, #ths)
assert(th1:equal(th2))
end
local function test_priorities()
create_thread(function() end, nil,
{priority = pthread.max_priority()})()
create_thread(function() end, nil,
{priority = pthread.min_priority()})()
end
--speed/leak long test
local function stress_test(times)
io.stdout:write'creating many threads '
for i=1,times do
local joins = {}
local n=10
for i=1,n do
local join, th = create_thread(function(i)
io.stdout:write(i..' ')
end, i)
table.insert(joins, join)
end
for i=n,1,-1 do
joins[i]()
end
collectgarbage()
end
print()
end
--mutexes
local function test_mutex(times, threads)
local m = pthread.mutex{type = 'recursive'}
local joins = {}
local n = ffi.new'int[1]'
for i=1,threads do
local join = create_thread(function(args)
local m, times, n = unpack(args)
local ffi = require'ffi'
local pthread = require'pthread'
local function ptr(ctype, p)
return ffi.cast(ctype, ffi.cast('void*', p))
end
local m = ptr('pthread_mutex_t*', m)
n = ptr('int*', n)
local p=0
for i=1,times do
while not m:trylock() do
p=p+1
end
n[0]=n[0]+1
m:unlock()
end
return p
end, {addr(m), times, addr(n)})
table.insert(joins, join)
end
print'mutex trylocks:'
local np = 0
for i=1,threads do
local _, p = joins[i]()
print('', 'thread ', i, p)
np = np + p
end
assert(n[0] == threads * times)
print(string.format('failed trylocks: %d%%', np / n[0] * 100))
m:free()
end
--test cond. vars
local function test_cond_var(times, timeout)
local mutex = pthread.mutex()
local cond = pthread.cond()
local n = ffi.new('double[1]', -times/2)
local join1 = create_thread(function(args)
local mutex, cond, times, timeout, n = unpack(args)
local ffi = require'ffi'
local pthread = require'pthread'
local function ptr(ctype, p)
return ffi.cast(ctype, ffi.cast('void*', p))
end
local mutex = ptr('pthread_mutex_t*', mutex)
local cond = ptr('pthread_cond_t*', cond)
n = ptr('double*', n)
local p, t = 0, 0
for i=1,times do
mutex:lock()
if n[0] == 100 then
mutex:unlock()
break
end
while n[0] < 0 do
if not cond:wait(mutex, os.time() + timeout) then
t = t + 1
else
--p = p + 1
end
end
if n[0] >= 0 then
p = p + 1
end
mutex:unlock()
end
return p, t
end, {addr(mutex), addr(cond), times, timeout, addr(n)})
local join2 = create_thread(function(args)
local mutex, cond, times, n = unpack(args)
local ffi = require'ffi'
local pthread = require'pthread'
local function ptr(ctype, p)
return ffi.cast(ctype, ffi.cast('void*', p))
end
local mutex = ptr('pthread_mutex_t*', mutex)
local cond = ptr('pthread_cond_t*', cond)
n = ptr('double*', n)
for i=1,times do
mutex:lock()
n[0] = math.sin(i/10)
if n[0] >= 0 then
cond:broadcast()
end
mutex:unlock()
end
--signal exit to other thread
mutex:lock()
n[0] = 100
cond:broadcast()
mutex:unlock()
end, {addr(mutex), addr(cond), times, addr(n)})
local _, p, t = join1()
join2()
print(string.format('cond. var: caught: %d%%, timeouts: %d%%',
p/times * 100, t/times * 100))
cond:free()
mutex:free()
end
--test r/w locks
local function test_rwlock(readtimes, readthreads, writetimes, writethreads)
local rwlock = pthread.rwlock()
local joins = {}
local n = ffi.new'int[1]'
for i = 1, readthreads + writethreads do
local reader = i > writethreads
local join = create_thread(function(args)
local rwlock, times, n, reader = unpack(args)
local ffi = require'ffi'
local pthread = require'pthread'
local function ptr(ctype, p)
return ffi.cast(ctype, ffi.cast('void*', p))
end
local rwlock = ptr('pthread_rwlock_t*', rwlock)
n = ptr('int*', n)
local p=0
for i = 1, times do
if reader then
while not rwlock:tryreadlock() do
p=p+1
end
rwlock:unlock()
else
while not rwlock:trywritelock() do
p=p+1
end
n[0]=n[0]+1
rwlock:unlock()
end
end
return p
end, {
addr(rwlock),
reader and readtimes or writetimes,
addr(n),
reader,
})
table.insert(joins, join)
end
print'rwlock trylocks:'
local np = 0
for i = 1, readthreads + writethreads do
local _, p = joins[i]()
print('', (i > writethreads and 'read' or 'write')..' thread ', i, p)
np = np + p
end
assert(n[0] == writethreads * writetimes)
print(string.format('failed trywritelocks: %d%%', np / n[0] * 100))
rwlock:free()
end
local function test_all()
test_priority_range()
test_thread_self_equal()
test_priorities()
stress_test(10)
test_mutex(50000, 10)
test_cond_var(100000, 1)
test_rwlock(50000, 10, 50000, 1)
end
test_all()