add extra checking for race cond in bgapi

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11680 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2009-02-07 16:44:46 +00:00
parent ece81e6604
commit 304bd74cdc
1 changed files with 30 additions and 6 deletions

View File

@ -1199,7 +1199,8 @@ struct api_command_struct {
char *arg; char *arg;
listener_t *listener; listener_t *listener;
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
uint8_t bg; int bg;
int ack;
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
}; };
@ -1216,11 +1217,14 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
return NULL; return NULL;
} }
if (!acs->listener || !acs->listener->rwlock || switch_thread_rwlock_tryrdlock(acs->listener->rwlock) != SWITCH_STATUS_SUCCESS) { if (!acs->listener || !switch_test_flag(acs->listener, LFLAG_RUNNING) ||
!acs->listener->rwlock || switch_thread_rwlock_tryrdlock(acs->listener->rwlock) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error! cannot get read lock.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error! cannot get read lock.\n");
acs->ack = -1;
goto done; goto done;
} }
acs->ack = 1;
SWITCH_STANDARD_STREAM(stream); SWITCH_STANDARD_STREAM(stream);
@ -1270,12 +1274,23 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
} }
done: done:
if (acs->bg) { if (acs->bg) {
switch_memory_pool_t *pool = acs->pool; switch_memory_pool_t *pool = acs->pool;
if (acs->ack == -1) {
int sanity = 2000;
while(acs->ack == -1) {
switch_cond_next();
if (--sanity <= 0) break;
}
}
acs = NULL; acs = NULL;
switch_core_destroy_memory_pool(&pool); switch_core_destroy_memory_pool(&pool);
pool = NULL; pool = NULL;
} }
return NULL; return NULL;
} }
@ -1539,6 +1554,7 @@ static switch_status_t parse_command(listener_t *listener, switch_event_t **even
switch_thread_t *thread; switch_thread_t *thread;
switch_threadattr_t *thd_attr = NULL; switch_threadattr_t *thd_attr = NULL;
switch_uuid_t uuid; switch_uuid_t uuid;
int sanity;
strip_cr(api_cmd); strip_cr(api_cmd);
@ -1571,6 +1587,14 @@ static switch_status_t parse_command(listener_t *listener, switch_event_t **even
} }
switch_snprintf(reply, reply_len, "~Reply-Text: +OK Job-UUID: %s\nJob-UUID: %s\n\n", acs->uuid_str, acs->uuid_str); switch_snprintf(reply, reply_len, "~Reply-Text: +OK Job-UUID: %s\nJob-UUID: %s\n\n", acs->uuid_str, acs->uuid_str);
switch_thread_create(&thread, thd_attr, api_exec, acs, acs->pool); switch_thread_create(&thread, thd_attr, api_exec, acs, acs->pool);
sanity = 2000;
while(!acs->ack) {
switch_cond_next();
if (--sanity <= 0) break;
}
if (acs->ack == -1) {
acs->ack--;
}
status = SWITCH_STATUS_SUCCESS; status = SWITCH_STATUS_SUCCESS;
goto done_noreply; goto done_noreply;