[asterisk-commits] russell: trunk r38060 - /trunk/channel.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Jul 21 15:36:25 MST 2006


Author: russell
Date: Fri Jul 21 17:36:25 2006
New Revision: 38060

URL: http://svn.digium.com/view/asterisk?rev=38060&view=rev
Log:
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

Modified:
    trunk/channel.c

Modified: trunk/channel.c
URL: http://svn.digium.com/view/asterisk/trunk/channel.c?rev=38060&r1=38059&r2=38060&view=diff
==============================================================================
--- trunk/channel.c (original)
+++ trunk/channel.c Fri Jul 21 17:36:25 2006
@@ -92,7 +92,9 @@
 
 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 @@
 
 	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 @@
 	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;
 	}
 }
 



More information about the asterisk-commits mailing list