Test thread-safety of global error object

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/ca15901d
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/ca15901d
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/ca15901d

Branch: refs/heads/master
Commit: ca15901d7752abe03f865696c62c3be345b72bbf
Parents: cd4f31b
Author: Nick Wellnhofer <[email protected]>
Authored: Wed Dec 24 18:08:09 2014 +0100
Committer: Nick Wellnhofer <[email protected]>
Committed: Sat Dec 27 16:10:02 2014 +0100

----------------------------------------------------------------------
 runtime/c/src/Clownfish/Test/TestThreads.c  | 113 +++++++++++++++++++++++
 runtime/core/Clownfish/Test.c               |   2 +
 runtime/core/Clownfish/Test/TestThreads.c   |  28 ++++++
 runtime/core/Clownfish/Test/TestThreads.cfh |  28 ++++++
 runtime/perl/t/core/050-threads.t           |  23 +++++
 runtime/perl/xs/XSBind.c                    |  10 ++
 6 files changed, 204 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ca15901d/runtime/c/src/Clownfish/Test/TestThreads.c
----------------------------------------------------------------------
diff --git a/runtime/c/src/Clownfish/Test/TestThreads.c 
b/runtime/c/src/Clownfish/Test/TestThreads.c
new file mode 100644
index 0000000..bf4a203
--- /dev/null
+++ b/runtime/c/src/Clownfish/Test/TestThreads.c
@@ -0,0 +1,113 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "charmony.h"
+
+#include "Clownfish/Test/TestThreads.h"
+
+#include "Clownfish/Err.h"
+#include "Clownfish/String.h"
+#include "Clownfish/TestHarness/TestBatchRunner.h"
+
+/**************************** 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)
+
+#include <windows.h>
+
+static DWORD
+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 0;
+}
+
+static void
+test_threads(TestBatchRunner *runner) {
+    Err_set_error(Err_new(Str_newf("main")));
+
+    HANDLE thread = CreateThread(NULL, 0, S_err_thread, runner, 0, NULL);
+    TEST_TRUE(runner, thread != NULL, "CreateThread succeeds");
+    DWORD event = WaitForSingleObject(thread, INFINITE);
+    TEST_INT_EQ(runner, event, WAIT_OBJECT_0, "WaitForSingleObject succeeds");
+    CloseHandle(thread);
+
+    String *mess = Err_Get_Mess(Err_get_error());
+    TEST_TRUE(runner, Str_Equals_Utf8(mess, "main", 4),
+              "thread doesn't clobber global error");
+}
+
+/******************************** 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
+TestThreads_Run_IMP(TestThreads *self, TestBatchRunner *runner) {
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 4);
+    test_threads(runner);
+}
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ca15901d/runtime/core/Clownfish/Test.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test.c b/runtime/core/Clownfish/Test.c
index 2a1e445..606e8fe 100644
--- a/runtime/core/Clownfish/Test.c
+++ b/runtime/core/Clownfish/Test.c
@@ -31,6 +31,7 @@
 #include "Clownfish/Test/TestLockFreeRegistry.h"
 #include "Clownfish/Test/TestNum.h"
 #include "Clownfish/Test/TestObj.h"
+#include "Clownfish/Test/TestThreads.h"
 #include "Clownfish/Test/TestVArray.h"
 #include "Clownfish/Test/Util/TestAtomic.h"
 #include "Clownfish/Test/Util/TestMemory.h"
@@ -55,6 +56,7 @@ Test_create_test_suite() {
     TestSuite_Add_Batch(suite, (TestBatch*)TestAtomic_new());
     TestSuite_Add_Batch(suite, (TestBatch*)TestLFReg_new());
     TestSuite_Add_Batch(suite, (TestBatch*)TestMemory_new());
+    TestSuite_Add_Batch(suite, (TestBatch*)TestThreads_new());
 
     return suite;
 }

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ca15901d/runtime/core/Clownfish/Test/TestThreads.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestThreads.c 
b/runtime/core/Clownfish/Test/TestThreads.c
new file mode 100644
index 0000000..054f9f0
--- /dev/null
+++ b/runtime/core/Clownfish/Test/TestThreads.c
@@ -0,0 +1,28 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "Clownfish/Test/TestThreads.h"
+
+#include "Clownfish/Class.h"
+
+TestThreads*
+TestThreads_new() {
+    return (TestThreads*)Class_Make_Obj(TESTTHREADS);
+}
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ca15901d/runtime/core/Clownfish/Test/TestThreads.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestThreads.cfh 
b/runtime/core/Clownfish/Test/TestThreads.cfh
new file mode 100644
index 0000000..8406ab5
--- /dev/null
+++ b/runtime/core/Clownfish/Test/TestThreads.cfh
@@ -0,0 +1,28 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel TestClownfish;
+
+class Clownfish::Test::TestThreads
+    inherits Clownfish::TestHarness::TestBatch {
+
+    inert incremented TestThreads*
+    new();
+
+    void
+    Run(TestThreads *self, TestBatchRunner *runner);
+}
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ca15901d/runtime/perl/t/core/050-threads.t
----------------------------------------------------------------------
diff --git a/runtime/perl/t/core/050-threads.t 
b/runtime/perl/t/core/050-threads.t
new file mode 100644
index 0000000..48d636f
--- /dev/null
+++ b/runtime/perl/t/core/050-threads.t
@@ -0,0 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+use strict;
+use warnings;
+
+use Clownfish::Test;
+my $success = Clownfish::Test::run_tests("Clownfish::Test::TestThreads");
+
+exit($success ? 0 : 1);
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ca15901d/runtime/perl/xs/XSBind.c
----------------------------------------------------------------------
diff --git a/runtime/perl/xs/XSBind.c b/runtime/perl/xs/XSBind.c
index d7c5e88..de8b310 100644
--- a/runtime/perl/xs/XSBind.c
+++ b/runtime/perl/xs/XSBind.c
@@ -25,6 +25,8 @@
 #include "Clownfish/CharBuf.h"
 #include "Clownfish/LockFreeRegistry.h"
 #include "Clownfish/Method.h"
+#include "Clownfish/Test/TestThreads.h"
+#include "Clownfish/TestHarness/TestBatchRunner.h"
 #include "Clownfish/Util/StringHelper.h"
 #include "Clownfish/Util/NumberUtils.h"
 #include "Clownfish/Util/Memory.h"
@@ -959,3 +961,11 @@ CFISH_LFReg_To_Host_IMP(cfish_LockFreeRegistry *self) {
     return host_obj;
 }
 
+/*********************** Clownfish::Test::TestThreads ***********************/
+
+void
+TESTCFISH_TestThreads_Run_IMP(testcfish_TestThreads *self,
+                              cfish_TestBatchRunner *runner) {
+    CFISH_TestBatchRunner_Plan(runner, (cfish_TestBatch*)self, 0);
+}
+

Reply via email to