Repository: lucy-clownfish
Updated Branches:
  refs/heads/thread_safe_errors [created] 78f6a18a2


Store global error variables in a single struct


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

Branch: refs/heads/thread_safe_errors
Commit: 98282c907a2d7224363883a8ac7917e6ed81d506
Parents: 55d99f5
Author: Nick Wellnhofer <[email protected]>
Authored: Thu Dec 4 19:34:07 2014 +0100
Committer: Nick Wellnhofer <[email protected]>
Committed: Thu Dec 4 21:56:18 2014 +0100

----------------------------------------------------------------------
 runtime/c/src/Clownfish/Err.c | 45 +++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/98282c90/runtime/c/src/Clownfish/Err.c
----------------------------------------------------------------------
diff --git a/runtime/c/src/Clownfish/Err.c b/runtime/c/src/Clownfish/Err.c
index eac107f..d08b16f 100644
--- a/runtime/c/src/Clownfish/Err.c
+++ b/runtime/c/src/Clownfish/Err.c
@@ -28,10 +28,19 @@
 #include "Clownfish/Util/Memory.h"
 #include "Clownfish/Class.h"
 
+typedef struct {
+    Err *current_error;
+    Err *thrown_error;
+    jmp_buf *current_env;
+} cfish_ErrGlobals;
+
 /* TODO: Thread safety */
-static Err *current_error;
-static Err *thrown_error;
-static jmp_buf  *current_env;
+static cfish_ErrGlobals err_globals;
+
+static cfish_ErrGlobals*
+S_get_globals() {
+    return &err_globals;
+}
 
 void
 Err_init_class(void) {
@@ -39,22 +48,26 @@ Err_init_class(void) {
 
 Err*
 Err_get_error() {
-    return current_error;
+    return S_get_globals()->current_error;
 }
 
 void
 Err_set_error(Err *error) {
-    if (current_error) {
-        DECREF(current_error);
+    cfish_ErrGlobals *globals = S_get_globals();
+
+    if (globals->current_error) {
+        DECREF(globals->current_error);
     }
-    current_error = error;
+    globals->current_error = error;
 }
 
 void
 Err_do_throw(Err *error) {
-    if (current_env) {
-        thrown_error = error;
-        longjmp(*current_env, 1);
+    cfish_ErrGlobals *globals = S_get_globals();
+
+    if (globals->current_env) {
+        globals->thrown_error = error;
+        longjmp(*globals->current_env, 1);
     }
     else {
         String *message = Err_Get_Mess(error);
@@ -89,18 +102,20 @@ Err_warn_mess(String *message) {
 
 Err*
 Err_trap(Err_Attempt_t routine, void *context) {
+    cfish_ErrGlobals *globals = S_get_globals();
+
     jmp_buf  env;
-    jmp_buf *prev_env = current_env;
-    current_env = &env;
+    jmp_buf *prev_env = globals->current_env;
+    globals->current_env = &env;
 
     if (!setjmp(env)) {
         routine(context);
     }
 
-    current_env = prev_env;
+    globals->current_env = prev_env;
 
-    Err *error = thrown_error;
-    thrown_error = NULL;
+    Err *error = globals->thrown_error;
+    globals->thrown_error = NULL;
     return error;
 }
 

Reply via email to