13b1e007c6ba6b2bd11cf1d8da77a24463a8ee66
[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 #ifdef SOLARIS
64 #define THREADSTORAGE_ONCE_INIT {PTHREAD_ONCE_INIT}
65 #else
66 #define THREADSTORAGE_ONCE_INIT PTHREAD_ONCE_INIT
67 #endif
68
69 #if defined(DEBUG_THREADLOCALS)
70 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line);
71 void __ast_threadstorage_object_remove(void *key);
72 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len);
73 #endif /* defined(DEBUG_THREADLOCALS) */
74
75 /*!
76  * \brief Define a thread storage variable
77  *
78  * \param name The name of the thread storage object
79  *
80  * This macro would be used to declare an instance of thread storage in a file.
81  *
82  * Example usage:
83  * \code
84  * AST_THREADSTORAGE(my_buf);
85  * \endcode
86  */
87 #define AST_THREADSTORAGE(name) \
88         AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, ast_free_ptr, static) 
89 #define AST_THREADSTORAGE_PUBLIC(name) \
90         AST_THREADSTORAGE_CUSTOM_SCOPE(name, NULL, ast_free_ptr,) 
91 #define AST_THREADSTORAGE_EXTERNAL(name) \
92         extern struct ast_threadstorage name
93
94 /*!
95  * \brief Define a thread storage variable, with custom initialization and cleanup
96  *
97  * \param name The name of the thread storage object
98  * \param init This is a custom function that will be called after each thread specific
99  *           object is allocated, with the allocated block of memory passed
100  *           as the argument.
101  * \param cleanup This is a custom function that will be called instead of ast_free
102  *              when the thread goes away.  Note that if this is used, it *MUST*
103  *              call free on the allocated memory.
104  *
105  * Example usage:
106  * \code
107  * AST_THREADSTORAGE_CUSTOM(my_buf, my_init, my_cleanup);
108  * \endcode
109  */
110 #define AST_THREADSTORAGE_CUSTOM(a,b,c) AST_THREADSTORAGE_CUSTOM_SCOPE(a,b,c,static)
111
112 #if !defined(DEBUG_THREADLOCALS)
113 #define AST_THREADSTORAGE_CUSTOM_SCOPE(name, c_init, c_cleanup, scope)  \
114 static void __init_##name(void);                                \
115 scope struct ast_threadstorage name = {                 \
116         .once = THREADSTORAGE_ONCE_INIT,                        \
117         .key_init = __init_##name,                              \
118         .custom_init = c_init,                                  \
119 };                                                              \
120 static void __init_##name(void)                                 \
121 {                                                               \
122         pthread_key_create(&(name).key, c_cleanup);             \
123 }
124 #else /* defined(DEBUG_THREADLOCALS) */
125 #define AST_THREADSTORAGE_CUSTOM_SCOPE(name, c_init, c_cleanup, scope)  \
126 static void __init_##name(void);                                \
127 scope struct ast_threadstorage name = {                 \
128         .once = THREADSTORAGE_ONCE_INIT,                        \
129         .key_init = __init_##name,                              \
130         .custom_init = c_init,                                  \
131 };                                                              \
132 static void __cleanup_##name(void *data)                        \
133 {                                                               \
134         __ast_threadstorage_object_remove(data);                \
135         c_cleanup(data);                                        \
136 }                                                               \
137 static void __init_##name(void)                                 \
138 {                                                               \
139         pthread_key_create(&(name).key, __cleanup_##name);      \
140 }
141 #endif /* defined(DEBUG_THREADLOCALS) */
142
143 /*!
144  * \brief Retrieve thread storage
145  *
146  * \param ts This is a pointer to the thread storage structure declared by using
147  *      the AST_THREADSTORAGE macro.  If declared with 
148  *      AST_THREADSTORAGE(my_buf), then this argument would be (&my_buf).
149  * \param init_size This is the amount of space to be allocated the first time
150  *      this thread requests its data. Thus, this should be the size that the
151  *      code accessing this thread storage is assuming the size to be.
152  *
153  * \return This function will return the thread local storage associated with
154  *         the thread storage management variable passed as the first argument.
155  *         The result will be NULL in the case of a memory allocation error.
156  *
157  * Example usage:
158  * \code
159  * AST_THREADSTORAGE(my_buf);
160  * #define MY_BUF_SIZE   128
161  * ...
162  * void my_func(const char *fmt, ...)
163  * {
164  *      void *buf;
165  *
166  *      if (!(buf = ast_threadstorage_get(&my_buf, MY_BUF_SIZE)))
167  *           return;
168  *      ...
169  * }
170  * \endcode
171  */
172 #if !defined(DEBUG_THREADLOCALS)
173 AST_INLINE_API(
174 void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size),
175 {
176         void *buf;
177
178         pthread_once(&ts->once, ts->key_init);
179         if (!(buf = pthread_getspecific(ts->key))) {
180                 if (!(buf = ast_calloc(1, init_size)))
181                         return NULL;
182                 if (ts->custom_init && ts->custom_init(buf)) {
183                         free(buf);
184                         return NULL;
185                 }
186                 pthread_setspecific(ts->key, buf);
187         }
188
189         return buf;
190 }
191 )
192 #else /* defined(DEBUG_THREADLOCALS) */
193 AST_INLINE_API(
194 void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, const char *file, const char *function, unsigned int line),
195 {
196         void *buf;
197
198         pthread_once(&ts->once, ts->key_init);
199         if (!(buf = pthread_getspecific(ts->key))) {
200                 if (!(buf = ast_calloc(1, init_size)))
201                         return NULL;
202                 if (ts->custom_init && ts->custom_init(buf)) {
203                         free(buf);
204                         return NULL;
205                 }
206                 pthread_setspecific(ts->key, buf);
207                 __ast_threadstorage_object_add(buf, init_size, file, function, line);
208         }
209
210         return buf;
211 }
212 )
213
214 #define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
215 #endif /* defined(DEBUG_THREADLOCALS) */
216
217 #endif /* ASTERISK_THREADSTORAGE_H */