/*! \brief Number of buckets for types (should be prime for performance reasons) */
#define TYPE_BUCKETS 53
+/*! \brief Number of buckets for instances (should be prime for performance reasons) */
+#define INSTANCE_BUCKETS 17
+
/*! \brief Thread pool for observers */
static struct ast_threadpool *threadpool;
struct ast_sorcery {
/*! \brief Container for known object types */
struct ao2_container *types;
+ /*! \brief The name of the module owning this sorcery instance */
+ char module_name[0];
};
/*! \brief Structure for passing load/reload details */
};
/*! \brief Registered sorcery wizards */
-struct ao2_container *wizards;
+static struct ao2_container *wizards;
+
+/*! \brief Registered sorcery instances */
+static struct ao2_container *instances;
static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
{
/*! \brief Hashing function for sorcery wizards */
static int sorcery_wizard_hash(const void *obj, const int flags)
{
- const struct ast_sorcery_wizard *wizard = obj;
- const char *name = obj;
+ const struct ast_sorcery_wizard *object;
+ const char *key;
- return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_KEY:
+ key = obj;
+ break;
+ case OBJ_SEARCH_OBJECT:
+ object = obj;
+ key = object->name;
+ break;
+ default:
+ ast_assert(0);
+ return 0;
+ }
+ return ast_str_hash(key);
}
/*! \brief Comparator function for sorcery wizards */
static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
{
- struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
- const char *name = arg;
-
- return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
+ const struct ast_sorcery_wizard *object_left = obj;
+ const struct ast_sorcery_wizard *object_right = arg;
+ const char *right_key = arg;
+ int cmp;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_OBJECT:
+ right_key = object_right->name;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(object_left->name, right_key);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(object_left->name, right_key, strlen(right_key));
+ break;
+ default:
+ cmp = 0;
+ break;
+ }
+ if (cmp) {
+ return 0;
+ }
+ return CMP_MATCH;
}
/*! \brief Cleanup function */
static void sorcery_cleanup(void)
{
ao2_cleanup(wizards);
+ wizards = NULL;
+ ao2_cleanup(instances);
+ instances = NULL;
+}
+
+/*! \brief Compare function for sorcery instances */
+static int sorcery_instance_cmp(void *obj, void *arg, int flags)
+{
+ const struct ast_sorcery *object_left = obj;
+ const struct ast_sorcery *object_right = arg;
+ const char *right_key = arg;
+ int cmp;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_OBJECT:
+ right_key = object_right->module_name;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(object_left->module_name, right_key);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
+ break;
+ default:
+ cmp = 0;
+ break;
+ }
+ if (cmp) {
+ return 0;
+ }
+ return CMP_MATCH;
+}
+
+/*! \brief Hashing function for sorcery instances */
+static int sorcery_instance_hash(const void *obj, const int flags)
+{
+ const struct ast_sorcery *object;
+ const char *key;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_KEY:
+ key = obj;
+ break;
+ case OBJ_SEARCH_OBJECT:
+ object = obj;
+ key = object->module_name;
+ break;
+ default:
+ ast_assert(0);
+ return 0;
+ }
+ return ast_str_hash(key);
}
int ast_sorcery_init(void)
return -1;
}
+ instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
+ sorcery_instance_hash, sorcery_instance_cmp);
+ if (!instances) {
+ sorcery_cleanup();
+ sorcery_exit();
+ return -1;
+ }
+
ast_register_cleanup(sorcery_cleanup);
ast_register_atexit(sorcery_exit);
/*! \brief Hashing function for sorcery types */
static int sorcery_type_hash(const void *obj, const int flags)
{
- const struct ast_sorcery_object_type *type = obj;
- const char *name = obj;
+ const struct ast_sorcery_object_type *object;
+ const char *key;
- return ast_str_hash(flags & OBJ_KEY ? name : type->name);
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_KEY:
+ key = obj;
+ break;
+ case OBJ_SEARCH_OBJECT:
+ object = obj;
+ key = object->name;
+ break;
+ default:
+ ast_assert(0);
+ return 0;
+ }
+ return ast_str_hash(key);
}
/*! \brief Comparator function for sorcery types */
static int sorcery_type_cmp(void *obj, void *arg, int flags)
{
- struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
- const char *name = arg;
-
- return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
+ const struct ast_sorcery_object_type *object_left = obj;
+ const struct ast_sorcery_object_type *object_right = arg;
+ const char *right_key = arg;
+ int cmp;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_OBJECT:
+ right_key = object_right->name;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(object_left->name, right_key);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(object_left->name, right_key, strlen(right_key));
+ break;
+ default:
+ cmp = 0;
+ break;
+ }
+ if (cmp) {
+ return 0;
+ }
+ return CMP_MATCH;
}
-struct ast_sorcery *ast_sorcery_open(void)
+struct ast_sorcery *__ast_sorcery_open(const char *module_name)
{
struct ast_sorcery *sorcery;
- if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
- return NULL;
+ ast_assert(module_name != NULL);
+
+ ao2_wrlock(instances);
+ if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
+ goto done;
+ }
+
+ if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
+ goto done;
}
if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
ao2_ref(sorcery, -1);
sorcery = NULL;
+ goto done;
}
+ strcpy(sorcery->module_name, module_name); /* Safe */
+ ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
+
+done:
+ ao2_unlock(instances);
return sorcery;
}
+/*! \brief Search function for sorcery instances */
+struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
+{
+ return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
+}
+
/*! \brief Destructor function for object types */
static void sorcery_object_type_destructor(void *obj)
{
void ast_sorcery_unref(struct ast_sorcery *sorcery)
{
- ao2_cleanup(sorcery);
+ if (sorcery) {
+ /* One ref for what we just released, the other for the instances container. */
+ ao2_wrlock(instances);
+ if (ao2_ref(sorcery, -1) == 2) {
+ ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
+ }
+ ao2_unlock(instances);
+ }
}
const char *ast_sorcery_object_get_id(const void *object)
sip_threadpool = ast_threadpool_create("SIP", NULL, &options);
if (!sip_threadpool) {
ast_log(LOG_ERROR, "Failed to create SIP threadpool. Aborting load\n");
+ ast_sip_destroy_system();
pj_pool_release(memory_pool);
memory_pool = NULL;
pjsip_endpt_destroy(ast_pjsip_endpoint);
NULL, PJ_THREAD_DEFAULT_STACK_SIZE * 2, 0, &monitor_thread);
if (status != PJ_SUCCESS) {
ast_log(LOG_ERROR, "Failed to start SIP monitor thread. Aborting load\n");
+ ast_sip_destroy_system();
pj_pool_release(memory_pool);
memory_pool = NULL;
pjsip_endpt_destroy(ast_pjsip_endpoint);
ast_log(LOG_ERROR, "Failed to initialize SIP configuration. Aborting load\n");
ast_sip_destroy_global_headers();
stop_monitor_thread();
+ ast_sip_destroy_system();
pj_pool_release(memory_pool);
memory_pool = NULL;
pjsip_endpt_destroy(ast_pjsip_endpoint);
ast_res_pjsip_destroy_configuration();
ast_sip_destroy_global_headers();
stop_monitor_thread();
+ ast_sip_destroy_system();
pj_pool_release(memory_pool);
memory_pool = NULL;
pjsip_endpt_destroy(ast_pjsip_endpoint);
ast_res_pjsip_destroy_configuration();
ast_sip_destroy_global_headers();
stop_monitor_thread();
+ ast_sip_destroy_system();
pj_pool_release(memory_pool);
memory_pool = NULL;
pjsip_endpt_destroy(ast_pjsip_endpoint);
ast_res_pjsip_destroy_configuration();
ast_sip_destroy_global_headers();
stop_monitor_thread();
+ ast_sip_destroy_system();
pj_pool_release(memory_pool);
memory_pool = NULL;
pjsip_endpt_destroy(ast_pjsip_endpoint);
#include "asterisk/test.h"
#include "asterisk/module.h"
+#include "asterisk/astobj2.h"
#include "asterisk/sorcery.h"
#include "asterisk/logger.h"
#include "asterisk/json.h"
AST_TEST_DEFINE(sorcery_open)
{
RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+ RAII_VAR(struct ast_sorcery *, sorcery2, NULL, ast_sorcery_unref);
+ int refcount;
switch (cmd) {
case TEST_INIT:
info->name = "open";
info->category = "/main/sorcery/";
- info->summary = "sorcery open unit test";
+ info->summary = "sorcery open/close unit test";
info->description =
- "Test opening of sorcery";
+ "Test opening of sorcery and registry operations";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
+ if ((sorcery = ast_sorcery_retrieve_by_module_name(AST_MODULE))) {
+ ast_test_status_update(test, "There should NOT have been an existing sorcery instance\n");
+ return AST_TEST_FAIL;
+ }
+
if (!(sorcery = ast_sorcery_open())) {
ast_test_status_update(test, "Failed to open new sorcery structure\n");
return AST_TEST_FAIL;
}
+ if (!(sorcery2 = ast_sorcery_retrieve_by_module_name(AST_MODULE))) {
+ ast_test_status_update(test, "Failed to find sorcery structure in registry\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (sorcery2 != sorcery) {
+ ast_test_status_update(test, "Should have gotten same sorcery on retrieve\n");
+ return AST_TEST_FAIL;
+ }
+ ast_sorcery_unref(sorcery2);
+
+ if ((refcount = ao2_ref(sorcery, 0)) != 2) {
+ ast_test_status_update(test, "Should have been 2 references to sorcery instead of %d\n", refcount);
+ return AST_TEST_FAIL;
+ }
+
+ if (!(sorcery2 = ast_sorcery_open())) {
+ ast_test_status_update(test, "Failed to open second sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (sorcery2 != sorcery) {
+ ast_test_status_update(test, "Should have gotten same sorcery on 2nd open\n");
+ return AST_TEST_FAIL;
+ }
+
+ if ((refcount = ao2_ref(sorcery, 0)) != 3) {
+ ast_test_status_update(test, "Should have been 3 references to sorcery instead of %d\n", refcount);
+ return AST_TEST_FAIL;
+ }
+
+ ast_sorcery_unref(sorcery);
+ ast_sorcery_unref(sorcery2);
+
+ sorcery2 = NULL;
+
+ if ((sorcery = ast_sorcery_retrieve_by_module_name(AST_MODULE))) {
+ ast_sorcery_unref(sorcery);
+ sorcery = NULL;
+ ast_test_status_update(test, "Should NOT have found sorcery structure in registry\n");
+ return AST_TEST_FAIL;
+ }
+
return AST_TEST_PASS;
}