[asterisk-commits] qwell: branch qwell/skinny-linkedlists r49768 - /team/qwell/skinny-linkedlist...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Jan 6 14:08:58 MST 2007


Author: qwell
Date: Sat Jan  6 15:08:57 2007
New Revision: 49768

URL: http://svn.digium.com/view/asterisk?view=rev&rev=49768
Log:
linked lists stuff.
There are a few obvious places that still need to be changed, but this should be a good start at least.

I also changed the device types to an enum...just because..

Modified:
    team/qwell/skinny-linkedlists/channels/chan_skinny.c

Modified: team/qwell/skinny-linkedlists/channels/chan_skinny.c
URL: http://svn.digium.com/view/asterisk/team/qwell/skinny-linkedlists/channels/chan_skinny.c?view=diff&rev=49768&r1=49767&r2=49768
==============================================================================
--- team/qwell/skinny-linkedlists/channels/chan_skinny.c (original)
+++ team/qwell/skinny-linkedlists/channels/chan_skinny.c Sat Jan  6 15:08:57 2007
@@ -54,7 +54,6 @@
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
 #include "asterisk/options.h"
-#include "asterisk/lock.h"
 #include "asterisk/sched.h"
 #include "asterisk/io.h"
 #include "asterisk/rtp.h"
@@ -73,6 +72,7 @@
 #include "asterisk/astobj.h"
 #include "asterisk/abstract_jb.h"
 #include "asterisk/threadstorage.h"
+#include "asterisk/linkedlists.h"
 
 /*************************************
  * Skinny/Asterisk Protocol Settings *
@@ -774,34 +774,36 @@
 static int amaflags = 0;
 static int callnums = 1;
 
-#define SKINNY_DEVICE_UNKNOWN		-1
-#define SKINNY_DEVICE_NONE		0
-#define SKINNY_DEVICE_30SPPLUS		1
-#define SKINNY_DEVICE_12SPPLUS		2
-#define SKINNY_DEVICE_12SP		3
-#define SKINNY_DEVICE_12		4
-#define SKINNY_DEVICE_30VIP		5
-#define SKINNY_DEVICE_7910		6
-#define SKINNY_DEVICE_7960		7
-#define SKINNY_DEVICE_7940		8
-#define SKINNY_DEVICE_7935		9
-#define SKINNY_DEVICE_ATA186		12	/* Cisco ATA-186 */
-#define SKINNY_DEVICE_7941		115
-#define SKINNY_DEVICE_7971		119
-#define SKINNY_DEVICE_7985		302
-#define SKINNY_DEVICE_7911		307
-#define SKINNY_DEVICE_7961GE		308
-#define SKINNY_DEVICE_7941GE		309
-#define SKINNY_DEVICE_7905		20000
-#define SKINNY_DEVICE_7920		30002
-#define SKINNY_DEVICE_7970		30006
-#define SKINNY_DEVICE_7912		30007
-#define SKINNY_DEVICE_7902		30008
-#define SKINNY_DEVICE_CIPC		30016	/* Cisco IP Communicator */
-#define SKINNY_DEVICE_7961		30018
-#define SKINNY_DEVICE_7936		30019
-#define SKINNY_DEVICE_SCCPGATEWAY_AN	30027	/* ??? */
-#define SKINNY_DEVICE_SCCPGATEWAY_BRI	30028	/* ??? */
+enum skinny_devices {
+	SKINNY_DEVICE_UNKNOWN = -1,
+	SKINNY_DEVICE_NONE = 0,
+	SKINNY_DEVICE_30SPPLUS = 1,
+	SKINNY_DEVICE_12SPPLUS = 2,
+	SKINNY_DEVICE_12SP = 3,
+	SKINNY_DEVICE_12 = 4,
+	SKINNY_DEVICE_30VIP = 5,
+	SKINNY_DEVICE_7910 = 6,
+	SKINNY_DEVICE_7960 = 7,
+	SKINNY_DEVICE_7940 = 8,
+	SKINNY_DEVICE_7935 = 9,
+	SKINNY_DEVICE_ATA186 = 12, /* Cisco ATA-186 */
+	SKINNY_DEVICE_7941 = 115,
+	SKINNY_DEVICE_7971 = 119,
+	SKINNY_DEVICE_7985 = 302,
+	SKINNY_DEVICE_7911 = 307,
+	SKINNY_DEVICE_7961GE = 308,
+	SKINNY_DEVICE_7941GE = 309,
+	SKINNY_DEVICE_7905 = 20000,
+	SKINNY_DEVICE_7920 = 30002,
+	SKINNY_DEVICE_7970 = 30006,
+	SKINNY_DEVICE_7912 = 30007,
+	SKINNY_DEVICE_7902 = 30008,
+	SKINNY_DEVICE_CIPC = 30016, /* Cisco IP Communicator */
+	SKINNY_DEVICE_7961 = 30018,
+	SKINNY_DEVICE_7936 = 30019,
+	SKINNY_DEVICE_SCCPGATEWAY_AN = 30027, /* ??? */
+	SKINNY_DEVICE_SCCPGATEWAY_BRI = 30028 /* ??? */
+};
 
 #define SKINNY_SPEAKERON 1
 #define SKINNY_SPEAKEROFF 2
