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