Skip to content

Commit

Permalink
cfgutils Add "trylock" function
Browse files Browse the repository at this point in the history
  • Loading branch information
oej committed Jun 10, 2016
1 parent a7534a7 commit 71f6e6b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
38 changes: 34 additions & 4 deletions 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
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions modules/cfgutils/doc/cfgutils_admin.xml
Expand Up @@ -502,6 +502,31 @@ if(is_gflag("4"))
...
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>
Expand Down

0 comments on commit 71f6e6b

Please sign in to comment.