@@ -873,14 +875,6 @@
 AST_MUTEX_DEFINE_STATIC(monlock);
 /* Protect the network socket */
 AST_MUTEX_DEFINE_STATIC(netlock);
-/* Protect the session list */
-AST_MUTEX_DEFINE_STATIC(sessionlock);
-/* Protect the device list */
-AST_MUTEX_DEFINE_STATIC(devicelock);
-#if 0
-/* Protect the paging device list */
-AST_MUTEX_DEFINE_STATIC(pagingdevicelock);
-#endif
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use. */
@@ -911,7 +905,7 @@
 	int outgoing;
 	int alreadygone;
 
-	struct skinny_subchannel *next;
+	AST_LIST_ENTRY(skinny_subchannel) list;
 	struct skinny_line *parent;
 };
 
@@ -957,8 +951,8 @@
 	int nat;
 
 	struct ast_codec_pref prefs;
-	struct skinny_subchannel *sub;
-	struct skinny_line *next;
+	AST_LIST_HEAD(, skinny_subchannel) sub;
+	AST_LIST_ENTRY(skinny_line) list;
 	struct skinny_device *parent;
 };
 
@@ -968,7 +962,7 @@
 	char exten[AST_MAX_EXTENSION];
 	int instance;
 
-	struct skinny_speeddial *next;
+	AST_LIST_ENTRY(skinny_speeddial) list;
 	struct skinny_device *parent;
 };
 
@@ -976,39 +970,41 @@
 	ast_mutex_t lock;
 	char type[10];
 
-	struct skinny_addon *next;
+	AST_LIST_ENTRY(skinny_addon) list;
 	struct skinny_device *parent;
 };
 
-static struct skinny_device {
+struct skinny_device {
 	/* A device containing one or more lines */
 	char name[80];
 	char id[16];
 	char version_id[16];
-	int type;
+	enum skinny_devices type;
 	int registered;
 	int lastlineinstance;
 	int lastcallreference;
 	int capability;
 	struct sockaddr_in addr;
 	struct in_addr ourip;
-	struct skinny_line *lines;
-	struct skinny_speeddial *speeddials;
-	struct skinny_addon *addons;
+	AST_LIST_HEAD(, skinny_line) lines;
+	AST_LIST_HEAD(, skinny_speeddial) speeddials;
+	AST_LIST_HEAD(, skinny_addon) addons;
 	struct ast_codec_pref prefs;
 	struct ast_ha *ha;
 	struct skinnysession *session;
-	struct skinny_device *next;
-} *devices = NULL;
+	AST_LIST_ENTRY(skinny_device) list;
+};
+
+static AST_LIST_HEAD_STATIC(devices, skinny_device);
 
 struct skinny_paging_device {
 	char name[80];
 	char id[16];
 	struct skinny_device ** devices;
-	struct skinny_paging_device *next;
-};
-
-static struct skinnysession {
+	AST_LIST_ENTRY(skinny_paging_device) list;
+};
+
+struct skinnysession {
 	pthread_t t;
 	ast_mutex_t lock;
 	struct sockaddr_in sin;
@@ -1016,8 +1012,10 @@
 	char inbuf[SKINNY_MAX_PACKET];
 	char outbuf[SKINNY_MAX_PACKET];
 	struct skinny_device *device;
-	struct skinnysession *next;
-} *sessions = NULL;
+	AST_LIST_ENTRY(skinnysession) list;
+};
+
+static AST_LIST_HEAD_STATIC(sessions, skinnysession);
 
 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
