forked from jakibaki/beatsaber-hook
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix il2cpp thread util to be more or less equivalent to std::thread, …
…and add tests
- Loading branch information
1 parent
351982b
commit 7f8c281
Showing
4 changed files
with
154 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ function(setup_target target add_test) | |
TEST_LIST | ||
TEST_STRING | ||
TEST_HOOK | ||
TEST_THREAD | ||
) | ||
endif() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#ifdef NO_TEST | ||
#if defined(TEST_CALLBACKS) || defined(TEST_SAFEPTR) || defined(TEST_BYREF) || defined(TEST_ARRAY) || defined(TEST_LIST) || defined(TEST_STRING) || defined(TEST_HOOK) | ||
#if defined(TEST_CALLBACKS) || defined(TEST_SAFEPTR) || defined(TEST_BYREF) || defined(TEST_ARRAY) || defined(TEST_LIST) || defined(TEST_STRING) || defined(TEST_HOOK) || defined(TEST_THREAD) | ||
#error "tests are being built into the release for bs hook!" | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#ifdef TEST_THREAD | ||
#include "utils/il2cpp-utils.hpp" | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunused-variable" | ||
#pragma clang diagnostic ignored "-Wunused-parameter" | ||
|
||
static void thread_method() {} | ||
static void thread_method_value(int v) {} | ||
static void thread_method_lvalue(int& v) {} | ||
static void thread_method_rvalue(int&& v) {} | ||
|
||
struct ThreadTest { | ||
void method() {} | ||
|
||
void method_value(int v) {} | ||
void method_lvalue(int& v) {} | ||
void method_rvalue(int&& v) {} | ||
|
||
// can we do all the same things from an instance of a struct | ||
void test_thread(); | ||
|
||
void some_call() {}; | ||
}; | ||
|
||
// test both il2cpp aware thread and std::thread for equivalence | ||
#define IL2CPP_THREAD_TEST(...) \ | ||
il2cpp_utils::il2cpp_aware_thread(__VA_ARGS__).join(); \ | ||
std::thread(__VA_ARGS__).join() | ||
|
||
void test_thread() { | ||
// can we make a 0 arg lambda thread? | ||
IL2CPP_THREAD_TEST([](){}); | ||
|
||
IL2CPP_THREAD_TEST(&thread_method); | ||
|
||
int v = 0; | ||
|
||
// can we capture? | ||
IL2CPP_THREAD_TEST([=](){ int b = v + 1; }); | ||
IL2CPP_THREAD_TEST([&](){ v += 1;}); | ||
IL2CPP_THREAD_TEST([v](){ int b = v + 1; }); | ||
IL2CPP_THREAD_TEST([value = v](){ int b = value + 1; }); | ||
IL2CPP_THREAD_TEST([value = &v](){ *value += 1; }); | ||
IL2CPP_THREAD_TEST([&v](){ v += 1; }); | ||
|
||
// can we make a lambda that accepts an integer from lvalue & rvalue? | ||
IL2CPP_THREAD_TEST([](int v){}, v); | ||
IL2CPP_THREAD_TEST([](int v){}, 10); | ||
|
||
// pass rvalue into lvalue | ||
// IL2CPP_THREAD_TEST([]( int& v){}, 10); // should not work | ||
// pass rvalue into rvalue | ||
IL2CPP_THREAD_TEST([](int&& v){}, 10); // should work | ||
// pass lvalue into lvalue | ||
// IL2CPP_THREAD_TEST([]( int& v){}, v); // should work | ||
// pass lvalue into rvalue | ||
IL2CPP_THREAD_TEST([](int&& v){}, v); // should work | ||
|
||
// the same for a method? | ||
IL2CPP_THREAD_TEST(&thread_method_value, v); // should work | ||
IL2CPP_THREAD_TEST(&thread_method_value, 10); // should work | ||
|
||
// can we pass the value as appropriate? | ||
// lvalue into lvalue | ||
// IL2CPP_THREAD_TEST(&thread_method_lvalue, v); // should not work | ||
// lvalue into rvalue | ||
IL2CPP_THREAD_TEST(&thread_method_rvalue, v); // should work | ||
// rvalue into lvalue | ||
// IL2CPP_THREAD_TEST(&thread_method_lvalue, 10); // should not work | ||
// rvalue into rvalue | ||
IL2CPP_THREAD_TEST(&thread_method_rvalue, 10); // should work | ||
|
||
ThreadTest tt; | ||
tt.test_thread(); | ||
} | ||
|
||
void ThreadTest::test_thread() { | ||
// can we make a 0 arg lambda thread? | ||
IL2CPP_THREAD_TEST([](){}); | ||
IL2CPP_THREAD_TEST(&ThreadTest::method, this); | ||
|
||
int v = 0; | ||
|
||
// can we capture? | ||
IL2CPP_THREAD_TEST([this](){ some_call(); }); | ||
IL2CPP_THREAD_TEST([=](){ int b = v + 1; }); | ||
IL2CPP_THREAD_TEST([&](){ some_call(); }); | ||
IL2CPP_THREAD_TEST([v](){ int b = v + 1; }); | ||
IL2CPP_THREAD_TEST([value = v](){ int b = value + 1; }); | ||
IL2CPP_THREAD_TEST([value = &v](){ *value += 1; }); | ||
IL2CPP_THREAD_TEST([&v](){ v += 1; }); | ||
|
||
// can we make a lambda that accepts an integer from lvalue & rvalue? | ||
IL2CPP_THREAD_TEST([](ThreadTest* self, int v){}, this, v); | ||
IL2CPP_THREAD_TEST([](ThreadTest* self, int v){}, this, 10); | ||
|
||
// the same for a method? | ||
IL2CPP_THREAD_TEST(&ThreadTest::method_value, this, v); | ||
IL2CPP_THREAD_TEST(&ThreadTest::method_value, this, 10); | ||
|
||
// can we pass the value as appropriate? | ||
// IL2CPP_THREAD_TEST(&ThreadTest::method_lvalue, this, v); // should not work | ||
IL2CPP_THREAD_TEST(&ThreadTest::method_rvalue, this, 10); | ||
} | ||
|
||
#pragma clang diagnostic pop | ||
|
||
#endif |