summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/asterisk/_private.h1
-rw-r--r--include/asterisk/named_locks.h105
-rw-r--r--include/asterisk/pbx.h46
-rw-r--r--main/asterisk.c5
-rw-r--r--main/named_locks.c142
-rw-r--r--main/pbx.c120
-rw-r--r--res/res_pjsip_registrar_expire.c4
-rw-r--r--tests/test_named_lock.c151
-rw-r--r--third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch30
9 files changed, 530 insertions, 74 deletions
diff --git a/include/asterisk/_private.h b/include/asterisk/_private.h
index 2148a2082..ebee96cc4 100644
--- a/include/asterisk/_private.h
+++ b/include/asterisk/_private.h
@@ -38,6 +38,7 @@ int dnsmgr_reload(void); /*!< Provided by dnsmgr.c */
void threadstorage_init(void); /*!< Provided by threadstorage.c */
int ast_device_state_engine_init(void); /*!< Provided by devicestate.c */
int astobj2_init(void); /*!< Provided by astobj2.c */
+int ast_named_locks_init(void); /*!< Provided by named_locks.c */
int ast_file_init(void); /*!< Provided by file.c */
int ast_features_init(void); /*!< Provided by features.c */
void ast_autoservice_init(void); /*!< Provided by autoservice.c */
diff --git a/include/asterisk/named_locks.h b/include/asterisk/named_locks.h
new file mode 100644
index 000000000..0fe07d992
--- /dev/null
+++ b/include/asterisk/named_locks.h
@@ -0,0 +1,105 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Named Locks
+ *
+ * \author George Joseph <george.joseph@fairview5.com>
+ */
+
+#ifndef INCLUDE_ASTERISK_NAMED_LOCKS_H_
+#define INCLUDE_ASTERISK_NAMED_LOCKS_H_
+
+#include "asterisk/astobj2.h"
+
+/*!
+ * \defgroup named_locks Named mutex and read-write locks
+ * @{
+ * \page NamedLocks Named mutex and read-write locks
+ * \since 13.9.0
+ *
+ * Locking some objects like sorcery objects can be tricky because the underlying
+ * ao2 object may not be the same for all callers. For instance, two threads that
+ * call ast_sorcery_retrieve_by_id on the same aor name might actually get 2 different
+ * ao2 objects if the underlying wizard had to rehydrate the aor from a database.
+ * Locking one ao2 object doesn't have any effect on the other even if those objects
+ * had locks in the first place
+ *
+ * Named locks allow access control by name. Now an aor named "1000" can be locked and
+ * any other thread attempting to lock the aor named "1000" will wait regardless of whether
+ * the underlying ao2 object is the same or not.
+ *
+ * To use a named lock:
+ * Call ast_named_lock_get with the appropriate keyspace and key.
+ * Use the standard ao2 lock/unlock functions as needed.
+ * Call ast_named_lock_put when you're finished with it.
+ */
+
+/*!
+ * \brief Which type of lock to request.
+ */
+enum ast_named_lock_type {
+ /*! Request a named mutex. */
+ AST_NAMED_LOCK_TYPE_MUTEX = AO2_ALLOC_OPT_LOCK_MUTEX,
+ /*! Request a named read/write lock. */
+ AST_NAMED_LOCK_TYPE_RWLOCK = AO2_ALLOC_OPT_LOCK_RWLOCK,
+};
+
+struct ast_named_lock;
+
+struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
+ enum ast_named_lock_type lock_type, const char *keyspace, const char *key);
+
+int __ast_named_lock_put(const char *filename, int lineno, const char *func,
+ struct ast_named_lock *lock);
+
+/*!
+ * \brief Geta named lock handle
+ * \since 13.9.0
+ *
+ * \param lock_type One of ast_named_lock_type
+ * \param keyspace
+ * \param key
+ * \retval A pointer to an ast_named_lock structure
+ * \retval NULL on error
+ *
+ * \note
+ * keyspace and key can be anything. For sorcery objects, keyspace could be the object type
+ * and key could be the object id.
+ */
+#define ast_named_lock_get(lock_type, keyspace, key) \
+ __ast_named_lock_get(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock_type, \
+ keyspace, key)
+
+/*!
+ * \brief Put a named lock handle away
+ * \since 13.9.0
+ *
+ * \param lock The pointer to the ast_named_lock structure returned by ast_named_lock_get
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+#define ast_named_lock_put(lock) \
+ __ast_named_lock_put(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock)
+
+/*!
+ * @}
+ */
+
+#endif /* INCLUDE_ASTERISK_NAMED_LOCKS_H_ */
diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index 599745878..d722e123f 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -573,70 +573,80 @@ int ast_hint_presence_state(struct ast_channel *c, const char *context, const ch
const char *ast_extension_state2str(int extension_state);
/*!
- * \brief Registers a state change callback with destructor.
+ * \brief Add watcher for extension states with destructor.
* \since 1.8.9
* \since 10.1.0
*
* \param context which context to look in
* \param exten which extension to get state
* \param change_cb callback to call if state changed
- * \param destroy_cb callback to call when registration destroyed.
- * \param data to pass to callback
+ * \param destroy_cb callback to call when the watcher is destroyed.
+ * \param data to pass to callbacks
+ *
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
*
* \note The change_cb is called if the state of an extension is changed.
*
- * \note The destroy_cb is called when the registration is
- * deleted so the registerer can release any associated
- * resources.
+ * \note The destroy_cb is called when the watcher is deleted so the
+ * watcher can release any associated resources.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add_destroy(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data);
/*!
- * \brief Registers an extended state change callback with destructor.
+ * \brief Add watcher for extended extension states with destructor.
* \since 11
*
* \param context which context to look in
* \param exten which extension to get state
* \param change_cb callback to call if state changed
- * \param destroy_cb callback to call when registration destroyed.
- * \param data to pass to callback
+ * \param destroy_cb callback to call when the watcher is destroyed.
+ * \param data to pass to callbacks
+ *
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
*
* \note The change_cb is called if the state of an extension is changed.
* The extended state is passed to the callback in the device_state_info
* member of ast_state_cb_info.
*
- * \note The destroy_cb is called when the registration is
- * deleted so the registerer can release any associated
- * resources.
+ * \note The destroy_cb is called when the watcher is deleted so the
+ * watcher can release any associated resources.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add_destroy_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data);
/*!
- * \brief Registers a state change callback
+ * \brief Add watcher for extension states.
*
* \param context which context to look in
* \param exten which extension to get state
* \param change_cb callback to call if state changed
* \param data to pass to callback
*
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
+ *
* \note The change_cb is called if the state of an extension is changed.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data);
/*!
- * \brief Registers an extended state change callback
+ * \brief Add watcher for extended extension states.
* \since 11
*
* \param context which context to look in
@@ -644,20 +654,24 @@ int ast_extension_state_add(const char *context, const char *exten,
* \param change_cb callback to call if state changed
* \param data to pass to callback
*
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
+ *
* \note The change_cb is called if the state of an extension is changed.
* The extended state is passed to the callback in the device_state_info
* member of ast_state_cb_info.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data);
/*!
- * \brief Deletes a registered state change callback by ID
+ * \brief Deletes a state change watcher by ID
*
- * \param id of the registered state callback to delete
+ * \param id of the state watcher to delete (0 for global watcher)
* \param change_cb callback to call if state changed (Used if id == 0 (global))
*
* \retval 0 success
diff --git a/main/asterisk.c b/main/asterisk.c
index 5901d579c..ea998d492 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -4391,6 +4391,11 @@ static void asterisk_daemon(int isroot, const char *runuser, const char *rungrou
exit(1);
}
+ if (ast_named_locks_init()) {
+ printf("Failed: ast_named_locks_init\n%s", term_quit());
+ exit(1);
+ }
+
if (ast_opt_console) {
if (el_hist == NULL || el == NULL)
ast_el_initialize();
diff --git a/main/named_locks.c b/main/named_locks.c
new file mode 100644
index 000000000..b977b553c
--- /dev/null
+++ b/main/named_locks.c
@@ -0,0 +1,142 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Named Locks
+ *
+ * \author George Joseph <george.joseph@fairview5.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_REGISTER_FILE()
+
+#include "asterisk/_private.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/named_locks.h"
+#include "asterisk/utils.h"
+
+struct ao2_container *named_locks;
+#define NAMED_LOCKS_BUCKETS 101
+
+struct ast_named_lock {
+ char key[0];
+};
+
+static int named_locks_hash(const void *obj, const int flags)
+{
+ const struct ast_named_lock *lock = obj;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_KEY:
+ return ast_str_hash(obj);
+ case OBJ_SEARCH_OBJECT:
+ return ast_str_hash(lock->key);
+ default:
+ /* Hash can only work on something with a full key. */
+ ast_assert(0);
+ return 0;
+ }
+}
+
+static int named_locks_cmp(void *obj_left, void *obj_right, int flags)
+{
+ const struct ast_named_lock *object_left = obj_left;
+ const struct ast_named_lock *object_right = obj_right;
+ const char *right_key = obj_right;
+ int cmp;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_OBJECT:
+ right_key = object_right->key;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(object_left->key, right_key);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(object_left->key, right_key, strlen(right_key));
+ break;
+ default:
+ cmp = 0;
+ break;
+ }
+
+ return cmp ? 0 : CMP_MATCH;
+}
+
+static void named_locks_shutdown(void)
+{
+ ao2_cleanup(named_locks);
+}
+
+int ast_named_locks_init(void)
+{
+ named_locks = ao2_container_alloc_hash(0, 0, NAMED_LOCKS_BUCKETS, named_locks_hash, NULL,
+ named_locks_cmp);
+ if (!named_locks) {
+ return -1;
+ }
+
+ ast_register_cleanup(named_locks_shutdown);
+
+ return 0;
+}
+
+struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
+ enum ast_named_lock_type lock_type, const char *keyspace, const char *key)
+{
+ struct ast_named_lock *lock = NULL;
+ int concat_key_buff_len = strlen(keyspace) + strlen(key) + 2;
+ char *concat_key = ast_alloca(concat_key_buff_len);
+
+ sprintf(concat_key, "%s-%s", keyspace, key); /* Safe */
+
+ ao2_lock(named_locks);
+ lock = ao2_find(named_locks, concat_key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
+ if (lock) {
+ ao2_unlock(named_locks);
+ ast_assert((ao2_options_get(lock) & AO2_ALLOC_OPT_LOCK_MASK) == lock_type);
+ return lock;
+ }
+
+ lock = ao2_alloc_options(sizeof(*lock) + concat_key_buff_len, NULL, lock_type);
+ if (lock) {
+ strcpy(lock->key, concat_key); /* Safe */
+ ao2_link_flags(named_locks, lock, OBJ_NOLOCK);
+ }
+ ao2_unlock(named_locks);
+
+ return lock;
+}
+
+int __ast_named_lock_put(const char *filename, int lineno, const char *func,
+ struct ast_named_lock *lock)
+{
+ if (!lock) {
+ return -1;
+ }
+
+ ao2_lock(named_locks);
+ if (ao2_ref(lock, -1) == 2) {
+ ao2_unlink_flags(named_locks, lock, OBJ_NOLOCK);
+ }
+ ao2_unlock(named_locks);
+
+ return 0;
+}
diff --git a/main/pbx.c b/main/pbx.c
index b166978a1..6b0069c06 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -3252,7 +3252,8 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
{
struct ao2_iterator cb_iter;
struct ast_state_cb *state_cb;
- int state, same_state;
+ int state;
+ int same_state;
struct ao2_container *device_state_info;
int first_extended_cb_call = 1;
char context_name[AST_MAX_CONTEXT];
@@ -3291,7 +3292,8 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
device_state_info = alloc_device_state_info();
state = ast_extension_state3(*hint_app, device_state_info);
- if ((same_state = state == hint->laststate) && (~state & AST_EXTENSION_RINGING)) {
+ same_state = state == hint->laststate;
+ if (same_state && (~state & AST_EXTENSION_RINGING)) {
ao2_cleanup(device_state_info);
return;
}
@@ -3300,17 +3302,19 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
hint->laststate = state; /* record we saw the change */
/* For general callbacks */
- cb_iter = ao2_iterator_init(statecbs, 0);
- for (; !same_state && (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
- execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_DEVICE,
- hint,
- NULL);
+ if (!same_state) {
+ cb_iter = ao2_iterator_init(statecbs, 0);
+ for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
+ execute_state_callback(state_cb->change_cb,
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_DEVICE,
+ hint,
+ NULL);
+ }
+ ao2_iterator_destroy(&cb_iter);
}
- ao2_iterator_destroy(&cb_iter);
/* For extension callbacks */
/* extended callbacks are called when the state changed or when AST_STATE_RINGING is
@@ -3325,12 +3329,12 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
}
if (state_cb->extended || !same_state) {
execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_DEVICE,
- hint,
- state_cb->extended ? device_state_info : NULL);
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_DEVICE,
+ hint,
+ state_cb->extended ? device_state_info : NULL);
}
}
ao2_iterator_destroy(&cb_iter);
@@ -3422,12 +3426,12 @@ static void presence_state_notify_callbacks(
cb_iter = ao2_iterator_init(statecbs, 0);
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_PRESENCE,
- hint,
- NULL);
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_PRESENCE,
+ hint,
+ NULL);
}
ao2_iterator_destroy(&cb_iter);
@@ -3435,12 +3439,12 @@ static void presence_state_notify_callbacks(
cb_iter = ao2_iterator_init(hint->callbacks, 0);
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_cleanup(state_cb)) {
execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_PRESENCE,
- hint,
- NULL);
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_PRESENCE,
+ hint,
+ NULL);
}
ao2_iterator_destroy(&cb_iter);
}
@@ -3460,27 +3464,32 @@ static int handle_hint_change_message_type(struct stasis_message *msg, enum ast_
hint = stasis_message_data(msg);
- if (reason == AST_HINT_UPDATE_DEVICE) {
+ switch (reason) {
+ case AST_HINT_UPDATE_DEVICE:
device_state_notify_callbacks(hint, &hint_app);
- } else if (reason == AST_HINT_UPDATE_PRESENCE) {
- char *presence_subtype = NULL;
- char *presence_message = NULL;
- int state;
+ break;
+ case AST_HINT_UPDATE_PRESENCE:
+ {
+ char *presence_subtype = NULL;
+ char *presence_message = NULL;
+ int state;
- state = extension_presence_state_helper(
- hint->exten, &presence_subtype, &presence_message);
+ state = extension_presence_state_helper(
+ hint->exten, &presence_subtype, &presence_message);
+ {
+ struct ast_presence_state_message presence_state = {
+ .state = state > 0 ? state : AST_PRESENCE_INVALID,
+ .subtype = presence_subtype,
+ .message = presence_message
+ };
- {
- struct ast_presence_state_message presence_state = {
- .state = state > 0 ? state : AST_PRESENCE_INVALID,
- .subtype = presence_subtype,
- .message = presence_message
- };
- presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
- }
+ presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
+ }
- ast_free(presence_subtype);
- ast_free(presence_message);
+ ast_free(presence_subtype);
+ ast_free(presence_message);
+ }
+ break;
}
ast_free(hint_app);
@@ -3655,28 +3664,24 @@ static int extension_state_add_destroy(const char *context, const char *exten,
return id;
}
-/*! \brief Add watcher for extension states with destructor */
int ast_extension_state_add_destroy(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
{
return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 0);
}
-/*! \brief Add watcher for extension states */
int ast_extension_state_add(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data)
{
return extension_state_add_destroy(context, exten, change_cb, NULL, data, 0);
}
-/*! \brief Add watcher for extended extension states with destructor */
int ast_extension_state_add_destroy_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
{
return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 1);
}
-/*! \brief Add watcher for extended extension states */
int ast_extension_state_add_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data)
{
@@ -3698,7 +3703,6 @@ static int find_hint_by_cb_id(void *obj, void *arg, int flags)
return 0;
}
-/*! \brief ast_extension_state_del: Remove a watcher from the callback list */
int ast_extension_state_del(int id, ast_state_cb_type change_cb)
{
struct ast_state_cb *p_cur;
@@ -3904,12 +3908,12 @@ static int ast_add_hint(struct ast_exten *e)
cb_iter = ao2_iterator_init(statecbs, 0);
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
execute_state_callback(state_cb->change_cb,
- ast_get_context_name(ast_get_extension_context(e)),
- ast_get_extension_name(e),
- state_cb->data,
- AST_HINT_UPDATE_DEVICE,
- hint_new,
- NULL);
+ ast_get_context_name(ast_get_extension_context(e)),
+ ast_get_extension_name(e),
+ state_cb->data,
+ AST_HINT_UPDATE_DEVICE,
+ hint_new,
+ NULL);
}
ao2_iterator_destroy(&cb_iter);
}
diff --git a/res/res_pjsip_registrar_expire.c b/res/res_pjsip_registrar_expire.c
index bdb86e50e..97f2ca1c0 100644
--- a/res/res_pjsip_registrar_expire.c
+++ b/res/res_pjsip_registrar_expire.c
@@ -91,6 +91,7 @@ static void expiration_global_loaded(const char *object_type)
} else {
if (check_thread != AST_PTHREADT_NULL) {
pthread_kill(check_thread, SIGURG);
+ pthread_join(check_thread, NULL);
check_thread = AST_PTHREADT_NULL;
ast_debug(3, "Interval = 0, shutting thread down\n");
}
@@ -105,7 +106,10 @@ static struct ast_sorcery_observer expiration_global_observer = {
static int unload_module(void)
{
if (check_thread != AST_PTHREADT_NULL) {
+ check_interval = 0;
pthread_kill(check_thread, SIGURG);
+ pthread_join(check_thread, NULL);
+
check_thread = AST_PTHREADT_NULL;
}
diff --git a/tests/test_named_lock.c b/tests/test_named_lock.c
new file mode 100644
index 000000000..9e383088e
--- /dev/null
+++ b/tests/test_named_lock.c
@@ -0,0 +1,151 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Named Lock unit tests
+ *
+ * \author George Joseph <george.joseph@fairview5.com>
+ *
+ */
+
+/*** MODULEINFO
+ <depend>TEST_FRAMEWORK</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+#include <signal.h>
+
+#include "asterisk/test.h"
+#include "asterisk/utils.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+#include "asterisk/named_locks.h"
+
+static void *lock_thread(void *data)
+{
+ struct ast_named_lock *lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", data);
+
+ if (!lock) {
+ return NULL;
+ }
+
+ ao2_lock(lock);
+ usleep(3000000);
+ ao2_unlock(lock);
+
+ ast_named_lock_put(lock);
+
+ return NULL;
+}
+
+AST_TEST_DEFINE(named_lock_test)
+{
+ enum ast_test_result_state res = AST_TEST_FAIL;
+ struct ast_named_lock *lock1 = NULL;
+ struct ast_named_lock *lock2 = NULL;
+ pthread_t thread1;
+ pthread_t thread2;
+ struct timeval start_time;
+ int64_t duration;
+
+ switch(cmd) {
+ case TEST_INIT:
+ info->name = "named_lock_test";
+ info->category = "/main/lock/";
+ info->summary = "Named Lock test";
+ info->description =
+ "Tests that named locks operate as expected";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_status_update(test, "This test should take about 3 seconds\n");
+
+ /* 2 locks/threads to make sure they're independent */
+ ast_pthread_create(&thread1, NULL, lock_thread, "lock_1");
+ ast_pthread_create(&thread2, NULL, lock_thread, "lock_2");
+
+ lock1 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_1");
+ ast_test_validate_cleanup(test, lock1 != NULL, res, fail);
+
+ lock2 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_2");
+ ast_test_validate_cleanup(test, lock2 != NULL, res, fail);
+
+ usleep(1000000);
+
+ /* These should both fail */
+ if (!ao2_trylock(lock1)) {
+ ast_test_status_update(test, "ao2_trylock on lock1 succeeded when it should have failed\n");
+ ao2_unlock(lock1);
+ goto fail;
+ }
+
+ if (!ao2_trylock(lock2)) {
+ ast_test_status_update(test, "ao2_trylock on lock2 succeeded when it should have failed\n");
+ ao2_unlock(lock2);
+ goto fail;
+ }
+
+ start_time = ast_tvnow();
+
+ /* These should both succeed eventually */
+ if (ao2_lock(lock1)) {
+ ast_test_status_update(test, "ao2_lock on lock1 failed\n");
+ goto fail;
+ }
+ ao2_unlock(lock1);
+
+ if (ao2_lock(lock2)) {
+ ast_test_status_update(test, "ao2_lock on lock2 failed\n");
+ goto fail;
+ }
+ ao2_unlock(lock2);
+
+ duration = ast_tvdiff_ms(ast_tvnow(), start_time);
+ ast_test_validate_cleanup(test, duration > 1500 && duration < 3500, res, fail);
+
+ res = AST_TEST_PASS;
+
+fail:
+
+ ast_named_lock_put(lock1);
+ ast_named_lock_put(lock2);
+
+ pthread_join(thread1, NULL);
+ pthread_join(thread2, NULL);
+
+ return res;
+}
+
+
+static int unload_module(void)
+{
+ AST_TEST_UNREGISTER(named_lock_test);
+ return 0;
+}
+
+static int load_module(void)
+{
+ AST_TEST_REGISTER(named_lock_test);
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Named Lock test module");
diff --git a/third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch b/third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch
new file mode 100644
index 000000000..60c27cb1a
--- /dev/null
+++ b/third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch
@@ -0,0 +1,30 @@
+From 0fc7ef5f01be9cc74d184c3ca3a973ff1ef44c93 Mon Sep 17 00:00:00 2001
+From: George Joseph <george.joseph@fairview5.com>
+Date: Sun, 10 Apr 2016 12:54:06 -0600
+Subject: [PATCH] sip_parser.c: Fix pjsip_VIA_PARAM_SPEC_ESC
+
+pjsip_VIA_PARAM_SPEC_ESC should have been pjsip_TOKEN_SPEC_ESC + ":" but
+instead of appending ":" to pjsip_VIA_PARAM_SPEC_ESC it was being appended
+to pjsip_VIA_PARAM_SPEC again. This was causing parsing of Via headers
+to fail when an ipv6 address was in a "received" param and
+PJSIP_UNESCAPE_IN_PLACE was used. Probably just a copy/paste error.
+---
+ pjsip/src/pjsip/sip_parser.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/pjsip/src/pjsip/sip_parser.c b/pjsip/src/pjsip/sip_parser.c
+index 378c22f..c18faa3 100644
+--- a/pjsip/src/pjsip/sip_parser.c
++++ b/pjsip/src/pjsip/sip_parser.c
+@@ -327,7 +327,7 @@ static pj_status_t init_parser()
+
+ status = pj_cis_dup(&pconst.pjsip_VIA_PARAM_SPEC_ESC, &pconst.pjsip_TOKEN_SPEC_ESC);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+- pj_cis_add_str(&pconst.pjsip_VIA_PARAM_SPEC, ":");
++ pj_cis_add_str(&pconst.pjsip_VIA_PARAM_SPEC_ESC, ":");
+
+ status = pj_cis_dup(&pconst.pjsip_HOST_SPEC, &pconst.pjsip_ALNUM_SPEC);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+--
+2.5.5
+