[asterisk-commits] russell: branch russell/events r84242 - /team/russell/events/res/ais/lck.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Oct 1 15:44:50 CDT 2007


Author: russell
Date: Mon Oct  1 15:44:49 2007
New Revision: 84242

URL: http://svn.digium.com/view/asterisk?view=rev&rev=84242
Log:
commit progress ... nothing new yet

Modified:
    team/russell/events/res/ais/lck.c

Modified: team/russell/events/res/ais/lck.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/res/ais/lck.c?view=diff&rev=84242&r1=84241&r2=84242
==============================================================================
--- team/russell/events/res/ais/lck.c (original)
+++ team/russell/events/res/ais/lck.c Mon Oct  1 15:44:49 2007
@@ -46,6 +46,8 @@
 #include "asterisk/logger.h"
 #include "asterisk/pbx.h"
 #include "asterisk/app.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/strings.h"
 
 SaLckHandleT lck_handle;
 
@@ -74,13 +76,56 @@
 	TRY_WRLOCK,
 };
 
-static struct ao2_container * attribute_unused lock_resources;
+#define LOCK_BUCKETS 101
+
+static struct ao2_container *lock_resources;
 
 struct lock_resource {
 	SaLckResourceHandleT *handle;
 	SaNameT ais_name;
-	char name[1];
-};
+	struct ast_str *name;
+};
+
+static void lock_destructor(void *obj)
+{
+	struct lock_resource *lock = obj;
+
+	if (lock->name)
+		ast_free(lock->name);
+}
+
+static inline struct lock_resource *lock_unref(struct lock_resource *lock)
+{
+	ao2_ref(lock, -1);
+	return NULL;
+}
+
+static struct lock_resource *find_lock(const char *name)
+{
+	struct ast_str *lock_name = ast_str_alloca(512);
+	struct lock_resource *lock, tmp_lock = {
+		.name = lock_name,
+	};
+
+	ast_str_set(&lock_name, 0, name);
+
+	if ((lock = ao2_find(lock_resources, &tmp_lock, OBJ_POINTER)))
+		return lock;
+
+	if (!(lock = ao2_alloc(sizeof(*lock), lock_destructor)))
+		return NULL;
+
+	if (!(lock->name = ast_str_create(16)))
+		return lock_unref(lock);
+
+	ast_str_set(&lock->name, 0, name);
+	if (!lock->name)
+		return lock_unref(lock);
+
+	// XXX open the dlock
+
+	return lock;
+}
 
 static int handle_lock(struct ast_channel *chan, enum lock_type lock_type,
 	char *data, char *buf, size_t len)
@@ -91,6 +136,7 @@
 	);
 	int res = 0;
 	double timeout = 3;
+	struct lock_resource *lock;
 
 	ast_autoservice_start(chan);
 
@@ -118,12 +164,18 @@
 		}
 	}
 
-
+	if (!(lock = find_lock(args.name))) {
+		ast_copy_string(buf, "FAILURE", len);
+		res = -1;
+		goto return_cleanup;
+	}
+
+	// XXX
 
 return_cleanup:
 	ast_autoservice_stop(chan);
 
-	return 0;
+	return res;
 }
 
 static int handle_rdlock(struct ast_channel *chan, const char *cmd, char *data,
@@ -153,6 +205,7 @@
 static int handle_unlock(struct ast_channel *chan, const char *cmd, char *data,
 	char *buf, size_t len)
 {
+	// XXX
 
 	return -1;
 }
@@ -234,13 +287,33 @@
 	.read = handle_unlock,
 };
 
+static int lock_hash_cb(const void *obj, int flags)
+{
+	const struct lock_resource *lock = obj;
+
+	return ast_str_hash(lock->name->str);
+}
+
+static int lock_cmp_cb(void *obj, void *arg, int flags)
+{
+	struct lock_resource *lock1 = obj, *lock2 = arg;
+
+	return !strcasecmp(lock1->name->str, lock2->name->str) ? CMP_MATCH : 0;
+}
+
 int ast_ais_lck_load_module(void)
 {
 	SaAisErrorT ais_res;
 	int res;
 
+	if (!(lock_resources = ao2_container_alloc(LOCK_BUCKETS, 
+		lock_hash_cb, lock_cmp_cb))) {
+		return -1;
+	}
+
 	ais_res = saLckInitialize(&lck_handle, &lck_callbacks, &ais_version);
 	if (ais_res != SA_AIS_OK) {
+		ao2_ref(lock_resources, -1);
 		ast_log(LOG_ERROR, "Could not initialize distributed locking service: %s\n",
 			ais_err2str(ais_res));
 		return -1;
@@ -258,6 +331,7 @@
 int ast_ais_lck_unload_module(void)
 {
 	SaAisErrorT ais_res;
+	int res = 0;
 
 	ast_custom_function_unregister(&dlock_rdlock);
 	ast_custom_function_unregister(&dlock_wrlock);
@@ -269,8 +343,10 @@
 	if (ais_res != SA_AIS_OK) {
 		ast_log(LOG_ERROR, "Problem stopping distributed locking service: %s\n", 
 			ais_err2str(ais_res));
-		return -1;
-	}
-
-	return 0;
-}
+		res = -1;
+	}
+
+	ao2_ref(lock_resources, -1);
+
+	return res;
+}




More information about the asterisk-commits mailing list