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