![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
threadstorage.h
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 2006, Digium, Inc. 00005 * 00006 * Russell Bryant <russell@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! 00020 * \file threadstorage.h 00021 * \author Russell Bryant <russell@digium.com> 00022 * \brief Definitions to aid in the use of thread local storage 00023 * 00024 * \arg \ref AstThreadStorage 00025 */ 00026 00027 /*! 00028 * \page AstThreadStorage The Asterisk Thread Storage API 00029 * 00030 * 00031 * The POSIX threads (pthreads) API provides the ability to define thread 00032 * specific data. The functions and structures defined here are intended 00033 * to centralize the code that is commonly used when using thread local 00034 * storage. 00035 * 00036 * The motivation for using this code in Asterisk is for situations where 00037 * storing data on a thread-specific basis can provide some amount of 00038 * performance benefit. For example, there are some call types in Asterisk 00039 * where ast_frame structures must be allocated very rapidly (easily 50, 100, 00040 * 200 times a second). Instead of doing the equivalent of that many calls 00041 * to malloc() and free() per second, thread local storage is used to keep a 00042 * list of unused frame structures so that they can be continuously reused. 00043 * 00044 * - \ref threadstorage.h 00045 */ 00046 00047 #ifndef ASTERISK_THREADSTORAGE_H 00048 #define ASTERISK_THREADSTORAGE_H 00049 00050 #include <pthread.h> 00051 00052 #include "asterisk/utils.h" 00053 #include "asterisk/inline_api.h" 00054 00055 /*! 00056 * \brief data for a thread locally stored variable 00057 */ 00058 struct ast_threadstorage { 00059 pthread_once_t once; /*!< Ensure that the key is only initialized by one thread */ 00060 pthread_key_t key; /*!< The key used to retrieve this thread's data */ 00061 void (*key_init)(void); /*!< The function that initializes the key */ 00062 int (*custom_init)(void *); /*!< Custom initialization function specific to the object */ 00063 }; 00064 00065 #if defined(DEBUG_THREADLOCALS) 00066 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line); 00067 void __ast_threadstorage_object_remove(void *key); 00068 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len); 00069 #endif /* defined(DEBUG_THREADLOCALS) */ 00070 00071 /*! 00072 * \brief Define a thread storage variable 00073 * 00074 * \arg name The name of the thread storage object 00075 * 00076 * This macro would be used to declare an instance of thread storage in a file. 00077 * 00078 * Example usage: 00079 * \code 00080 * AST_THREADSTORAGE(my_buf); 00081 * \endcode 00082 */ 00083 #define AST_THREADSTORAGE(name) \ 00084 AST_THREADSTORAGE_CUSTOM(name, NULL, ast_free) 00085 00086 /*! 00087 * \brief Define a thread storage variable, with custom initialization and cleanup 00088 * 00089 * \arg name The name of the thread storage object 00090 * \arg init This is a custom function that will be called after each thread specific 00091 * object is allocated, with the allocated block of memory passed 00092 * as the argument. 00093 * \arg cleanup This is a custom function that will be called instead of ast_free 00094 * when the thread goes away. Note that if this is used, it *MUST* 00095 * call free on the allocated memory. 00096 * 00097 * Example usage: 00098 * \code 00099 * AST_THREADSTORAGE_CUSTOM(my_buf, my_init, my_cleanup); 00100 * \endcode 00101 */ 00102 #if !defined(DEBUG_THREADLOCALS) 00103 #define AST_THREADSTORAGE_CUSTOM(name, c_init, c_cleanup) \ 00104 static void __init_##name(void); \ 00105 static struct ast_threadstorage name = { \ 00106 .once = PTHREAD_ONCE_INIT, \ 00107 .key_init = __init_##name, \ 00108 .custom_init = c_init, \ 00109 }; \ 00110 static void __init_##name(void) \ 00111 { \ 00112 pthread_key_create(&(name).key, c_cleanup); \ 00113 } 00114 #else /* defined(DEBUG_THREADLOCALS) */ 00115 #define AST_THREADSTORAGE_CUSTOM(name, c_init, c_cleanup) \ 00116 static void __init_##name(void); \ 00117 static struct ast_threadstorage name = { \ 00118 .once = PTHREAD_ONCE_INIT, \ 00119 .key_init = __init_##name, \ 00120 .custom_init = c_init, \ 00121 }; \ 00122 static void __cleanup_##name(void *data) \ 00123 { \ 00124 __ast_threadstorage_object_remove(data); \ 00125 c_cleanup(data); \ 00126 } \ 00127 static void __init_##name(void) \ 00128 { \ 00129 pthread_key_create(&(name).key, __cleanup_##name); \ 00130 } 00131 #endif /* defined(DEBUG_THREADLOCALS) */ 00132 00133 /*! 00134 * \brief Retrieve thread storage 00135 * 00136 * \arg ts This is a pointer to the thread storage structure declared by using 00137 * the AST_THREADSTORAGE macro. If declared with 00138 * AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 00139 * (&my_buf). 00140 * \arg init_size This is the amount of space to be allocated the first time 00141 * this thread requests its data. Thus, this should be the size that the 00142 * code accessing this thread storage is assuming the size to be. 00143 * 00144 * \return This function will return the thread local storage associated with 00145 * the thread storage management variable passed as the first argument. 00146 * The result will be NULL in the case of a memory allocation error. 00147 * 00148 * Example usage: 00149 * \code 00150 * AST_THREADSTORAGE(my_buf, my_buf_init); 00151 * #define MY_BUF_SIZE 128 00152 * ... 00153 * void my_func(const char *fmt, ...) 00154 * { 00155 * void *buf; 00156 * 00157 * if (!(buf = ast_threadstorage_get(&my_buf, MY_BUF_SIZE))) 00158 * return; 00159 * ... 00160 * } 00161 * \endcode 00162 */ 00163 #if !defined(DEBUG_THREADLOCALS) 00164 AST_INLINE_API( 00165 void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size), 00166 { 00167 void *buf; 00168 00169 pthread_once(&ts->once, ts->key_init); 00170 if (!(buf = pthread_getspecific(ts->key))) { 00171 if (!(buf = ast_calloc(1, init_size))) 00172 return NULL; 00173 if (ts->custom_init && ts->custom_init(buf)) { 00174 free(buf); 00175 return NULL; 00176 } 00177 pthread_setspecific(ts->key, buf); 00178 } 00179 00180 return buf; 00181 } 00182 ) 00183 #else /* defined(DEBUG_THREADLOCALS) */ 00184 AST_INLINE_API( 00185 void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, const char *file, const char *function, unsigned int line), 00186 { 00187 void *buf; 00188 00189 pthread_once(&ts->once, ts->key_init); 00190 if (!(buf = pthread_getspecific(ts->key))) { 00191 if (!(buf = ast_calloc(1, init_size))) 00192 return NULL; 00193 if (ts->custom_init && ts->custom_init(buf)) { 00194 free(buf); 00195 return NULL; 00196 } 00197 pthread_setspecific(ts->key, buf); 00198 __ast_threadstorage_object_add(buf, init_size, file, function, line); 00199 } 00200 00201 return buf; 00202 } 00203 ) 00204 00205 #define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00206 #endif /* defined(DEBUG_THREADLOCALS) */ 00207 00208 #endif /* ASTERISK_THREADSTORAGE_H */