You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2014/12/24 18:18:45 UTC

[1/3] lucy-clownfish git commit: Initialize error globals to zero

Repository: lucy-clownfish
Updated Branches:
  refs/heads/thread_safe_errors 78f6a18a2 -> c5e90d666


Initialize error globals to zero


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

Branch: refs/heads/thread_safe_errors
Commit: 46eb7151fb628e1b69023315096b0702ec8bbd54
Parents: 78f6a18
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 24 18:06:39 2014 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 24 18:06:39 2014 +0100

----------------------------------------------------------------------
 runtime/c/src/Clownfish/Err.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/46eb7151/runtime/c/src/Clownfish/Err.c
----------------------------------------------------------------------
diff --git a/runtime/c/src/Clownfish/Err.c b/runtime/c/src/Clownfish/Err.c
index a6e5090..69b9b79 100644
--- a/runtime/c/src/Clownfish/Err.c
+++ b/runtime/c/src/Clownfish/Err.c
@@ -70,7 +70,7 @@ S_get_globals() {
         = (cfish_ErrGlobals*)TlsGetValue(err_globals_tls_index);
 
     if (!globals) {
-        globals = (cfish_ErrGlobals*)MALLOCATE(sizeof(cfish_ErrGlobals));
+        globals = (cfish_ErrGlobals*)CALLOCATE(1, sizeof(cfish_ErrGlobals));
         if (!TlsSetValue(err_globals_tls_index, globals)) {
             fprintf(stderr, "TlsSetValue failed: %d\n", GetLastError());
             abort();
@@ -115,7 +115,7 @@ S_get_globals() {
         = (cfish_ErrGlobals*)pthread_getspecific(err_globals_key);
 
     if (!globals) {
-        globals = (cfish_ErrGlobals*)MALLOCATE(sizeof(cfish_ErrGlobals));
+        globals = (cfish_ErrGlobals*)CALLOCATE(1, sizeof(cfish_ErrGlobals));
         int error = pthread_setspecific(err_globals_key, globals);
         if (error) {
             fprintf(stderr, "pthread_setspecific failed: %d\n", error);


[2/3] lucy-clownfish git commit: Decref global error on thread exit

Posted by nw...@apache.org.
Decref global error on thread exit


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

Branch: refs/heads/thread_safe_errors
Commit: 57b57df51ba93f2e81199f2f8848cecb0afa6b79
Parents: 46eb715
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 24 18:07:36 2014 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 24 18:07:36 2014 +0100

----------------------------------------------------------------------
 runtime/c/src/Clownfish/Err.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/57b57df5/runtime/c/src/Clownfish/Err.c
----------------------------------------------------------------------
diff --git a/runtime/c/src/Clownfish/Err.c b/runtime/c/src/Clownfish/Err.c
index 69b9b79..ec588db 100644
--- a/runtime/c/src/Clownfish/Err.c
+++ b/runtime/c/src/Clownfish/Err.c
@@ -83,8 +83,13 @@ S_get_globals() {
 BOOL WINAPI
 DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved) {
     if (reason == DLL_THREAD_DETACH) {
-        void *globals = TlsGetValue(err_globals_tls_index);
-        FREEMEM(globals);
+        cfish_ErrGlobals *globals
+            = (cfish_ErrGlobals*)TlsGetValue(err_globals_tls_index);
+
+        if (globals) {
+            DECREF(globals->current_error);
+            FREEMEM(globals);
+        }
     }
 
     return TRUE;
@@ -127,7 +132,9 @@ S_get_globals() {
 }
 
 static void
-S_destroy_globals(void *globals) {
+S_destroy_globals(void *arg) {
+    cfish_ErrGlobals *globals = (cfish_ErrGlobals*)arg;
+    DECREF(globals->current_error);
     FREEMEM(globals);
 }
 


[3/3] lucy-clownfish git commit: Start to test thread-safety of global error object

Posted by nw...@apache.org.
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 <we...@aevum.de>
Authored: Wed Dec 24 18:08:09 2014 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
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);
 }