aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2011-02-14 20:10:28 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2011-02-14 20:10:28 +0000
commit41fe6df427966456e6f403a77751af0a0bc3d213 (patch)
treee5f783ea82a8020530650d2c72f6829f8f4d004a
parentb54eb6e1d80b85a2e56caf6481919dbc79192611 (diff)
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 git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.2@307792 f38db490-d61c-443f-a65b-d21fe96a405b
-rw-r--r--res/res_odbc.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/res/res_odbc.c b/res/res_odbc.c
index a75ed415f..c6bbd7a57 100644
--- a/res/res_odbc.c
+++ b/res/res_odbc.c
@@ -1177,10 +1177,11 @@ struct odbc_obj *ast_odbc_request_obj2(const char *name, struct ast_flags flags)
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 @@ struct odbc_obj *ast_odbc_request_obj2(const char *name, struct ast_flags flags)
if (odbc_obj_connect(obj) == ODBC_FAIL) {
ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
ao2_ref(obj, -1);
- ast_assert(ao2_ref(class, 0) > 0);
obj = NULL;
+ ast_assert(ao2_ref(class, 0) > 0);
+ 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;