<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>You should export the new function also to kemi:</p>
    <p>  -
<a class="moz-txt-link-freetext" href="https://github.com/kamailio/kamailio/blob/71f6e6b1303a6daf80414c7217bdade5014c8eb0/modules/cfgutils/cfgutils.c#L1094">https://github.com/kamailio/kamailio/blob/71f6e6b1303a6daf80414c7217bdade5014c8eb0/modules/cfgutils/cfgutils.c#L1094</a></p>
    <p>Cheers,<br>
      Daniel<br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 10/06/16 14:08, Olle E. Johansson
      wrote:<br>
    </div>
    <blockquote cite="mid:E1bBLEV-0006kQ-Fw@www.kamailio.org"
      type="cite">
      <pre wrap="">Module: kamailio
Branch: master
Commit: 71f6e6b1303a6daf80414c7217bdade5014c8eb0
URL: <a class="moz-txt-link-freetext" href="https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0">https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0</a>

Author: Olle E. Johansson <a class="moz-txt-link-rfc2396E" href="mailto:oej@edvina.net"><oej@edvina.net></a>
Committer: Olle E. Johansson <a class="moz-txt-link-rfc2396E" href="mailto:oej@edvina.net"><oej@edvina.net></a>
Date: 2016-06-10T14:08:07+02:00

cfgutils Add "trylock" function

---

Modified: modules/cfgutils/cfgutils.c
Modified: modules/cfgutils/doc/cfgutils_admin.xml

---

Diff:  <a class="moz-txt-link-freetext" href="https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0.diff">https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0.diff</a>
Patch: <a class="moz-txt-link-freetext" href="https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0.patch">https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0.patch</a>

---

diff --git a/modules/cfgutils/cfgutils.c b/modules/cfgutils/cfgutils.c
index 082cfe5..8d8dc8e 100644
--- a/modules/cfgutils/cfgutils.c
+++ b/modules/cfgutils/cfgutils.c
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * Copyright (C) 2012 Edvina AB
  * Copyright (C) 2007 1&1 Internet AG
  * Copyright (C) 2007 BASIS AudioNet GmbH
@@ -106,6 +104,7 @@ static int is_gflag(struct sip_msg*, char *, char *);
 
 static int w_cfg_lock(struct sip_msg*, char *, char *);
 static int w_cfg_unlock(struct sip_msg*, char *, char *);
+static int w_cfg_trylock(struct sip_msg*, char *, char *);
 
 static struct mi_root* mi_set_prob(struct mi_root* cmd, void* param );
 static struct mi_root* mi_reset_prob(struct mi_root* cmd, void* param );
@@ -184,6 +183,8 @@ static cmd_export_t cmds[]={
                ANY_ROUTE},
        {"unlock",       (cmd_function)w_cfg_unlock,  1,   fixup_spve_null, 0,
                ANY_ROUTE},
+       {"trylock",       (cmd_function)w_cfg_trylock,  1,   fixup_spve_null, 0,
+               ANY_ROUTE},
        {"core_hash",    (cmd_function)w_core_hash, 3,   fixup_core_hash, 0,
                ANY_ROUTE},
        {"check_route_exists",    (cmd_function)w_check_route_exists, 1,   0, 0,
@@ -828,11 +829,28 @@ static int cfg_lock_helper(str *lkey, int mode)
 {
        unsigned int pos;
        pos = core_case_hash(lkey, 0, _cfg_lock_size);
+
        LM_DBG("cfg_lock mode %d on %u\n", mode, pos);
-       if(mode==0)
+
+       if(mode==0) {
+               /* Lock */
                lock_set_get(_cfg_lock_set, pos);
-       else
+       } else if (mode == 1) {
+               /* Unlock */
                lock_set_release(_cfg_lock_set, pos);
+       } else {
+               int res;
+               /* Trylock */
+               res = lock_set_try(_cfg_lock_set, pos);
+               if (res != 0) {
+                       LM_DBG("Failed to trylock \n");
+                       /* Failed to lock */
+                       return -1;
+               }
+               LM_DBG("Succeeded with trylock \n");
+               /* Succeeded in locking */
+               return 1;
+       }
        return 1;
 }
 
@@ -846,6 +864,11 @@ static int cfg_unlock(str *lkey)
        return cfg_lock_helper(lkey, 1);
 }
 
+static int cfg_trylock(str *lkey)
+{
+       return cfg_lock_helper(lkey, 2);
+}
+
 static int w_cfg_lock_wrapper(struct sip_msg *msg, gparam_p key, int mode)
 {
        str s;
@@ -871,6 +894,13 @@ static int w_cfg_unlock(struct sip_msg *msg, char *key, char *s2)
        return w_cfg_lock_wrapper(msg, (gparam_p)key, 1);
 }
 
+static int w_cfg_trylock(struct sip_msg *msg, char *key, char *s2)
+{
+       if(_cfg_lock_set==NULL || key==NULL)
+               return -1;
+       return w_cfg_lock_wrapper(msg, (gparam_p)key, 2);
+}
+
 /*! Check if a route block exists - only request routes
  */
 static int w_check_route_exists(struct sip_msg *msg, char *route)
diff --git a/modules/cfgutils/doc/cfgutils_admin.xml b/modules/cfgutils/doc/cfgutils_admin.xml
index 3e22fba..4b222e1 100644
--- a/modules/cfgutils/doc/cfgutils_admin.xml
+++ b/modules/cfgutils/doc/cfgutils_admin.xml
@@ -505,6 +505,31 @@ lock("$rU");
 </programlisting>
                </example>
        </section>
+       <section id="cfgutils.f.trylock">
+               <title><function moreinfo="none">trylock(key)</function></title>
+               <para>
+               Try to lock the key. If the lock can not be obtained (possibly already locked),
+               the function returns an error and script execution continues. 
+               </para>
+               <para>
+               <quote>key</quote> can be static string or string with PVs.
+               </para>
+               <para>
+               This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
+               ONREPLY_ROUTE, BRANCH_ROUTE and LOCAL_ROUTE.
+               </para>
+               <example>
+               <title><function moreinfo="none">trylock()</function> usage</title>
+               <programlisting format="linespecific">
+...
+if (trylock("$rU")) {
+       xlog("L_INFO", "Doing some cool stuff\n");
+       unlock("$rU");
+}
+...
+</programlisting>
+               </example>
+       </section>
        <section id="cfgutils.f.unlock">
                <title><function moreinfo="none">unlock(key)</function></title>
                <para>


_______________________________________________
sr-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:sr-dev@lists.sip-router.org">sr-dev@lists.sip-router.org</a>
<a class="moz-txt-link-freetext" href="http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev">http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Daniel-Constantin Mierla
<a class="moz-txt-link-freetext" href="http://www.asipto.com">http://www.asipto.com</a> - <a class="moz-txt-link-freetext" href="http://www.kamailio.org">http://www.kamailio.org</a>
<a class="moz-txt-link-freetext" href="http://twitter.com/#!/miconda">http://twitter.com/#!/miconda</a> - <a class="moz-txt-link-freetext" href="http://www.linkedin.com/in/miconda">http://www.linkedin.com/in/miconda</a></pre>
  </body>
</html>