Skip to content

Commit c48dfe7

Browse files
committed
Implement async callback test on Windows and improve async specs
1 parent 6d14c0a commit c48dfe7

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

spec/ffi/async_callback_spec.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@ module LibTest
1919
skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
2020
v = 0xdeadbeef
2121
called = false
22-
cb = Proc.new {|i| v = i; called = true }
22+
cb = Proc.new {|i| v = i; called = Thread.current }
2323
LibTest.testAsyncCallback(cb, 0x7fffffff)
24-
expect(called).to be true
24+
expect(called).to be_kind_of(Thread)
25+
expect(called).to_not eq(Thread.current)
2526
expect(v).to eq(0x7fffffff)
2627
end
2728

2829
it "called a second time" do
2930
skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
30-
v = 0xdeadbeef
31-
called = false
32-
cb = Proc.new {|i| v = i; called = true }
33-
LibTest.testAsyncCallback(cb, 0x7fffffff)
34-
expect(called).to be true
35-
expect(v).to eq(0x7fffffff)
31+
v = 1
32+
th1 = th2 = false
33+
LibTest.testAsyncCallback(2) { |i| v += i; th1 = Thread.current }
34+
LibTest.testAsyncCallback(3) { |i| v += i; th2 = Thread.current }
35+
expect(th1).to be_kind_of(Thread)
36+
expect(th2).to be_kind_of(Thread)
37+
expect(th1).to_not eq(Thread.current)
38+
expect(th2).to_not eq(Thread.current)
39+
expect(th1).to_not eq(th2)
40+
expect(v).to eq(6)
3641
end
3742
end

spec/ffi/fixtures/FunctionTest.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#ifdef _WIN32
88
#include <windows.h>
9+
#include <process.h>
910
#endif
1011

1112
#ifndef _WIN32
@@ -115,17 +116,26 @@ static void* asyncThreadCall(void *data)
115116
return NULL;
116117
}
117118

119+
#ifdef _WIN32
120+
static void
121+
asyncThreadCall_win32(void *arg)
122+
{
123+
asyncThreadCall(arg);
124+
}
125+
#endif
126+
118127
void testAsyncCallback(void (*fn)(int), int value)
119128
{
120-
#ifndef _WIN32
121-
pthread_t t;
122129
struct async_data d;
123130
d.fn = fn;
124131
d.value = value;
132+
#ifndef _WIN32
133+
pthread_t t;
125134
pthread_create(&t, NULL, asyncThreadCall, &d);
126135
pthread_join(t, NULL);
127136
#else
128-
(*fn)(value);
137+
HANDLE hThread = (HANDLE) _beginthread(asyncThreadCall_win32, 0, &d);
138+
WaitForSingleObject(hThread, INFINITE);
129139
#endif
130140
}
131141

0 commit comments

Comments
 (0)