mirror of
https://github.com/json-c/json-c.git
synced 2026-03-27 17:09:06 +08:00
Explicitly check for GCC's atomic functions instead of depending on the __GNUC__ define.
Add a comment mentioning the limitation even though the _ref_count value is hanled atomically.
This commit is contained in:
@@ -165,7 +165,7 @@ extern struct json_object* json_object_get(struct json_object *jso)
|
||||
{
|
||||
if (!jso) return jso;
|
||||
|
||||
#if defined __GNUC__
|
||||
#ifdef HAVE_ATOMIC_BUILTINS
|
||||
__sync_add_and_fetch(&jso->_ref_count, 1);
|
||||
#else
|
||||
++jso->_ref_count;
|
||||
@@ -178,7 +178,13 @@ int json_object_put(struct json_object *jso)
|
||||
{
|
||||
if(!jso) return 0;
|
||||
|
||||
#if defined __GNUC__
|
||||
#ifdef HAVE_ATOMIC_BUILTINS
|
||||
/* Note: this only allow the refcount to remain correct
|
||||
* when multiple threads are adjusting it. It is still an error
|
||||
* for a thread to decrement the refcount if it doesn't "own" it,
|
||||
* as that can result in the thread that loses the race to 0
|
||||
* operating on an already-freed object.
|
||||
*/
|
||||
if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
|
||||
#else
|
||||
if (--jso->_ref_count > 0) return 0;
|
||||
|
||||
Reference in New Issue
Block a user