diff options
author | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-07-21 22:36:25 +0000 |
---|---|---|
committer | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-07-21 22:36:25 +0000 |
commit | 17d5d2fdb799f2838aca5c24b3a8a6fdaaf5b32d (patch) | |
tree | e29282376eeb2d327c2b4d7551572d3eb7bb67dd /channel.c | |
parent | e4561310c6f06332382a8c6fe079208589904aea (diff) |
make ast_state2str thread safe by using thread local storage instead of a
static buffer for storing the result when the state value is unknown
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@38060 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'channel.c')
-rw-r--r-- | channel.c | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -92,7 +92,9 @@ static int uniqueint = 0; unsigned long global_fin = 0, global_fout = 0; -/* XXX Lock appropriately in more functions XXX */ +static pthread_key_t state2str_buf_key; +static pthread_once_t state2str_buf_once = PTHREAD_ONCE_INIT; +#define STATE2STR_BUFSIZE 32 struct chanlist { const struct ast_channel_tech *tech; @@ -482,12 +484,17 @@ int ast_str2cause(const char *name) return -1; } - + +static void state2str_buf_key_create(void) +{ + pthread_key_create(&state2str_buf_key, free); +} + /*! \brief Gives the string form of a given channel state */ char *ast_state2str(int state) { - /* XXX Not reentrant XXX */ - static char localtmp[256]; + char *buf; + switch(state) { case AST_STATE_DOWN: return "Down"; @@ -506,8 +513,14 @@ char *ast_state2str(int state) case AST_STATE_BUSY: return "Busy"; default: - snprintf(localtmp, sizeof(localtmp), "Unknown (%d)\n", state); - return localtmp; + pthread_once(&state2str_buf_once, state2str_buf_key_create); + if (!(buf = pthread_getspecific(state2str_buf_key))) { + if (!(buf = ast_calloc(1, STATE2STR_BUFSIZE))) + return NULL; + pthread_setspecific(state2str_buf_key, buf); + } + snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)\n", state); + return buf; } } |