[asterisk-commits] kpfleming: trunk r49578 - in /trunk: ./ build_tools/ include/ include/asteris...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Jan 4 16:18:37 MST 2007


Author: kpfleming
Date: Thu Jan  4 17:18:36 2007
New Revision: 49578

URL: http://svn.digium.com/view/asterisk?view=rev&rev=49578
Log:
Merged revisions 49553 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r49553 | kpfleming | 2007-01-04 16:51:01 -0600 (Thu, 04 Jan 2007) | 2 lines

add support for tracking thread-local-storage objects that exist via 'threadstorage' CLI commands

........

Added:
    trunk/main/threadstorage.c
      - copied unchanged from r49553, branches/1.4/main/threadstorage.c
Modified:
    trunk/   (props changed)
    trunk/build_tools/cflags.xml
    trunk/include/asterisk.h
    trunk/include/asterisk/strings.h
    trunk/include/asterisk/threadstorage.h
    trunk/main/Makefile
    trunk/main/asterisk.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-1.4-merged' - no diff available.

Modified: trunk/build_tools/cflags.xml
URL: http://svn.digium.com/view/asterisk/trunk/build_tools/cflags.xml?view=diff&rev=49578&r1=49577&r2=49578
==============================================================================
--- trunk/build_tools/cflags.xml (original)
+++ trunk/build_tools/cflags.xml Thu Jan  4 17:18:36 2007
@@ -4,6 +4,8 @@
 		<member name="DEBUG_SCHEDULER" displayname="Enable Scheduler Debugging Output">
 		</member>
 		<member name="DEBUG_THREADS" displayname="Enable Thread Debugging">
+		</member>
+		<member name="DEBUG_THREADLOCALS" displayname="Enable Thread-Local-Storage Debugging">
 		</member>
 		<member name="DETECT_DEADLOCKS" displayname="Detect Deadlocks">
 		</member>

Modified: trunk/include/asterisk.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk.h?view=diff&rev=49578&r1=49577&r2=49578
==============================================================================
--- trunk/include/asterisk.h (original)
+++ trunk/include/asterisk.h Thu Jan  4 17:18:36 2007
@@ -81,6 +81,7 @@
 int dnsmgr_init(void);				/*!< Provided by dnsmgr.c */ 
 void dnsmgr_start_refresh(void);		/*!< Provided by dnsmgr.c */
 int dnsmgr_reload(void);			/*!< Provided by dnsmgr.c */
+void threadstorage_init(void);			/*!< Provided by threadstorage.c */
 
 /* Many headers need 'ast_channel' to be defined */
 struct ast_channel;

Modified: trunk/include/asterisk/strings.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/strings.h?view=diff&rev=49578&r1=49577&r2=49578
==============================================================================
--- trunk/include/asterisk/strings.h (original)
+++ trunk/include/asterisk/strings.h Thu Jan  4 17:18:36 2007
@@ -365,6 +365,10 @@
 AST_INLINE_API(
 int ast_str_make_space(struct ast_str **buf, size_t new_len),
 {
+#if defined(DEBUG_THREADLOCALS)
+	struct ast_str *old_buf = *buf;
+#endif /* defined(DEBUG_THREADLOCALS) */
+
 	if (new_len <= (*buf)->len) 
 		return 0;	/* success */
 	if ((*buf)->ts == DS_ALLOCA || (*buf)->ts == DS_STATIC)
@@ -372,8 +376,12 @@
 	*buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
 	if (*buf == NULL) /* XXX watch out, we leak memory here */
 		return -1;
-	if ((*buf)->ts != DS_MALLOC)
+	if ((*buf)->ts != DS_MALLOC) {
 		pthread_setspecific((*buf)->ts->key, *buf);
+#if defined(DEBUG_THREADLOCALS)
+		__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));
+#endif /* defined(DEBUG_THREADLOCALS) */
+	}
 
         (*buf)->len = new_len;
         return 0;

Modified: trunk/include/asterisk/threadstorage.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/threadstorage.h?view=diff&rev=49578&r1=49577&r2=49578
==============================================================================
--- trunk/include/asterisk/threadstorage.h (original)
+++ trunk/include/asterisk/threadstorage.h Thu Jan  4 17:18:36 2007
@@ -62,6 +62,12 @@
 	int (*custom_init)(void *); /*!< Custom initialization function specific to the object */
 };
 
