From 23ee243113ac47730c7ca900b35a1abbcb568743 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 27 Nov 2015 16:49:32 +0100 Subject: [PATCH 1/4] reference increment and decrement is now atomic (when using a GCC compatible compiler), which allows passing json objects between threads --- json_object.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/json_object.c b/json_object.c index 9ac22a3..cad3137 100644 --- a/json_object.c +++ b/json_object.c @@ -160,25 +160,29 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len) extern struct json_object* json_object_get(struct json_object *jso) { - if (jso) - jso->_ref_count++; + if (!jso) return jso; +#if defined __GNUC__ + __sync_add_and_fetch(&jso->_ref_count, 1); +#else + ++jso->_ref_count; +#endif return jso; } int json_object_put(struct json_object *jso) { - if(jso) - { - jso->_ref_count--; - if(!jso->_ref_count) - { - if (jso->_user_delete) - jso->_user_delete(jso, jso->_userdata); - jso->_delete(jso); - return 1; - } - } - return 0; + if(!jso) return 0; + +#if defined __GNUC__ + if (__sync_fetch_and_sub(&jso->_ref_count, 1) > 0) return 0; +#else + if (--jso->_ref_count > 0) return 0; +#endif + + if (jso->_user_delete) + jso->_user_delete(jso, jso->_userdata); + jso->_delete(jso); + return 1; } From 827f0fd8ef57b71dce7910325b6ce541bd6d0b50 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 27 Nov 2015 16:52:17 +0100 Subject: [PATCH 2/4] update indentation --- json_object.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/json_object.c b/json_object.c index cad3137..2b9bef7 100644 --- a/json_object.c +++ b/json_object.c @@ -161,11 +161,13 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len) extern struct json_object* json_object_get(struct json_object *jso) { if (!jso) return jso; + #if defined __GNUC__ __sync_add_and_fetch(&jso->_ref_count, 1); #else ++jso->_ref_count; #endif + return jso; } From 9d8536767970bb78abc2f0fc26e9ca8fdf35bd6b Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 27 Nov 2015 16:53:57 +0100 Subject: [PATCH 3/4] added tabs instead of spaces to be compatible with rest of code --- json_object.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/json_object.c b/json_object.c index 2b9bef7..eae0f8a 100644 --- a/json_object.c +++ b/json_object.c @@ -163,9 +163,9 @@ extern struct json_object* json_object_get(struct json_object *jso) if (!jso) return jso; #if defined __GNUC__ - __sync_add_and_fetch(&jso->_ref_count, 1); + __sync_add_and_fetch(&jso->_ref_count, 1); #else - ++jso->_ref_count; + ++jso->_ref_count; #endif return jso; @@ -176,15 +176,15 @@ int json_object_put(struct json_object *jso) if(!jso) return 0; #if defined __GNUC__ - if (__sync_fetch_and_sub(&jso->_ref_count, 1) > 0) return 0; + if (__sync_fetch_and_sub(&jso->_ref_count, 1) > 0) return 0; #else - if (--jso->_ref_count > 0) return 0; + if (--jso->_ref_count > 0) return 0; #endif - if (jso->_user_delete) - jso->_user_delete(jso, jso->_userdata); - jso->_delete(jso); - return 1; + if (jso->_user_delete) + jso->_user_delete(jso, jso->_userdata); + jso->_delete(jso); + return 1; } From 7e98ed93f4942a1baade1bade36a67c77b43ec85 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 28 Nov 2015 14:19:43 +0100 Subject: [PATCH 4/4] subtract first, then retrieve value --- json_object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_object.c b/json_object.c index eae0f8a..b7ca93b 100644 --- a/json_object.c +++ b/json_object.c @@ -176,7 +176,7 @@ int json_object_put(struct json_object *jso) if(!jso) return 0; #if defined __GNUC__ - if (__sync_fetch_and_sub(&jso->_ref_count, 1) > 0) return 0; + if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0; #else if (--jso->_ref_count > 0) return 0; #endif