[sr-dev] git:master:71f6e6b1: cfgutils Add "trylock" function

Olle E. Johansson oej at edvina.net
Fri Jun 10 14:08:15 CEST 2016


Module: kamailio
Branch: master
Commit: 71f6e6b1303a6daf80414c7217bdade5014c8eb0
URL: https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0

Author: Olle E. Johansson <oej at edvina.net>
Committer: Olle E. Johansson <oej at edvina.net>
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:  https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0.diff
Patch: https://github.com/kamailio/kamailio/commit/71f6e6b1303a6daf80414c7217bdade5014c8eb0.patch

---

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>




More information about the sr-dev mailing list