00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef ASTERISK_THREADSTORAGE_H
00048 #define ASTERISK_THREADSTORAGE_H
00049
00050 #include "asterisk/utils.h"
00051 #include "asterisk/inline_api.h"
00052
00053
00054
00055
00056 struct ast_threadstorage {
00057 pthread_once_t once;
00058 pthread_key_t key;
00059 void (*key_init)(void);
00060 int (*custom_init)(void *);
00061 };
00062
00063 #if defined(DEBUG_THREADLOCALS)
00064 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line);
00065 void __ast_threadstorage_object_remove(void *key);
00066 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len);
00067 #endif
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 #define AST_THREADSTORAGE(name) \
00082 AST_THREADSTORAGE_CUSTOM(name, NULL, ast_free_ptr)
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 #if defined(PTHREAD_ONCE_INIT_NEEDS_BRACES)
00101 # define AST_PTHREAD_ONCE_INIT { PTHREAD_ONCE_INIT }
00102 #else
00103 # define AST_PTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
00104 #endif
00105
00106 #if !defined(DEBUG_THREADLOCALS)
00107 #define AST_THREADSTORAGE_CUSTOM(name, c_init, c_cleanup) \
00108 static void __init_##name(void); \
00109 static struct ast_threadstorage name = { \
00110 .once = AST_PTHREAD_ONCE_INIT, \
00111 .key_init = __init_##name, \
00112 .custom_init = c_init, \
00113 }; \
00114 static void __init_##name(void) \
00115 { \
00116 pthread_key_create(&(name).key, c_cleanup); \
00117 }
00118 #else
00119 #define AST_THREADSTORAGE_CUSTOM_SCOPE(name, c_init, c_cleanup, scope) \
00120 static void __init_##name(void); \
00121 static struct ast_threadstorage name = { \
00122 .once = AST_PTHREAD_ONCE_INIT, \
00123 .key_init = __init_##name, \
00124 .custom_init = c_init, \
00125 }; \
00126 static void __cleanup_##name(void *data) \
00127 { \
00128 __ast_threadstorage_object_remove(data); \
00129 c_cleanup(data); \
00130 } \
00131 static void __init_##name(void) \
00132 { \
00133 pthread_key_create(&(name).key, __cleanup_##name); \
00134 }
00135 #endif
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 #if !defined(DEBUG_THREADLOCALS)
00167 AST_INLINE_API(
00168 void *ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size),
00169 {
00170 void *buf;
00171
00172 pthread_once(&ts->once, ts->key_init);
00173 if (!(buf = pthread_getspecific(ts->key))) {
00174 if (!(buf = ast_calloc(1, init_size)))
00175 return NULL;
00176 if (ts->custom_init && ts->custom_init(buf)) {
00177 free(buf);
00178 return NULL;
00179 }
00180 pthread_setspecific(ts->key, buf);
00181 }
00182
00183 return buf;
00184 }
00185 )
00186 #else
00187 AST_INLINE_API(
00188 void *__ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size, const char *file, const char *function, unsigned int line),
00189 {
00190 void *buf;
00191
00192 pthread_once(&ts->once, ts->key_init);
00193 if (!(buf = pthread_getspecific(ts->key))) {
00194 if (!(buf = ast_calloc(1, init_size)))
00195 return NULL;
00196 if (ts->custom_init && ts->custom_init(buf)) {
00197 free(buf);
00198 return NULL;
00199 }
00200 pthread_setspecific(ts->key, buf);
00201 __ast_threadstorage_object_add(buf, init_size, file, function, line);
00202 }
00203
00204 return buf;
00205 }
00206 )
00207
00208 #define ast_threadstorage_get(ts, init_size) __ast_threadstorage_get(ts, init_size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00209 #endif
00210
00211 #endif