Skip to content

Commit

Permalink
Implement async callback test on Windows and improve async specs
Browse files Browse the repository at this point in the history
  • Loading branch information
larskanis committed Feb 28, 2021
1 parent 6d14c0a commit c48dfe7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
21 changes: 13 additions & 8 deletions spec/ffi/async_callback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@ module LibTest
skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
v = 0xdeadbeef
called = false
cb = Proc.new {|i| v = i; called = true }
cb = Proc.new {|i| v = i; called = Thread.current }
LibTest.testAsyncCallback(cb, 0x7fffffff)
expect(called).to be true
expect(called).to be_kind_of(Thread)
expect(called).to_not eq(Thread.current)
expect(v).to eq(0x7fffffff)
end

it "called a second time" do
skip "not yet supported on TruffleRuby" if RUBY_ENGINE == "truffleruby"
v = 0xdeadbeef
called = false
cb = Proc.new {|i| v = i; called = true }
LibTest.testAsyncCallback(cb, 0x7fffffff)
expect(called).to be true
expect(v).to eq(0x7fffffff)
v = 1
th1 = th2 = false
LibTest.testAsyncCallback(2) { |i| v += i; th1 = Thread.current }
LibTest.testAsyncCallback(3) { |i| v += i; th2 = Thread.current }
expect(th1).to be_kind_of(Thread)
expect(th2).to be_kind_of(Thread)
expect(th1).to_not eq(Thread.current)
expect(th2).to_not eq(Thread.current)
expect(th1).to_not eq(th2)
expect(v).to eq(6)
end
end
16 changes: 13 additions & 3 deletions spec/ffi/fixtures/FunctionTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#ifdef _WIN32
#include <windows.h>
#include <process.h>
#endif

#ifndef _WIN32
Expand Down Expand Up @@ -115,17 +116,26 @@ static void* asyncThreadCall(void *data)
return NULL;
}

#ifdef _WIN32
static void
asyncThreadCall_win32(void *arg)
{
asyncThreadCall(arg);
}
#endif

void testAsyncCallback(void (*fn)(int), int value)
{
#ifndef _WIN32
pthread_t t;
struct async_data d;
d.fn = fn;
d.value = value;
#ifndef _WIN32
pthread_t t;
pthread_create(&t, NULL, asyncThreadCall, &d);
pthread_join(t, NULL);
#else
(*fn)(value);
HANDLE hThread = (HANDLE) _beginthread(asyncThreadCall_win32, 0, &d);
WaitForSingleObject(hThread, INFINITE);
#endif
}

Expand Down

0 comments on commit c48dfe7

Please sign in to comment.