[asterisk-commits] russell: branch russell/ast_verbose_threadstorage r38407 - in /team/russell/a...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Jul 27 23:58:19 MST 2006


Author: russell
Date: Fri Jul 28 01:58:18 2006
New Revision: 38407

URL: http://svn.digium.com/view/asterisk?rev=38407&view=rev
Log:
add the beginning of abstracting of the thread storage code i have done so far

Added:
    team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h   (with props)
Modified:
    team/russell/ast_verbose_threadstorage/channel.c
    team/russell/ast_verbose_threadstorage/utils.c

Modified: team/russell/ast_verbose_threadstorage/channel.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/channel.c?rev=38407&r1=38406&r2=38407&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/channel.c (original)
+++ team/russell/ast_verbose_threadstorage/channel.c Fri Jul 28 01:58:18 2006
@@ -66,6 +66,7 @@
 #include "asterisk/transcap.h"
 #include "asterisk/devicestate.h"
 #include "asterisk/sha1.h"
+#include "asterisk/threadstorage.h"
 
 struct channel_spy_trans {
 	int last_format;
@@ -92,8 +93,7 @@
 
 unsigned long global_fin = 0, global_fout = 0;
 
-static pthread_key_t state2str_buf_key;
-static pthread_once_t state2str_buf_once = PTHREAD_ONCE_INIT;
+AST_THREADSTORAGE(state2str_threadbuf, state2str_threadbuf_init);
 #define STATE2STR_BUFSIZE   32
 
 struct chanlist {
@@ -484,11 +484,6 @@
 	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)
 {
@@ -512,12 +507,8 @@
 	case AST_STATE_BUSY:
 		return "Busy";
 	default:
-		pthread_once(&state2str_buf_once, state2str_buf_key_create);
-		if (!(buf = pthread_getspecific(state2str_buf_key))) {
-			if (!(buf = ast_malloc(STATE2STR_BUFSIZE)))
-				return NULL;
-			pthread_setspecific(state2str_buf_key, buf);
-		}
+		if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
+			return "Unknown";
 		snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)\n", state);
 		return buf;
 	}

Added: team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h?rev=38407&view=auto
==============================================================================
--- team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h (added)
+++ team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h Fri Jul 28 01:58:18 2006
@@ -1,0 +1,65 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+  \file threadstorage.h
+  \brief Definitions to aid in the use of thread local storage
+*/
+
+#ifndef ASTERISK_THREADSTORAGE_H
+#define ASTERISK_THREADSTORAGE_H
+
+#include <pthread.h>
+
+#include "asterisk/utils.h"
+#include "asterisk/inline_api.h"
+
+struct ast_threadstorage {
+	pthread_once_t once;
+	pthread_key_t key;
+	void (*key_init)(void);
+};
+
+#define AST_THREADSTORAGE(name, name_init)     \
+static void name_init(void);                   \
+static struct ast_threadstorage name = {       \
+	.once = PTHREAD_ONCE_INIT,             \
+	.key_init = name_init,                 \
+};                                             \
+static void name_init(void)                    \
+{                                              \
+	pthread_key_create(&(name).key, FREE); \
+}
+
+AST_INLINE_API(
+void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size),
+{
+	void *buf;
+
+	pthread_once(&ts->once, ts->key_init);
+	if (!(buf = pthread_getspecific(ts->key))) {
+		if (!(buf = ast_calloc(1, init_size)))
+			return NULL;
+		pthread_setspecific(ts->key, buf);
+	}
+
+	return buf;
+}
+)
+
+#endif /* ASTERISK_THREADSTORAGE_H */

Propchange: team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/russell/ast_verbose_threadstorage/utils.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/utils.c?rev=38407&r1=38406&r2=38407&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/utils.c (original)
+++ team/russell/ast_verbose_threadstorage/utils.c Fri Jul 28 01:58:18 2006
@@ -58,6 +58,9 @@
 
 #define AST_API_MODULE		/* ensure that inlinable API functions will be built in this module if required */
 #include "asterisk/utils.h"
+
+#define AST_API_MODULE
+#include "asterisk/threadstorage.h"
 
 static char base64[64];
 static char b2a[256];



More information about the asterisk-commits mailing list