[asterisk-commits] kpfleming: branch group/new_loader_completion r40394 - /team/group/new_loader...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Aug 18 12:22:16 MST 2006


Author: kpfleming
Date: Fri Aug 18 14:22:15 2006
New Revision: 40394

URL: http://svn.digium.com/view/asterisk?rev=40394&view=rev
Log:
uhh... so here's the very most beginning part of the new loader, totally non-functional

Modified:
    team/group/new_loader_completion/loader.c

Modified: team/group/new_loader_completion/loader.c
URL: http://svn.digium.com/view/asterisk/team/group/new_loader_completion/loader.c?rev=40394&r1=40393&r2=40394&view=diff
==============================================================================
--- team/group/new_loader_completion/loader.c (original)
+++ team/group/new_loader_completion/loader.c Fri Aug 18 14:22:15 2006
@@ -79,30 +79,96 @@
 { 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
   0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };
 
+static unsigned int embedding = 1; /* we always start out by registering embedded modules,
+				      since they are here before we dlopen() any
+				   */
+
+enum flags {
+	FLAG_EMBEDDED = (1 << 0),		/* module is embedded */
+};
+
 struct ast_module {
-	const struct ast_module_info *mod;
+	const struct ast_module_info *info;
 	void *lib;					/* the shared lib, or NULL if embedded */
 	int usecount;					/* the number of 'users' currently in this module */
 	struct ast_module_user_list users;		/* the list of users in the module */
+	unsigned int flags;				/* flags for this module */
 	AST_LIST_ENTRY(ast_module) entry;
 	char resource[0];
 };
+
+static AST_LIST_HEAD_STATIC(module_list, ast_module);
 
 struct loadupdate {
 	int (*updater)(void);
 	AST_LIST_ENTRY(loadupdate) entry;
 };
 
-static AST_LIST_HEAD_STATIC(module_list, ast_module);
+/* when dynamic modules are being loaded, ast_module_register() will
+   need to know what filename the module was loaded from while it
+   is being registered
+*/
+const char *module_being_loaded = NULL;
+
 static AST_LIST_HEAD_STATIC(updaters, loadupdate);
+
 AST_MUTEX_DEFINE_STATIC(reloadlock);
 
-void ast_module_register(const struct ast_module_info *mod)
-{
-}
-
-void ast_module_unregister(const struct ast_module_info *mod)
-{
+void ast_module_register(const struct ast_module_info *info)
+{
+	struct ast_module *mod;
+	size_t alloc_len;
+	const char *resource;
+
+	if (embedding)
+		resource = info->name;
+	else
+		resource = module_being_loaded;
+
+	if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(resource) + 1)))
+		return;
+
+	mod->info = info;
+	strcpy(mod->resource, resource);
+	AST_LIST_HEAD_INIT(&mod->users);
+	ast_set2_flag(mod, embedding, FLAG_EMBEDDED);
+
+	/* during startup, before the loader has been initialized,
+	   there are no threads, so there is no need to take the lock
+	   on this list to manipulate it. it is also possible that it
+	   might be unsafe to use the list lock at that point... so
+	   let's avoid it altogether
+	*/
+	if (!embedding)
+		AST_LIST_LOCK(&module_list);
+	/* it is paramount that the new entry be placed at the head of
+	   the list, otherwise the code that uses dlopen() to load
+	   dynamic modules won't be able to find out if the module it
+	   just opened was registered or failed to load
+	*/
+	AST_LIST_INSERT_HEAD(&module_list, mod, entry);
+	if (!embedding)
+		AST_LIST_UNLOCK(&module_list);
+}
+
+void ast_module_unregister(const struct ast_module_info *info)
+{
+	struct ast_module *mod = NULL;
+
+	AST_LIST_LOCK(&module_list);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
+		if (mod->info == info) {
+			AST_LIST_REMOVE_CURRENT(&module_list, entry);
+			break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&module_list);
+
+	if (mod) {
+		AST_LIST_HEAD_DESTROY(&mod->users);
+		free(mod);
+	}
 }
 
 struct ast_module_user *__ast_module_user_add(struct ast_module *mod,
@@ -244,7 +310,7 @@
 		}
 
 		__ast_module_user_hangup_all(cur);
-		res = cur->mod->unload();
+		res = cur->info->unload();
 
 		if (res) {
 			ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
@@ -287,7 +353,7 @@
 	AST_LIST_LOCK(&module_list);
 	AST_LIST_TRAVERSE(&module_list, cur, entry) {
 		if (!strncasecmp(word, cur->resource, l) &&
-		    (cur->mod->reload || !needsreload) &&
+		    (cur->info->reload || !needsreload) &&
 		    ++which > state) {
 			ret = strdup(cur->resource);
 			break;
@@ -331,12 +397,12 @@
 	AST_LIST_LOCK(&module_list);
 	oldversion = modlistver;
 	AST_LIST_TRAVERSE(&module_list, cur, entry) {
-		const struct ast_module_info *mod = cur->mod;
+		const struct ast_module_info *info = cur->info;
 
 		if (name && strcasecmp(name, cur->resource))	/* not ours */
 			continue;
 
-		if (!mod->reload) {	/* cannot be reloaded */
+		if (!info->reload) {	/* cannot be reloaded */
 			if (res < 1)	/* store result if possible */
 				res = 1;	/* 1 = no reload() method */
 			continue;
@@ -344,8 +410,8 @@
 
 		res = 2;
 		if (option_verbose > 2) 
-			ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", cur->resource, mod->description);
-		mod->reload();
+			ast_verbose(VERBOSE_PREFIX_3 "Reloading module '%s' (%s)\n", cur->resource, info->description);
+		info->reload();
 	}
 	AST_LIST_UNLOCK(&module_list);
 
@@ -403,7 +469,7 @@
 		return NULL;
 	}
 
-	cur->mod = m;
+	cur->info = m;
 
 	if (!m->load)
 		errors++;
@@ -501,6 +567,9 @@
 	struct ast_config *cfg;
 	struct dirent *d;
 	DIR *mods;
+
+	/* all embedded modules have registered themselves by now */
+	embedding = 0;
 
 	if (option_verbose)
 		ast_verbose("Asterisk Dynamic Loader Starting:\n");
@@ -601,7 +670,7 @@
 		unlock = 0;
 
 	AST_LIST_TRAVERSE(&module_list, cur, entry) {
-		total_mod_loaded += modentry(cur->resource, cur->mod->description, cur->usecount, like);
+		total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
 	}
 
 	if (unlock)



More information about the asterisk-commits mailing list