Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 3437053

Browse files
Fixed issue with timeouts, and added tests from them.
1 parent 9270b80 commit 3437053

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

jitthreads/thread.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ Thread.__gc = Thread.destroy
155155
--- Waits for the thread to terminate, or after the timeout has passed
156156
-- @param timeout Number of seconds to wait. nil = no timeout
157157
-- @return True if the thread exited successfully, false and nil
158-
-- if the function returned due to timeout, and
158+
-- if the function returned due to timeout, or
159159
-- false and a string error message if the thread terminated
160160
-- due to an error.
161161
function Thread:join(timeout)
162162
if self.thread == nil then error("invalid thread",2) end
163-
if abstractions.thread_join(self.thread) then
163+
if abstractions.thread_join(self.thread, timeout) then
164164
local r = C.lua_tointeger(self.state, -1)
165165
if r ~= 0 then
166166
local len_b = int_b()
@@ -169,7 +169,7 @@ function Thread:join(timeout)
169169
end
170170
return true
171171
else
172-
return false, Threading.TIMEOUT
172+
return false
173173
end
174174
end
175175

tests.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
local Tests = {
33
"basic",
44
"mutex",
5+
"join_timeout",
6+
"mutex_timeout",
57
}
68

79
for _,f in ipairs(Tests) do

tests/basic.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@ local function testThread(c, f, ...)
1313
thread:destroy()
1414
end
1515

16-
16+
print("Basic hello world thread")
1717
testThread(1, function()
18-
print("Thread 1 says hi!")
18+
print("\tThread 1 says hi!")
1919
end)
20+
21+
print("\nThread error test")
2022
testThread(2, function()
2123
error("Thread 2 has errors.")
2224
end)
25+
26+
print("\nArguments test")
2327
testThread(3, function(...)
24-
print("Got values:",...)
28+
print("\tGot values:",...)
2529
end, nil, 2, "c", true)
2630

31+
print("\nCdata test")
2732
local vec = ffi.new("struct {int x, y, z;}", 100,200,300)
2833
testThread(4, function(v)
2934
local ffi = require "ffi"
3035
v = ffi.cast("struct {int x,y,z;}*", v)
31-
print(v.x, v.y, v.z)
36+
print("",v.x, v.y, v.z)
3237
end, vec)

tests/join_timeout.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
local ThreadF = function()
3+
local ffi = require "ffi"
4+
if ffi.os == "Windows" then
5+
ffi.cdef[[void Sleep(uint32_t);]]
6+
ffi.C.Sleep(5000)
7+
else
8+
ffi.cdef[[unsigned int sleep(unsigned int);]]
9+
ffi.C.sleep(5)
10+
end
11+
end
12+
13+
local Thread = require "jitthreads.thread"
14+
local Mutex = require "jitthreads.mutex"
15+
16+
local t = Thread(ThreadF)
17+
print("Thread will run for 5 seconds. Joining with 1 second timeouts.")
18+
while true do
19+
local ok, err = t:join(1)
20+
if ok then
21+
print(" Joined")
22+
break
23+
elseif not err then
24+
print(" Timed out")
25+
else
26+
print(" Error:")
27+
print(err)
28+
break
29+
end
30+
end
31+
t:destroy()

tests/mutex.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ local function threadMain(threadid, m)
1515
end
1616
end
1717

18+
print("Each thread will try to aquire the mutex 20 times.")
19+
1820
local mutex = Mutex()
1921
local threads = {}
2022
for i=1,3 do

tests/mutex_timeout.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
local ThreadF = function(m)
3+
local ffi = require "ffi"
4+
local Mutex = require "jitthreads.Mutex"
5+
6+
m = ffi.cast(ffi.typeof("$*",Mutex),m)
7+
for i=1,5 do
8+
assert(not m:lock(1), "Thread locked the mutex, somehow.")
9+
print("Timed out, i=",i)
10+
end
11+
end
12+
13+
local Thread = require "jitthreads.thread"
14+
local Mutex = require "jitthreads.mutex"
15+
16+
local m = Mutex()
17+
assert(m:lock(), "Couldn't lock a new mutex")
18+
19+
print("Thread will try to aquire locked mutex 5 times with 1 second timeout")
20+
local t = Thread(ThreadF, m)
21+
t:join()
22+
t:destroy()
23+
m:unlock()
24+
m:destroy()

0 commit comments

Comments
 (0)