use rwlock for global vars to reduce contention

This commit is contained in:
Anthony Minessale 2011-07-12 20:36:36 -05:00
parent 11690aff4c
commit 0521886de8
3 changed files with 18 additions and 16 deletions

View File

@ -221,7 +221,7 @@ struct switch_runtime {
switch_mutex_t *throttle_mutex; switch_mutex_t *throttle_mutex;
switch_mutex_t *session_hash_mutex; switch_mutex_t *session_hash_mutex;
switch_mutex_t *global_mutex; switch_mutex_t *global_mutex;
switch_mutex_t *global_var_mutex; switch_thread_rwlock_t *global_var_rwlock;
uint32_t sps_total; uint32_t sps_total;
int32_t sps; int32_t sps;
int32_t sps_last; int32_t sps_last;

View File

@ -219,6 +219,7 @@ SWITCH_DECLARE(void) switch_channel_perform_set_callstate(switch_channel_t *chan
if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) { if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Original-Channel-Call-State", switch_channel_callstate2str(o_callstate)); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Original-Channel-Call-State", switch_channel_callstate2str(o_callstate));
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Channel-Call-State-Number", "%d", callstate);
switch_channel_event_set_data(channel, event); switch_channel_event_set_data(channel, event);
switch_event_fire(&event); switch_event_fire(&event);
} }

View File

@ -304,9 +304,9 @@ SWITCH_DECLARE(const char *) switch_core_get_switchname(void)
SWITCH_DECLARE(char *) switch_core_get_variable(const char *varname) SWITCH_DECLARE(char *) switch_core_get_variable(const char *varname)
{ {
char *val; char *val;
switch_mutex_lock(runtime.global_var_mutex); switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
val = (char *) switch_event_get_header(runtime.global_vars, varname); val = (char *) switch_event_get_header(runtime.global_vars, varname);
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return val; return val;
} }
@ -314,11 +314,11 @@ SWITCH_DECLARE(char *) switch_core_get_variable_dup(const char *varname)
{ {
char *val = NULL, *v; char *val = NULL, *v;
switch_mutex_lock(runtime.global_var_mutex); switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) { if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
val = strdup(v); val = strdup(v);
} }
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return val; return val;
} }
@ -327,21 +327,21 @@ SWITCH_DECLARE(char *) switch_core_get_variable_pdup(const char *varname, switch
{ {
char *val = NULL, *v; char *val = NULL, *v;
switch_mutex_lock(runtime.global_var_mutex); switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) { if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
val = switch_core_strdup(pool, v); val = switch_core_strdup(pool, v);
} }
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return val; return val;
} }
static void switch_core_unset_variables(void) static void switch_core_unset_variables(void)
{ {
switch_mutex_lock(runtime.global_var_mutex); switch_thread_rwlock_wrlock(runtime.global_var_rwlock);
switch_event_destroy(&runtime.global_vars); switch_event_destroy(&runtime.global_vars);
switch_event_create_plain(&runtime.global_vars, SWITCH_EVENT_CHANNEL_DATA); switch_event_create_plain(&runtime.global_vars, SWITCH_EVENT_CHANNEL_DATA);
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
} }
SWITCH_DECLARE(void) switch_core_set_variable(const char *varname, const char *value) SWITCH_DECLARE(void) switch_core_set_variable(const char *varname, const char *value)
@ -349,7 +349,7 @@ SWITCH_DECLARE(void) switch_core_set_variable(const char *varname, const char *v
char *val; char *val;
if (varname) { if (varname) {
switch_mutex_lock(runtime.global_var_mutex); switch_thread_rwlock_wrlock(runtime.global_var_rwlock);
val = (char *) switch_event_get_header(runtime.global_vars, varname); val = (char *) switch_event_get_header(runtime.global_vars, varname);
if (val) { if (val) {
switch_event_del_header(runtime.global_vars, varname); switch_event_del_header(runtime.global_vars, varname);
@ -361,7 +361,7 @@ SWITCH_DECLARE(void) switch_core_set_variable(const char *varname, const char *v
} else { } else {
switch_event_del_header(runtime.global_vars, varname); switch_event_del_header(runtime.global_vars, varname);
} }
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
} }
} }
@ -370,17 +370,17 @@ SWITCH_DECLARE(switch_bool_t) switch_core_set_var_conditional(const char *varnam
char *val; char *val;
if (varname) { if (varname) {
switch_mutex_lock(runtime.global_var_mutex); switch_thread_rwlock_wrlock(runtime.global_var_rwlock);
val = (char *) switch_event_get_header(runtime.global_vars, varname); val = (char *) switch_event_get_header(runtime.global_vars, varname);
if (val) { if (val) {
if (!val2 || strcmp(val, val2) != 0) { if (!val2 || strcmp(val, val2) != 0) {
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return SWITCH_FALSE; return SWITCH_FALSE;
} }
switch_event_del_header(runtime.global_vars, varname); switch_event_del_header(runtime.global_vars, varname);
} else if (!zstr(val2)) { } else if (!zstr(val2)) {
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return SWITCH_FALSE; return SWITCH_FALSE;
} }
@ -391,7 +391,7 @@ SWITCH_DECLARE(switch_bool_t) switch_core_set_var_conditional(const char *varnam
} else { } else {
switch_event_del_header(runtime.global_vars, varname); switch_event_del_header(runtime.global_vars, varname);
} }
switch_mutex_unlock(runtime.global_var_mutex); switch_thread_rwlock_unlock(runtime.global_var_rwlock);
} }
return SWITCH_TRUE; return SWITCH_TRUE;
} }
@ -1400,7 +1400,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc
switch_mutex_init(&runtime.session_hash_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool); switch_mutex_init(&runtime.session_hash_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool);
switch_mutex_init(&runtime.global_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool); switch_mutex_init(&runtime.global_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool);
switch_mutex_init(&runtime.global_var_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool);
switch_thread_rwlock_create(&runtime.global_var_rwlock, runtime.memory_pool);
switch_core_set_globals(); switch_core_set_globals();
switch_core_session_init(runtime.memory_pool); switch_core_session_init(runtime.memory_pool);
switch_event_create_plain(&runtime.global_vars, SWITCH_EVENT_CHANNEL_DATA); switch_event_create_plain(&runtime.global_vars, SWITCH_EVENT_CHANNEL_DATA);