+#if defined(DEBUG_THREADLOCALS)
+void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line);
+void __ast_threadstorage_object_remove(void *key);
+void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len);
+#endif /* defined(DEBUG_THREADLOCALS) */
+
 /*!
  * \brief Define a thread storage variable
  *
@@ -93,17 +99,36 @@
  * AST_THREADSTORAGE_CUSTOM(my_buf, my_init, my_cleanup);
  * \endcode
  */
-#define AST_THREADSTORAGE_CUSTOM(name, c_init, c_cleanup) \
-static void init_##name(void);                            \
-static struct ast_threadstorage name = {                  \
-	.once = PTHREAD_ONCE_INIT,                        \
-	.key_init = init_##name,                          \
-	.custom_init = c_init,                            \
-};                                                        \
-static void init_##name(void)                             \
-{                                                         \
-	pthread_key_create(&(name).key, c_cleanup);       \
-}
+#if !defined(DEBUG_THREADLOCALS)
+#define AST_THREADSTORAGE_CUSTOM(name, c_init, c_cleanup)	\
+static void __init_##name(void);				\
+static struct ast_threadstorage name = {			\
+	.once = PTHREAD_ONCE_INIT,				\
+	.key_init = __init_##name,				\
+	.custom_init = c_init,					\
+};								\
+static void __init_##name(void)					\
+{								\
+	pthread_key_create(&(name).key, c_cleanup);		\
+}
+#else /* defined(DEBUG_THREADLOCALS) */
+#define AST_THREADSTORAGE_CUSTOM(name, c_init, c_cleanup)	\
+static void __init_##name(void);				\
+static struct ast_threadstorage name = {			\
+	.once = PTHREAD_ONCE_INIT,				\
+	.key_init = __init_##name,				\
+	.custom_init = c_init,					\
+};								\
+static void __cleanup_##name(void *data)			\
+{								\
+	__ast_threadstorage_object_remove(data);		\
+	c_cleanup(data);					\
+}								\
+static void __init_##name(void)					\
+{								\
+	pthread_key_create(&(name).key, __cleanup_##name);	\
+}
+#endif /* defined(DEBUG_THREADLOCALS) */
 
 /*!
  * \brief Retrieve thread storage
@@ -135,6 +160,7 @@
  * }
  * \endcode
  */
+#if !defined(DEBUG_THREADLOCALS)
 AST_INLINE_API(
 void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size),
 {
@@ -154,7 +180,29 @@
 	return buf;
 }
 )
-
-void __ast_threadstorage_cleanup(void *);
+#else /* defined(DEBUG_THREADLOCALS) */
+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;
+		if (ts->custom_init && ts->custom_init(buf)) {
+			free(buf);
+			return NULL;
+		}
+		pthread_setspecific(ts->key, buf);
+		__ast_threadstorage_object_add(buf, init_size, file, function, line);
+	}
+
+	return buf;
+}
+)
+
+#define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#endif /* defined(DEBUG_THREADLOCALS) */
 
 #endif /* ASTERISK_THREADSTORAGE_H */

Modified: trunk/main/Makefile
URL: http://svn.digium.com/view/asterisk/trunk/main/Makefile?view=diff&rev=49578&r1=49577&r2=49578
==============================================================================
--- trunk/main/Makefile (original)
+++ trunk/main/Makefile Thu Jan  4 17:18:36 2007
@@ -26,7 +26,7 @@
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
 	netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
 	cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
-	strcompat.o
+	strcompat.o threadstorage.o
 
 # we need to link in the objects statically, not as a library, because
 # otherwise modules will not have them available if none of the static

Modified: trunk/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/trunk/main/asterisk.c?view=diff&rev=49578&r1=49577&r2=49578
==============================================================================
--- trunk/main/asterisk.c (original)
+++ trunk/main/asterisk.c Thu Jan  4 17:18:36 2007
@@ -2686,6 +2686,9 @@
 		printf(term_quit());
 		exit(1);
 	}
+
+	threadstorage_init();
+
 	if (load_modules(1)) {		/* Load modules */
 		printf(term_quit());
 		exit(1);



More information about the asterisk-commits mailing list