diff options
author | Lars Kanis <lars@greiz-reinsdorf.de> | 2021-02-28 19:26:54 +0100 |
---|---|---|
committer | Lars Kanis <lars@greiz-reinsdorf.de> | 2021-02-28 19:26:54 +0100 |
commit | c48dfe79c772074d6afc9dfa85fbf8f58a27b860 (patch) | |
tree | 3c1249e0de7bd70a0fee01fa90fbe99f31c1f3e6 | |
parent | 6d14c0a9107c0d5febb3bf92a60d2581e768fa2f (diff) | |
download | ffi-c48dfe79c772074d6afc9dfa85fbf8f58a27b860.tar.gz |
Implement async callback test on Windows and improve async specs
-rw-r--r-- | spec/ffi/async_callback_spec.rb | 21 | ||||
-rw-r--r-- | spec/ffi/fixtures/FunctionTest.c | 16 |
2 files changed, 26 insertions, 11 deletions
diff --git a/spec/ffi/async_callback_spec.rb b/spec/ffi/async_callback_spec.rb index 3d24369..7783a8a 100644 --- a/spec/ffi/async_callback_spec.rb +++ b/spec/ffi/async_callback_spec.rb @@ -19,19 +19,24 @@ describe "async callback" do 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 diff --git a/spec/ffi/fixtures/FunctionTest.c b/spec/ffi/fixtures/FunctionTest.c index 1dd9185..3c3c726 100644 --- a/spec/ffi/fixtures/FunctionTest.c +++ b/spec/ffi/fixtures/FunctionTest.c @@ -6,6 +6,7 @@ #ifdef _WIN32 #include <windows.h> +#include <process.h> #endif #ifndef _WIN32 @@ -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 } |