<p>Joshua Colp <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/8184">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved; Approved for Submit

</div><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 contrib/scripts/install_prereq<br>M main/json.c<br>2 files changed, 42 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/contrib/scripts/install_prereq b/contrib/scripts/install_prereq<br>index 367a9a9..ff792f1 100755<br>--- a/contrib/scripts/install_prereq<br>+++ b/contrib/scripts/install_prereq<br>@@ -56,7 +56,7 @@<br> <br> KVERS=`uname -r`<br> <br>-JANSSON_VER=2.10<br>+JANSSON_VER=2.11<br> <br> case "$1" in<br> test)<br>diff --git a/main/json.c b/main/json.c<br>index 56df7f4..7d7bd1d 100644<br>--- a/main/json.c<br>+++ b/main/json.c<br>@@ -44,6 +44,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_malloc(size);<br>+}<br>+<br>+void ast_json_free(void *p)<br>+{<br>+    ast_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>@@ -187,6 +202,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>@@ -200,7 +216,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>@@ -208,6 +224,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>@@ -232,6 +251,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>@@ -664,7 +684,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>@@ -704,7 +728,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>@@ -714,7 +742,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>@@ -725,7 +757,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/8184">change 8184</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/8184"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Id985cb3ffa6681f9ac765642e20fcd187bd4aeee </div>
<div style="display:none"> Gerrit-Change-Number: 8184 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: Alexander Traud <pabstraud@compuserve.com> </div>
<div style="display:none"> Gerrit-Reviewer: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Richard Mudgett <rmudgett@digium.com> </div>