[asterisk-commits] russell: trunk r45623 - in /trunk: channels/ include/asterisk/ main/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Oct 18 18:00:58 MST 2006


Author: russell
Date: Wed Oct 18 20:00:57 2006
New Revision: 45623

URL: http://svn.digium.com/view/asterisk?rev=45623&view=rev
Log:
Extend the thread storage API such that a custom initialization function can
be called for each thread specific object after they are allocated.  Note that
there was already the ability to define a custom cleanup function.  Also, if
the custom cleanup function is used, it *MUST* call free on the thread
specific object at the end.  There is no way to have this magically done that
I can think of because the cleanup function registered with the pthread
implementation will only call the function back with a pointer to the
thread specific object, not the parent ast_threadstorage object.

Modified:
    trunk/channels/chan_sip.c
    trunk/channels/chan_skinny.c
    trunk/channels/iax2-parser.c
    trunk/include/asterisk/threadstorage.h
    trunk/main/channel.c
    trunk/main/cli.c
    trunk/main/frame.c
    trunk/main/logger.c
    trunk/main/manager.c
    trunk/main/utils.c

Modified: trunk/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_sip.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Wed Oct 18 20:00:57 2006
@@ -1143,7 +1143,7 @@
 } regl;
 
 /*! \brief A per-thread temporary pvt structure */
-AST_THREADSTORAGE(ts_temp_pvt, temp_pvt_init);
+AST_THREADSTORAGE(ts_temp_pvt);
 
 /*! \todo Move the sip_auth list to AST_LIST */
 static struct sip_auth *authl = NULL;		/*!< Authentication list for realm authentication */

Modified: trunk/channels/chan_skinny.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_skinny.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/channels/chan_skinny.c (original)
+++ trunk/channels/chan_skinny.c Wed Oct 18 20:00:57 2006
@@ -135,10 +135,10 @@
 };
 static struct ast_jb_conf global_jbconf;
 
-AST_THREADSTORAGE(device2str_threadbuf, device2str_threadbuf_init);
+AST_THREADSTORAGE(device2str_threadbuf);
 #define DEVICE2STR_BUFSIZE   15
 
-AST_THREADSTORAGE(control2str_threadbuf, control2str_threadbuf_init);
+AST_THREADSTORAGE(control2str_threadbuf);
 #define CONTROL2STR_BUFSIZE   100
 
 /*********************

Modified: trunk/channels/iax2-parser.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/iax2-parser.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/channels/iax2-parser.c (original)
+++ trunk/channels/iax2-parser.c Wed Oct 18 20:00:57 2006
@@ -53,7 +53,7 @@
 static void frame_cache_cleanup(void *data);
 
 /*! \brief A per-thread cache of iax_frame structures */
-AST_THREADSTORAGE_CUSTOM(frame_cache, frame_cache_init, frame_cache_cleanup);
+AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
 
 /*! \brief This is just so iax_frames, a list head struct for holding a list of
  *  iax_frame structures, is defined. */

Modified: trunk/include/asterisk/threadstorage.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/threadstorage.h?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/include/asterisk/threadstorage.h (original)
+++ trunk/include/asterisk/threadstorage.h Wed Oct 18 20:00:57 2006
@@ -41,35 +41,51 @@
 	pthread_key_t key;
 	/*! The function that initializes the key */
 	void (*key_init)(void);
