[asterisk-commits] tilghman: branch 1.6.2 r307792 - /branches/1.6.2/res/res_odbc.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Feb 14 14:10:34 CST 2011


Author: tilghman
Date: Mon Feb 14 14:10:28 2011
New Revision: 307792

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=307792
Log:
Increment usage count at first reference, to avoid a race condition with many threads creating connections all at once.

(issue #18156)
 Reported by: asgaroth
 Patches: 
       20110214__issue18156.diff.txt uploaded by tilghman (license 14)
 Tested by: tilghman

Modified:
    branches/1.6.2/res/res_odbc.c

Modified: branches/1.6.2/res/res_odbc.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.2/res/res_odbc.c?view=diff&rev=307792&r1=307791&r2=307792
==============================================================================
--- branches/1.6.2/res/res_odbc.c (original)
+++ branches/1.6.2/res/res_odbc.c Mon Feb 14 14:10:28 2011
@@ -1177,10 +1177,11 @@
 			ast_assert(ao2_ref(obj, 0) > 1);
 		}
 
-		if (!obj && (class->count < class->limit)) {
+		if (!obj && (ast_atomic_fetchadd_int(&class->count, +1) < class->limit)) {
 			obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
 			if (!obj) {
 				ao2_ref(class, -1);
+				ast_atomic_fetchadd_int(&class->count, -1);
 				return NULL;
 			}
 			ast_assert(ao2_ref(obj, 0) == 1);
@@ -1191,14 +1192,18 @@
 			if (odbc_obj_connect(obj) == ODBC_FAIL) {
 				ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
 				ao2_ref(obj, -1);
+				obj = NULL;
 				ast_assert(ao2_ref(class, 0) > 0);
-				obj = NULL;
+				ast_atomic_fetchadd_int(&class->count, -1);
 			} else {
 				obj->used = 1;
 				ao2_link(obj->parent->obj_container, obj);
-				ast_atomic_fetchadd_int(&obj->parent->count, +1);
 			}
 		} else {
+			/* If construction fails due to the limit, remove our increment. */
+			if (!obj) {
+				ast_atomic_fetchadd_int(&class->count, -1);
+			}
 			/* Object is not constructed, so delete outstanding reference to class. */
 			ao2_ref(class, -1);
 			class = NULL;




More information about the asterisk-commits mailing list