]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
reference increment and decrement is now atomic (when using a GCC compatible compiler...
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>
Fri, 27 Nov 2015 15:49:32 +0000 (16:49 +0100)
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>
Fri, 27 Nov 2015 15:49:32 +0000 (16:49 +0100)
json_object.c

index 9ac22a30f08f4dd58cc315c7f396ff7c9f3d6e82..cad3137457ac233e562eadbb57e22a4c256d9505 100644 (file)
@@ -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;
 }