Start to test thread-safety of global error object TODO: Only run these tests for the C bindings
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/c5e90d66 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/c5e90d66 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/c5e90d66 Branch: refs/heads/thread_safe_errors Commit: c5e90d666beac5156d1c7007bb14a7512fb3b643 Parents: 57b57df Author: Nick Wellnhofer <[email protected]> Authored: Wed Dec 24 18:08:09 2014 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Wed Dec 24 18:17:51 2014 +0100 ---------------------------------------------------------------------- runtime/common/charmonizer.c | 4 +- runtime/common/charmonizer.main | 4 +- runtime/core/Clownfish/Test/TestErr.c | 61 +++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c5e90d66/runtime/common/charmonizer.c ---------------------------------------------------------------------- diff --git a/runtime/common/charmonizer.c b/runtime/common/charmonizer.c index 1d0fe07..6875a38 100644 --- a/runtime/common/charmonizer.c +++ b/runtime/common/charmonizer.c @@ -8273,8 +8273,8 @@ S_need_libpthread(chaz_CLI *cli) { "#include <pthread.h>\n" "\n" "int main() {\n" - " pthread_key_t key;\n" - " pthread_key_create(&key, NULL);\n" + " pthread_create(0, 0, 0, 0);\n" + " pthread_key_create(0, 0);\n" " return 0;\n" "}\n"; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c5e90d66/runtime/common/charmonizer.main ---------------------------------------------------------------------- diff --git a/runtime/common/charmonizer.main b/runtime/common/charmonizer.main index 207599a..83981af 100644 --- a/runtime/common/charmonizer.main +++ b/runtime/common/charmonizer.main @@ -575,8 +575,8 @@ S_need_libpthread(chaz_CLI *cli) { "#include <pthread.h>\n" "\n" "int main() {\n" - " pthread_key_t key;\n" - " pthread_key_create(&key, NULL);\n" + " pthread_create(0, 0, 0, 0);\n" + " pthread_key_create(0, 0);\n" " return 0;\n" "}\n"; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c5e90d66/runtime/core/Clownfish/Test/TestErr.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestErr.c b/runtime/core/Clownfish/Test/TestErr.c index b31313d..2cb54dd 100644 --- a/runtime/core/Clownfish/Test/TestErr.c +++ b/runtime/core/Clownfish/Test/TestErr.c @@ -17,6 +17,8 @@ #define CFISH_USE_SHORT_NAMES #define TESTCFISH_USE_SHORT_NAMES +#include "charmony.h" + #include "Clownfish/Test/TestErr.h" #include "Clownfish/String.h" @@ -41,10 +43,67 @@ test_To_String(TestBatchRunner *runner) { DECREF(error); } +/**************************** No thread support ****************************/ +#ifdef CFISH_NOTHREADS + +static void +test_threads(TestBatchRunner *runner) { + SKIP(runner, 4, "no thread support"); +} + +/********************************** Windows ********************************/ +#elif defined(CHY_HAS_WINDOWS_H) + +static void +test_threads(TestBatchRunner *runner) { + SKIP(runner, 4, "TODO: test threads on Windows"); +} + +/******************************** pthreads *********************************/ +#elif defined(CHY_HAS_PTHREAD_H) + +#include <pthread.h> + +static void* +S_err_thread(void *arg) { + TestBatchRunner *runner = (TestBatchRunner*)arg; + + TEST_TRUE(runner, Err_get_error() == NULL, + "global error in thread initialized to null"); + + Err_set_error(Err_new(Str_newf("thread"))); + + return NULL; +} + +static void +test_threads(TestBatchRunner *runner) { + Err_set_error(Err_new(Str_newf("main"))); + + int err; + pthread_t thread; + err = pthread_create(&thread, NULL, S_err_thread, runner); + TEST_INT_EQ(runner, err, 0, "pthread_create succeeds"); + err = pthread_join(thread, NULL); + TEST_INT_EQ(runner, err, 0, "pthread_join succeeds"); + + String *mess = Err_Get_Mess(Err_get_error()); + TEST_TRUE(runner, Str_Equals_Utf8(mess, "main", 4), + "thread doesn't clobber global error"); +} + +/****************** No support for thread-local storage ********************/ +#else + +#error "No support for thread-local storage." + +#endif + void TestErr_Run_IMP(TestErr *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 1); + TestBatchRunner_Plan(runner, (TestBatch*)self, 5); test_To_String(runner); + test_threads(runner); }
