<p>Corey Farrell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/8186">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">json: Add conditionals to avoid locking if Jansson is thread safe.<br><br>Jansson is thread safe for all read-only functions and reference<br>counting starting v2.11.  This allows simplification of our code and<br>removal of locking around reference counting and dumping.<br><br>Change-Id: Id985cb3ffa6681f9ac765642e20fcd187bd4aeee<br>---<br>M main/json.c<br>1 file changed, 41 insertions(+), 5 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/86/8186/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/main/json.c b/main/json.c<br>index 057a262..1fe2abe 100644<br>--- a/main/json.c<br>+++ b/main/json.c<br>@@ -46,6 +46,21 @@<br> #include <jansson.h><br> #include <time.h><br> <br>+#if defined(JANSSON_THREAD_SAFE_REFCOUNT)<br>+void *ast_json_malloc(size_t size)<br>+{<br>+       return ast_std_malloc(size);<br>+}<br>+<br>+void ast_json_free(void *p)<br>+{<br>+        ast_std_free(p);<br>+}<br>+<br>+/* No need to lock since jansson is thread safe. */<br>+#define SCOPED_JSON_LOCK(json)<br>+<br>+#else<br> /*! \brief Magic number, for safety checks. */<br> #define JSON_MAGIC 0x1541992<br> <br>@@ -189,6 +204,7 @@<br> <br>  AST_LIST_INSERT_HEAD(free_list, mem, list);<br> }<br>+#endif<br> <br> void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*))<br> {<br>@@ -202,7 +218,7 @@<br> <br> struct ast_json *ast_json_ref(struct ast_json *json)<br> {<br>-       /* Jansson refcounting is non-atomic; lock it. */<br>+    /* If Jansson refcounting is non-atomic; lock it. */<br>  SCOPED_JSON_LOCK(json);<br>       json_incref((json_t *)json);<br>  return json;<br>@@ -210,6 +226,9 @@<br> <br> void ast_json_unref(struct ast_json *json)<br> {<br>+#if defined(JANSSON_THREAD_SAFE_REFCOUNT)<br>+    json_decref((json_t *) json);<br>+#else<br>         struct json_mem_list *free_list;<br>      struct json_mem *mem;<br> <br>@@ -234,6 +253,7 @@<br>         while ((mem = AST_LIST_REMOVE_HEAD(free_list, list))) {<br>               json_mem_free(mem);<br>   }<br>+#endif<br> }<br> <br> enum ast_json_type ast_json_typeof(const struct ast_json *json)<br>@@ -666,7 +686,11 @@<br> {<br>         /* Jansson's json_dump*, even though it's a read operation, isn't<br>          * thread safe for concurrent reads. Locking is necessary.<br>-    * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */<br>+        * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety.<br>+   *<br>+    * This comment does not apply when JANSSON_THREAD_SAFE_REFCOUNT is defined,<br>+  * in that case SCOPED_JSON_LOCK is a no-op.<br>+  */<br>   SCOPED_JSON_LOCK(root);<br>       return json_dumps((json_t *)root, dump_flags(format));<br> }<br>@@ -706,7 +730,11 @@<br> {<br>  /* Jansson's json_dump*, even though it's a read operation, isn't<br>          * thread safe for concurrent reads. Locking is necessary.<br>-    * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */<br>+        * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety.<br>+   *<br>+    * This comment does not apply when JANSSON_THREAD_SAFE_REFCOUNT is defined,<br>+  * in that case SCOPED_JSON_LOCK is a no-op.<br>+  */<br>   SCOPED_JSON_LOCK(root);<br>       return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags(format));<br> }<br>@@ -716,7 +744,11 @@<br> {<br>   /* Jansson's json_dump*, even though it's a read operation, isn't<br>          * thread safe for concurrent reads. Locking is necessary.<br>-    * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */<br>+        * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety.<br>+   *<br>+    * This comment does not apply when JANSSON_THREAD_SAFE_REFCOUNT is defined,<br>+  * in that case SCOPED_JSON_LOCK is a no-op.<br>+  */<br>   SCOPED_JSON_LOCK(root);<br>       if (!root || !output) {<br>               return -1;<br>@@ -727,7 +759,11 @@<br> {<br>  /* Jansson's json_dump*, even though it's a read operation, isn't<br>          * thread safe for concurrent reads. Locking is necessary.<br>-    * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */<br>+        * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety.<br>+   *<br>+    * This comment does not apply when JANSSON_THREAD_SAFE_REFCOUNT is defined,<br>+  * in that case SCOPED_JSON_LOCK is a no-op.<br>+  */<br>   SCOPED_JSON_LOCK(root);<br>       if (!root || !path) {<br>                 return -1;<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/8186">change 8186</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/8186"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Id985cb3ffa6681f9ac765642e20fcd187bd4aeee </div>
<div style="display:none"> Gerrit-Change-Number: 8186 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>