Merged revisions 177701 via svnmerge from
[asterisk/asterisk.git] / include / asterisk / threadstorage.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file threadstorage.h
21  * \author Russell Bryant <russell@digium.com>
22  * \brief Definitions to aid in the use of thread local storage
23  *
24  * \arg \ref AstThreadStorage
25  */
26
27 /*!
28  * \page AstThreadStorage The Asterisk Thread Storage API
29  *
30  *
31  * The POSIX threads (pthreads) API provides the ability to define thread
32  * specific data.  The functions and structures defined here are intended
33  * to centralize the code that is commonly used when using thread local
34  * storage.
35  *
36  * The motivation for using this code in Asterisk is for situations where
37  * storing data on a thread-specific basis can provide some amount of
38  * performance benefit.  For example, there are some call types in Asterisk
39  * where ast_frame structures must be allocated very rapidly (easily 50, 100,
40  * 200 times a second).  Instead of doing the equivalent of that many calls
41  * to malloc() and free() per second, thread local storage is used to keep a
42  * list of unused frame structures so that they can be continuously reused.
43  *
44  * - \ref threadstorage.h
45  */
46
47 #ifndef ASTERISK_THREADSTORAGE_H
48 #define ASTERISK_THREADSTORAGE_H
49
50 #include "asterisk/utils.h"
51 #include "asterisk/inline_api.h"
52
53 /*!
54  * \brief data for a thread locally stored variable
55  */
56 struct ast_threadstorage {
57         pthread_once_t once;    /*!< Ensure that the key is only initialized by one thread */
58         pthread_key_t key;      /*!< The key used to retrieve this thread's data */
59         void (*key_init)(void); /*!< The function that initializes the key */
60         int (*custom_init)(void *); /*!< Custom initialization function specific to the object */
61 };
62
63 #if defined(DEBUG_THREADLOCALS)
64 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line);
65 void __ast_threadstorage_object_remove(void *key);
66 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len);
67 #endif /* defined(DEBUG_THREADLOCALS) */
68
69 /*!
70  * \brief Define a thread storage variable
71  *
72  * \param name The name of the thread storage object
73  *
74  * This macro would be used to declare an instance of thread storage in a file.
75  *
76  * Example usage:
77  * \code
78  * AST_THREADSTORAGE(my_buf);
79  * \endcode
80  */
81 #define AST_THREADSTORAGE(name) \
82         AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, ast_free_ptr, static) 
83 #define AST_THREADSTORAGE_PUBLIC(name) \
84         AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, ast_free_ptr,) 
85 #define AST_THREADSTORAGE_EXTERNAL(name) \
86         extern struct ast_threadstorage name
87
88 /*!
89  * \brief Define a thread storage variable, with custom initialization and cleanup
90  *
91  * \param name The name of the thread storage object
92  * \param init This is a custom function that will be called after each thread specific
93  *           object is allocated, with the allocated block of memory passed
94  *           as the argument.
95  * \param cleanup This is a custom function that will be called instead of ast_free
96  *              when the thread goes away.  Note that if this is used, it *MUST*
97  *              call free on the allocated memory.
98  *
99  * Example usage:
100  * \code
101  * AST_THREADSTORAGE_CUSTOM(my_buf, my_init, my_cleanup);
102  * \endcode
103  */
104 #define AST_THREADSTORAGE_CUSTOM(a,b,c) AST_THREADSTORAGE_CUSTOM_SCOPE(a,b,c,static)
105
106 #if !defined(DEBUG_THREADLOCALS)
107 #define AST_THREADSTORAGE_CUSTOM_SCOPE(name, c_init, c_cleanup, scope)  \
108 static void __init_##name(void);                \
109 scope struct ast_threadstorage name = {         \
110         .once = PTHREAD_ONCE_INIT,                  \
111         .key_init = __init_##name,                  \
112         .custom_init = c_init,                      \
113 };                                              \
114 static void __init_##name(void)                 \
115 {                                               \
116         pthread_key_create(&(name).key, c_cleanup); \
117 }
118 #else /* defined(DEBUG_THREADLOCALS) */
119 #define AST_THREADSTORAGE_CUSTOM_SCOPE(name, c_init, c_cleanup, scope) \
120 static void __init_##name(void);                \
121 scope struct ast_threadstorage name = {         \
122         .once = PTHREAD_ONCE_INIT,                  \
123         .key_init = __init_##name,                  \
124         .custom_init = c_init,                      \
125 };                                              \
126 static void __cleanup_##name(void *data)        \
127 {                                               \
128         __ast_threadstorage_object_remove(data);    \
129         c_cleanup(data);                            \
130 }                                               \
131 static void __init_##name(void)                 \
132 {                                               \
133         pthread_key_create(&(name).key, __cleanup_##name); \
134 }
135 #endif /* defined(DEBUG_THREADLOCALS) */
136
137 /*!
138  * \brief Retrieve thread storage
139  *
140  * \param ts This is a pointer to the thread storage structure declared by using
141  *      the AST_THREADSTORAGE macro.  If declared with 
142  *      AST_THREADSTORAGE(my_buf), then this argument would be (&my_buf).
143  * \param init_size This is the amount of space to be allocated the first time
144  *      this thread requests its data. Thus, this should be the size that the
145  *      code accessing this thread storage is assuming the size to be.
146  *
147  * \return This function will return the thread local storage associated with
148  *         the thread storage management variable passed as the first argument.
149  *         The result will be NULL in the case of a memory allocation error.
150  *
151  * Example usage:
152  * \code
153  * AST_THREADSTORAGE(my_buf);
154  * #define MY_BUF_SIZE   128
155  * ...
156  * void my_func(const char *fmt, ...)
157  * {
158  *      void *buf;
159  *
160  *      if (!(buf = ast_threadstorage_get(&my_buf, MY_BUF_SIZE)))
161  *           return;
162  *      ...
163  * }
164  * \endcode
165  */
166 #if !defined(DEBUG_THREADLOCALS)
167 AST_INLINE_API(
168 void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size),
169 {
170         void *buf;
171
172         pthread_once(&ts->once, ts->key_init);
173         if (!(buf = pthread_getspecific(ts->key))) {
174                 if (!(buf = ast_calloc(1, init_size)))
175                         return NULL;
176                 if (ts->custom_init && ts->custom_init(buf)) {
177                         free(buf);
178                         return NULL;
179                 }
180                 pthread_setspecific(ts->key, buf);
181         }
182
183         return buf;
184 }
185 )
186 #else /* defined(DEBUG_THREADLOCALS) */
187 AST_INLINE_API(
188 void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, const char *file, const char *function, unsigned int line),
189 {
190         void *buf;
191
192         pthread_once(&ts->once, ts->key_init);
193         if (!(buf = pthread_getspecific(ts->key))) {
194                 if (!(buf = ast_calloc(1, init_size)))
195                         return NULL;
196                 if (ts->custom_init && ts->custom_init(buf)) {
197                         free(buf);
198                         return NULL;
199                 }
200                 pthread_setspecific(ts->key, buf);
201                 __ast_threadstorage_object_add(buf, init_size, file, function, line);
202         }
203
204         return buf;
205 }
206 )
207
208 #define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
209 #endif /* defined(DEBUG_THREADLOCALS) */
210
211 #endif /* ASTERISK_THREADSTORAGE_H */