mirror of
https://github.com/json-c/json-c.git
synced 2026-04-05 13:29:06 +08:00
reference increment and decrement is now atomic (when using a GCC compatible compiler), which allows passing json objects between threads
This commit is contained in:
@@ -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)
|
extern struct json_object* json_object_get(struct json_object *jso)
|
||||||
{
|
{
|
||||||
if (jso)
|
if (!jso) return jso;
|
||||||
jso->_ref_count++;
|
#if defined __GNUC__
|
||||||
|
__sync_add_and_fetch(&jso->_ref_count, 1);
|
||||||
|
#else
|
||||||
|
++jso->_ref_count;
|
||||||
|
#endif
|
||||||
return jso;
|
return jso;
|
||||||
}
|
}
|
||||||
|
|
||||||
int json_object_put(struct json_object *jso)
|
int json_object_put(struct json_object *jso)
|
||||||
{
|
{
|
||||||
if(jso)
|
if(!jso) return 0;
|
||||||
{
|
|
||||||
jso->_ref_count--;
|
#if defined __GNUC__
|
||||||
if(!jso->_ref_count)
|
if (__sync_fetch_and_sub(&jso->_ref_count, 1) > 0) return 0;
|
||||||
{
|
#else
|
||||||
if (jso->_user_delete)
|
if (--jso->_ref_count > 0) return 0;
|
||||||
jso->_user_delete(jso, jso->_userdata);
|
#endif
|
||||||
jso->_delete(jso);
|
|
||||||
return 1;
|
if (jso->_user_delete)
|
||||||
}
|
jso->_user_delete(jso, jso->_userdata);
|
||||||
}
|
jso->_delete(jso);
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user