+	/*! Custom initialization function specific to the object */
+	void (*custom_init)(void *);
 };
 
 /*!
  * \brief Define a thread storage variable
  *
- * \arg name The name of the thread storage
- * \arg name_init This is a name used to create the function that gets called
- *      to initialize this thread storage. It can be anything since it will not
- *      be referred to anywhere else
+ * \arg name The name of the thread storage object
  *
  * This macro would be used to declare an instance of thread storage in a file.
  *
  * Example usage:
  * \code
- * AST_THREADSTORAGE(my_buf, my_buf_init);
- * \endcode
- */
-#define AST_THREADSTORAGE(name, name_init) \
-	AST_THREADSTORAGE_CUSTOM(name, name_init, ast_free) 
-
-#define AST_THREADSTORAGE_CUSTOM(name, name_init, cleanup)  \
-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, cleanup);           \
+ * AST_THREADSTORAGE(my_buf);
+ * \endcode
+ */
+#define AST_THREADSTORAGE(name) \
+	AST_THREADSTORAGE_CUSTOM(name, NULL, NULL) 
+
+/*!
+ * \brief Define a thread storage variable, with custom initialization and cleanup
+ *
+ * \arg name The name of the thread storage object
+ * \arg init This is a custom that will be called after each thread specific
+ *           object is allocated, with the allocated block of memory passed
+ *           as the argument.
+ * \arg cleanup This is a custom function that will be called instead of ast_free
+ *              when the thread goes away.  Note that if this is used, it *MUST*
+ *              call free on the allocated memory.
+ *
+ * Example usage:
+ * \code
+ * AST_THREADSTORAGE(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);       \
 }
 
 /*!
@@ -111,12 +127,16 @@
 	if (!(buf = pthread_getspecific(ts->key))) {
 		if (!(buf = ast_calloc(1, init_size)))
 			return NULL;
+		if (ts->custom_init)
+			ts->custom_init(buf);
 		pthread_setspecific(ts->key, buf);
 	}
 
 	return buf;
 }
 )
+
+void __ast_threadstorage_cleanup(void *);
 
 /*!
  * \brief A dynamic length string

Modified: trunk/main/channel.c
URL: http://svn.digium.com/view/asterisk/trunk/main/channel.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/main/channel.c (original)
+++ trunk/main/channel.c Wed Oct 18 20:00:57 2006
@@ -100,7 +100,7 @@
 
 unsigned long global_fin = 0, global_fout = 0;
 
-AST_THREADSTORAGE(state2str_threadbuf, state2str_threadbuf_init);
+AST_THREADSTORAGE(state2str_threadbuf);
 #define STATE2STR_BUFSIZE   32
 
 /* XXX 100ms ... this won't work with wideband support */

Modified: trunk/main/cli.c
URL: http://svn.digium.com/view/asterisk/trunk/main/cli.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/main/cli.c (original)
+++ trunk/main/cli.c Wed Oct 18 20:00:57 2006
@@ -51,7 +51,7 @@
 
 extern unsigned long global_fin, global_fout;
 
-AST_THREADSTORAGE(ast_cli_buf, ast_cli_buf_init);
+AST_THREADSTORAGE(ast_cli_buf);
 
 /*! \brief Initial buffer size for resulting strings in ast_cli() */
 #define AST_CLI_INITLEN   256

Modified: trunk/main/frame.c
URL: http://svn.digium.com/view/asterisk/trunk/main/frame.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/main/frame.c (original)
+++ trunk/main/frame.c Wed Oct 18 20:00:57 2006
@@ -52,7 +52,7 @@
 static void frame_cache_cleanup(void *data);
 
 /*! \brief A per-thread cache of frame headers */
-AST_THREADSTORAGE_CUSTOM(frame_cache, frame_cache_init, frame_cache_cleanup);
+AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
 
 /*! 
  * \brief Maximum ast_frame cache size

Modified: trunk/main/logger.c
URL: http://svn.digium.com/view/asterisk/trunk/main/logger.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/main/logger.c (original)
+++ trunk/main/logger.c Wed Oct 18 20:00:57 2006
@@ -137,10 +137,10 @@
 	COLOR_BRGREEN
 };
 
-AST_THREADSTORAGE(verbose_buf, verbose_buf_init);
+AST_THREADSTORAGE(verbose_buf);
 #define VERBOSE_BUF_INIT_SIZE   128
 
-AST_THREADSTORAGE(log_buf, log_buf_init);
+AST_THREADSTORAGE(log_buf);
 #define LOG_BUF_INIT_SIZE       128
 
 static int make_components(char *s, int lineno)

Modified: trunk/main/manager.c
URL: http://svn.digium.com/view/asterisk/trunk/main/manager.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/main/manager.c (original)
+++ trunk/main/manager.c Wed Oct 18 20:00:57 2006
@@ -101,10 +101,10 @@
  * has one event in it (Placeholder) in init_manager().
  */
 
-AST_THREADSTORAGE(manager_event_buf, manager_event_buf_init);
+AST_THREADSTORAGE(manager_event_buf);
 #define MANAGER_EVENT_BUF_INITSIZE   256
 
-AST_THREADSTORAGE(astman_append_buf, astman_append_buf_init);
+AST_THREADSTORAGE(astman_append_buf);
 #define ASTMAN_APPEND_BUF_INITSIZE   256
 
 /*! \brief Descriptor for an AMI session, either a regular one

Modified: trunk/main/utils.c
URL: http://svn.digium.com/view/asterisk/trunk/main/utils.c?rev=45623&r1=45622&r2=45623&view=diff
==============================================================================
--- trunk/main/utils.c (original)
+++ trunk/main/utils.c Wed Oct 18 20:00:57 2006
@@ -65,7 +65,7 @@
 static char base64[64];
 static char b2a[256];
 
-AST_THREADSTORAGE(inet_ntoa_buf, inet_ntoa_buf_init);
+AST_THREADSTORAGE(inet_ntoa_buf);
 
 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
 



More information about the asterisk-commits mailing list