@@ -1051,7 +1049,7 @@
 static void *get_button_template(struct skinnysession *s, struct button_definition_template *btn)
 {
 	struct skinny_device *d = s->device;
-	struct skinny_addon *a = d->addons;
+	struct skinny_addon *a;
 	int i;
 
 	switch (d->type) {
@@ -1152,7 +1150,8 @@
 			break;
 	}
 
-	for (a = d->addons; a; a = a->next) {
+	AST_LIST_LOCK(&d->addons);
+	AST_LIST_TRAVERSE(&d->addons, a, list) {
 		if (!strcasecmp(a->type, "7914")) {
 			for (i = 0; i < 14; i++)
 				(btn++)->buttonDefinition = BT_CUST_LINESPEEDDIAL;
@@ -1160,6 +1159,7 @@
 			ast_log(LOG_WARNING, "Unknown addon type '%s' found.  Skipping.\n", a->type);
 		}
 	}
+	AST_LIST_UNLOCK(&d->addons);
 
 	return btn;
 }
@@ -1181,7 +1181,7 @@
 {
 	struct skinny_line *l;
 
-	for (l = d->lines; l; l = l->next) {
+	AST_LIST_TRAVERSE(&d->lines, l, list) {
 		if (l->instance == instance)
 			break;
 	}
@@ -1208,23 +1208,23 @@
 	}
 	*at++ = '\0';
 	device = at;
-	ast_mutex_lock(&devicelock);
-	for (d = devices; d; d = d->next) {
+	AST_LIST_LOCK(&devices);
+	AST_LIST_TRAVERSE(&devices, d, list) {
 		if (!strcasecmp(d->name, device)) {
 			if (skinnydebug)
 				ast_verbose("Found device: %s\n", d->name);
 			/* Found the device */
-			for (l = d->lines; l; l = l->next) {
+			AST_LIST_TRAVERSE(&d->lines, l, list) {
 				/* Search for the right line */
 				if (!strcasecmp(l->name, line)) {
-					ast_mutex_unlock(&devicelock);
+					AST_LIST_UNLOCK(&devices);
 					return l;
 				}
 			}
 		}
 	}
 	/* Device not found */
-	ast_mutex_unlock(&devicelock);
+	AST_LIST_UNLOCK(&devices);
 	return NULL;
 }
 
@@ -1238,7 +1238,7 @@
 		return NULL;
 	}
 
-	for (sub = l->sub; sub; sub = sub->next) {
+	AST_LIST_TRAVERSE(&l->sub, sub, list) {
 		if (sub->callid == reference)
 			break;
 	}
@@ -1255,8 +1255,8 @@
 	struct skinny_line *l;
 	struct skinny_subchannel *sub = NULL;
 
-	for (l = d->lines; l; l = l->next) {
-		for (sub = l->sub; sub; sub = sub->next) {
+	AST_LIST_TRAVERSE(&d->lines, l, list) {
+		AST_LIST_TRAVERSE(&l->sub, sub, list) {
 			if (sub->callid == reference)
 				break;
 		}
@@ -1278,7 +1278,7 @@
 {
 	struct skinny_speeddial *sd;
 
-	for (sd = d->speeddials; sd; sd = sd->next) {
+	AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
 		if (sd->instance == instance)
 			break;
 	}
@@ -1340,8 +1340,8 @@
 	struct sockaddr_in sin;
 	socklen_t slen;
 
-	ast_mutex_lock(&devicelock);
-	for (d = devices; d; d = d->next) {
+	AST_LIST_LOCK(&devices);
+	AST_LIST_TRAVERSE(&devices, d, list) {
 		if (!strcasecmp(req->data.reg.name, d->id)
 				&& ast_apply_ha(d->ha, &(s->sin))) {
 			s->device = d;
@@ -1361,7 +1361,7 @@
 			break;
 		}
 	}
-	ast_mutex_unlock(&devicelock);
+	AST_LIST_UNLOCK(&devices);
 	if (!d) {
 		return 0;
 	}
@@ -1689,7 +1689,7 @@
 	transmit_displaymessage(s, NULL);
 
 /*
-	for (l = d->lines; l; l = l->next) {
+	AST_LIST_TRAVERSE(&d->lines, l, list) {
 		if (has_voicemail(l)) {
 			if (skinnydebug)
 				ast_verbose("Checking for voicemail Skinny %s@%s\n", l->name, d->name);
@@ -1778,7 +1778,9 @@
 	int which = 0;
 
 	if (pos == 2) {
-		for (d = devices; d && !result; d = d->next) {
+		AST_LIST_TRAVERSE(&devices, d, list) {
+			if (result)
+				break;
 			if (!strncasecmp(word, d->id, wordlen) && ++which > state)
 				result = ast_strdup(d->id);
 		}
@@ -1795,9 +1797,9 @@
 	if (argc < 3 || argc > 4) {
 		return RESULT_SHOWUSAGE;
 	}
-	ast_mutex_lock(&devicelock);
-
-	for (d = devices; d; d = d->next) {
+
+	AST_LIST_LOCK(&devices);
+	AST_LIST_TRAVERSE(&devices, d, list) {
 		int fullrestart = 0;
 		if (!strcasecmp(argv[2], d->id) || !strcasecmp(argv[2], "all")) {
 			if (!(d->session))
@@ -1819,7 +1821,7 @@
 			transmit_response(d->session, req);
 		}
 	}
-	ast_mutex_unlock(&devicelock);
+	AST_LIST_UNLOCK(&devices);
 	return RESULT_SUCCESS;
 }
 
@@ -1901,13 +1903,13 @@
 	if (argc != 3) {
 		return RESULT_SHOWUSAGE;
 	}
-	ast_mutex_lock(&devicelock);
-
+
+	AST_LIST_LOCK(&devices);
 	ast_cli(fd, "Name                 DeviceId         IP              Type            R NL\n");
 	ast_cli(fd, "-------------------- ---------------- --------------- --------------- - --\n");
-	for (d = devices; d; d = d->next) {
+	AST_LIST_TRAVERSE(&devices, d, list) {
 		numlines = 0;
-		for (l = d->lines; l; l = l->next) {
+		AST_LIST_TRAVERSE(&d->lines, l, list) {
 			numlines++;
 		}
 
@@ -1919,7 +1921,7 @@
 				d->registered?'Y':'N',
 				numlines);
 	}
-	ast_mutex_unlock(&devicelock);
+	AST_LIST_UNLOCK(&devices);
 	return RESULT_SUCCESS;
 }
 
@@ -1931,12 +1933,12 @@
 	if (argc != 3) {
 		return RESULT_SHOWUSAGE;
 	}
-	ast_mutex_lock(&devicelock);
-
+
+	AST_LIST_LOCK(&devices);
 	ast_cli(fd, "Device Name          Instance Name                 Label               \n");
 	ast_cli(fd, "-------------------- -------- -------------------- --------------------\n");
-	for (d = devices; d; d = d->next) {
-		for (l = d->lines; l; l = l->next) {
+	AST_LIST_TRAVERSE(&devices, d, list) {
+		AST_LIST_TRAVERSE(&d->lines, l, list) {
 			ast_cli(fd, "%-20s %8d %-20s %-20s\n",
 				d->name,
 				l->instance,
@@ -1945,7 +1947,7 @@
 		}
 	}
 
-	ast_mutex_unlock(&devicelock);
+	AST_LIST_UNLOCK(&devices);
 	return RESULT_SUCCESS;
 }
 
@@ -2015,7 +2017,7 @@
 		d->lastlineinstance = 1;
 		d->capability = default_capability;
 		d->prefs = default_prefs;
-		while(v) {
+		for (; v; v = v->next) {
 			if (!strcasecmp(v->name, "host")) {
 				if (ast_get_ip(&d->addr, v->value)) {
 					free(d);
@@ -2097,8 +2099,7 @@
 						ast_copy_string(sd->label, exten, sizeof(sd->label));
 					sd->instance = speeddialInstance++;
 
-					sd->next = d->speeddials;
-					d->speeddials = sd;
+					AST_LIST_INSERT_HEAD(&d->speeddials, sd, list);
 				}
 			} else if (!strcasecmp(v->name, "addon")) {
 				if (!(a = ast_calloc(1, sizeof(struct skinny_addon)))) {
@@ -2107,8 +2108,7 @@
 					ast_mutex_init(&a->lock);
 					ast_copy_string(a->type, v->value, sizeof(a->type));
 
-					a->next = d->addons;
-					d->addons = a;
+					AST_LIST_INSERT_HEAD(&d->addons, a, list);
 				}
 			} else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
 				if (!(l = ast_calloc(1, sizeof(struct skinny_line)))) {
@@ -2155,16 +2155,14 @@
 					l->hookstate = SKINNY_ONHOOK;
 					l->nat = nat;
 
-					l->next = d->lines;
-					d->lines = l;
+					AST_LIST_INSERT_HEAD(&d->lines, l, list);
 				}
 			} else {
 				ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
 			}
-			v = v->next;
-	 	}
-
-	 	if (!d->lines) {
+		}
+
+		if (!AST_LIST_FIRST(&d->lines)) {
 			ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
 			return NULL;
 		}
@@ -2725,8 +2723,7 @@
 			sub->parent = l;
 			sub->onhold = 0;
 
-			sub->next = l->sub;
-			l->sub = sub;
+			AST_LIST_INSERT_HEAD(&l->sub, sub, list);
 		}
 		tmp->tech = &skinny_tech;
 		tmp->tech_pvt = sub;
@@ -2960,13 +2957,13 @@
 		f.frametype = AST_FRAME_DTMF_END;
 		ast_queue_frame(sub->owner, &f);
 		/* XXX This seriously needs to be fixed */
-		if (sub->next && sub->next->owner) {
+		if (AST_LIST_NEXT(sub, list) && AST_LIST_NEXT(sub, list)->owner) {
 			if (sub->owner->_state == 0) {
 				f.frametype = AST_FRAME_DTMF_BEGIN;
-				ast_queue_frame(sub->next->owner, &f);
+				ast_queue_frame(AST_LIST_NEXT(sub, list)->owner, &f);
 			}
 			f.frametype = AST_FRAME_DTMF_END;
-			ast_queue_frame(sub->next->owner, &f);
+			ast_queue_frame(AST_LIST_NEXT(sub, list)->owner, &f);
 		}
 	} else {
 		if (skinnydebug)
@@ -3303,7 +3300,7 @@
 	transmit_callstate(s, l->instance, l->hookstate, sub->callid);
 	if (skinnydebug)
 		ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
-	if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
+	if (l->transfer && (sub->owner && AST_LIST_NEXT(sub, list) && AST_LIST_NEXT(sub, list)->owner) && ((!sub->outgoing) || (AST_LIST_NEXT(sub, list) && !AST_LIST_NEXT(sub, list)->outgoing))) {
 		/* We're allowed to transfer, we have two active calls and
 		   we made at least one of the calls.  Let's try and transfer */
 
@@ -3329,7 +3326,7 @@
 				l->name, d->name, sub->callid);
 		}
 	}
-	if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
+	if ((l->hookstate == SKINNY_ONHOOK) && (AST_LIST_NEXT(sub, list) && !AST_LIST_NEXT(sub, list)->rtp)) {
 		do_housekeeping(s);
 	}
 	return 1;
@@ -3357,7 +3354,7 @@
 
 	d->capability &= codecs;
 	ast_verbose("Device capability set to '%d'\n", d->capability);
-	for (l = d->lines; l; l = l->next) {
+	AST_LIST_TRAVERSE(&d->lines, l, list) {
 		ast_mutex_lock(&l->lock);
 		l->capability = d->capability;
 		ast_mutex_unlock(&l->lock);
@@ -3399,7 +3396,7 @@
 
 	instance = letohl(req->data.line.lineNumber);
 
-	ast_mutex_lock(&devicelock);
+	AST_LIST_LOCK(&devices);
 
 	l = find_line_by_instance(d, instance);
 
@@ -3407,7 +3404,7 @@
 		return 0;
 	}
 
-	ast_mutex_unlock(&devicelock);
+	AST_LIST_UNLOCK(&devices);
 
 	if (!(req = req_alloc(sizeof(struct line_stat_res_message), LINE_STAT_RES_MESSAGE)))
 		return -1;
@@ -3469,7 +3466,7 @@
 				req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
 				req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
 
-				for (l = d->lines; l; l = l->next) {
+				AST_LIST_TRAVERSE(&d->lines, l, list) {
 					if (l->instance == lineInstance) {
 						ast_verbose("Adding button: %d, %d\n", BT_LINE, lineInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
@@ -3482,7 +3479,7 @@
 				}
 
 				if (!btnSet) {
-					for (sd = d->speeddials; sd; sd = sd->next) {
+					AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
 						if (sd->instance == speeddialInstance) {
 							ast_verbose("Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
 							req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
@@ -3499,7 +3496,7 @@
 				req->data.buttontemplate.definition[i].buttonDefinition = htolel(BT_NONE);
 				req->data.buttontemplate.definition[i].instanceNumber = htolel(0);
 
-				for (l = d->lines; l; l = l->next) {
+				AST_LIST_TRAVERSE(&d->lines, l, list) {
 					if (l->instance == lineInstance) {
 						ast_verbose("Adding button: %d, %d\n", BT_LINE, lineInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_LINE;
@@ -3515,7 +3512,7 @@
 				req->data.buttontemplate.definition[i].buttonDefinition = BT_NONE;
 				req->data.buttontemplate.definition[i].instanceNumber = 0;
 
-				for (sd = d->speeddials; sd; sd = sd->next) {
+				AST_LIST_TRAVERSE(&d->speeddials, sd, list) {
 					if (sd->instance == speeddialInstance) {
 						ast_verbose("Adding button: %d, %d\n", BT_SPEEDDIAL, speeddialInstance);
 						req->data.buttontemplate.definition[i].buttonDefinition = BT_SPEEDDIAL;
@@ -3854,7 +3851,7 @@
 			transmit_callstate(s, l->instance, l->hookstate, sub->callid);
 			if (skinnydebug)
 				ast_verbose("Skinny %s@%s went on hook\n", l->name, d->name);
-			if (l->transfer && (sub->owner && sub->next && sub->next->owner) && ((!sub->outgoing) || (sub->next && !sub->next->outgoing))) {
+			if (l->transfer && (sub->owner && AST_LIST_NEXT(sub, list) && AST_LIST_NEXT(sub, list)->owner) && ((!sub->outgoing) || (AST_LIST_NEXT(sub, list) && !AST_LIST_NEXT(sub, list)->outgoing))) {
 				/* We're allowed to transfer, we have two active calls and
 				   we made at least one of the calls.  Let's try and transfer */
 
@@ -3880,7 +3877,7 @@
 						l->name, d->name, sub->callid);
 	 			}
 			}
-			if ((l->hookstate == SKINNY_ONHOOK) && (sub->next && !sub->next->rtp)) {
+			if ((l->hookstate == SKINNY_ONHOOK) && (AST_LIST_NEXT(sub, list) && !AST_LIST_NEXT(sub, list)->rtp)) {
 				do_housekeeping(s);
 			}
 		}
@@ -4106,31 +4103,21 @@
 
 static void destroy_session(struct skinnysession *s)
 {
-	struct skinnysession *cur, *prev = NULL;
-	ast_mutex_lock(&sessionlock);
-	cur = sessions;
-	while(cur) {
+	struct skinnysession *cur;
+	AST_LIST_LOCK(&sessions);
+	AST_LIST_TRAVERSE(&sessions, cur, list) {
 		if (cur == s) {
-			break;
-		}
-		prev = cur;
-		cur = cur->next;
-	}
-	if (cur) {
-		if (prev) {
-			prev->next = cur->next;
+			AST_LIST_REMOVE(&sessions, s, list);
+			if (s->fd > -1) {
+				close(s->fd);
+			}
+			ast_mutex_destroy(&s->lock);
+			free(s);
 		} else {
-			sessions = cur->next;
-		}
-		if (s->fd > -1) {
-			close(s->fd);
-		}
-		ast_mutex_destroy(&s->lock);
-		free(s);
-	} else {
-		ast_log(LOG_WARNING, "Trying to delete nonexistent session %p?\n", s);
-	}
-	ast_mutex_unlock(&sessionlock);
+			ast_log(LOG_WARNING, "Trying to delete nonexistent session %p?\n", s);
+		}
+	}
+	AST_LIST_UNLOCK(&sessions);
 }
 
 static int get_input(struct skinnysession *s)
@@ -4294,10 +4281,9 @@
 		memcpy(&s->sin, &sin, sizeof(sin));
 		ast_mutex_init(&s->lock);
 		s->fd = as;
-		ast_mutex_lock(&sessionlock);
-		s->next = sessions;
-		sessions = s;
-		ast_mutex_unlock(&sessionlock);
+		AST_LIST_LOCK(&sessions);
+		AST_LIST_INSERT_HEAD(&sessions, s, list);
+		AST_LIST_UNLOCK(&sessions);
 
 		if (ast_pthread_create(&tcp_thread, &attr, skinny_session, s)) {
 			destroy_session(s);
@@ -4426,11 +4412,9 @@
 	memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
 
 	/* load the general section */
-	v = ast_variable_browse(cfg, "general");
-	while (v) {
+	for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
 		/* handle jb conf */
 		if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
-			v = v->next;
 			continue;
 		}
 
@@ -4456,7 +4440,6 @@
 				ast_log(LOG_WARNING, "Invalid bindport '%s' at line %d of %s\n", v->value, v->lineno, config);
 			}
 		}
-		v = v->next;
 	}
 
 	if (ntohl(bindaddr.sin_addr.s_addr)) {
@@ -4491,10 +4474,9 @@
 			if (d) {
 				if (option_verbose > 2)
 					ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
-				ast_mutex_lock(&devicelock);
-				d->next = devices;
-				devices = d;
-				ast_mutex_unlock(&devicelock);
+				AST_LIST_LOCK(&devices);
+				AST_LIST_INSERT_HEAD(&devices, d, list);
+				AST_LIST_UNLOCK(&devices);
 			}
 		}
 		cat = ast_category_browse(cfg, cat);
@@ -4545,42 +4527,29 @@
 
 static void delete_devices(void)
 {
-	struct skinny_device *d, *dlast;
-	struct skinny_line *l, *llast;
-	struct skinny_speeddial *sd, *sdlast;
-	struct skinny_addon *a, *alast;
-
-	ast_mutex_lock(&devicelock);
-
+	struct skinny_device *d;
+	struct skinny_line *l;
+	struct skinny_speeddial *sd;
+	struct skinny_addon *a;
+
+	AST_LIST_LOCK(&devices);
 	/* Delete all devices */
-	for (d=devices;d;) {
+	while ((d = AST_LIST_REMOVE_HEAD(&devices, list))) {
 		/* Delete all lines for this device */
-		for (l=d->lines;l;) {
-			llast = l;
-			l = l->next;
-			ast_mutex_destroy(&llast->lock);
-			free(llast);
+		while ((l = AST_LIST_REMOVE_HEAD(&d->lines, list))) {
+			free(l);
 		}
 		/* Delete all speeddials for this device */
-		for (sd=d->speeddials;sd;) {
-			sdlast = sd;
-			sd = sd->next;
-			ast_mutex_destroy(&sdlast->lock);
-			free(sdlast);
+		while ((sd = AST_LIST_REMOVE_HEAD(&d->speeddials, list))) {
+			free(sd);
 		}
 		/* Delete all addons for this device */
-		for (a=d->addons;a;) {
-			alast = a;
-			a = a->next;
-			ast_mutex_destroy(&alast->lock);
-			free(alast);
-		}
-		dlast = d;
-		d = d->next;
-		free(dlast);
-	}
-	devices=NULL;
-	ast_mutex_unlock(&devicelock);
+		while ((a = AST_LIST_REMOVE_HEAD(&d->addons, list))) {
+			free(a);
+		}
+		free(d);
+	}
+	AST_LIST_UNLOCK(&devices);
 }
 
 #if 0
@@ -4634,38 +4603,33 @@
 
 static int unload_module(void)
 {
-	struct skinnysession *s, *slast;
+	struct skinnysession *s;
 	struct skinny_device *d;
 	struct skinny_line *l;
 	struct skinny_subchannel *sub;
 
-	ast_mutex_lock(&sessionlock);
+	AST_LIST_LOCK(&sessions);
 	/* Destroy all the interfaces and free their memory */
-	s = sessions;
-	while(s) {
-		slast = s;
-		s = s->next;
-		for (d = slast->device; d; d = d->next) {
-			for (l = d->lines; l; l = l->next) {
-				ast_mutex_lock(&l->lock);
-				for (sub = l->sub; sub; sub = sub->next) {
-					ast_mutex_lock(&sub->lock);
-					if (sub->owner) {
-						sub->alreadygone = 1;
-						ast_softhangup(sub->owner, AST_SOFTHANGUP_APPUNLOAD);
-					}
-					ast_mutex_unlock(&sub->lock);
+	s = AST_LIST_FIRST(&sessions);
+	while ((s = AST_LIST_REMOVE_HEAD(&sessions, list))) {
+		d = s->device;
+		AST_LIST_TRAVERSE(&d->lines, l, list) {
+			ast_mutex_lock(&l->lock);
+			AST_LIST_TRAVERSE(&l->sub, sub, list) {
+				ast_mutex_lock(&sub->lock);
+				if (sub->owner) {
+					sub->alreadygone = 1;
+					ast_softhangup(sub->owner, AST_SOFTHANGUP_APPUNLOAD);
 				}
-				ast_mutex_unlock(&l->lock);
+				ast_mutex_unlock(&sub->lock);
 			}
-		}
-		if (slast->fd > -1)
-			close(slast->fd);
-		ast_mutex_destroy(&slast->lock);
-		free(slast);
-	}
-	sessions = NULL;
-	ast_mutex_unlock(&sessionlock);
+			ast_mutex_unlock(&l->lock);
+		}
+		if (s->fd > -1)
+			close(s->fd);
+		free(s);
+	}
+	AST_LIST_UNLOCK(&sessions);
 
 	delete_devices();
 



More information about the asterisk-commits mailing list