Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions tests/testsoon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ suite "callSoon() tests suite":
check test() == true

test "cross-thread callSoon() test":
var crossThreadCallSoonFlag = false
var crossThreadCallSoonCount = 0
proc crossThreadCallSoonCallback(udata: pointer) {.nimcall, gcsafe, raises: [].} =
cast[ptr bool](udata)[] = true
type T = (DispatcherHandle, ptr bool)
cast[ptr int](udata)[] += 1
type T = (DispatcherHandle, ptr int)
proc threadProc(disp: T) {.thread, nimcall.} =
callSoon(disp[0], crossThreadCallSoonCallback, disp[1])
callSoon(disp[0], crossThreadCallSoonCallback, disp[1])

# Pass the current thread's dispatcher pointer to a new thread
crossThreadCallSoonFlag = false
let disp = getThreadDispatcher()
var thread: Thread[T]
createThread(thread, threadProc, (disp.handle(), addr crossThreadCallSoonFlag))
createThread(thread, threadProc, (disp.handle(), addr crossThreadCallSoonCount))

# The callback was posted to the dispatcher's queue; poll to execute it
poll()
check: crossThreadCallSoonFlag == true
# Callbacks and wake-ups don't map 1:1 to polls, so poll until both
# callbacks have run; a callback left queued past this test would
# fire later through a pointer into this frame
while crossThreadCallSoonCount < 2:
poll()
check: crossThreadCallSoonCount == 2
joinThreads(thread)