00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CU_THREADLOCAL_H
00019 #define CU_THREADLOCAL_H
00020
00021 #include <cu/fwd.h>
00022 #include <cu/conf.h>
00023
00024 CU_BEGIN_DECLARATIONS
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 #ifdef CU_IN_DOXYGEN
00086
00087 # define CU_THREADLOCAL_DECL(name, private_name) \
00088 \
00089 name##_t name(void)
00090 # define CU_THREADLOCAL_DEF(name, private_name, static_name) ...
00091 # define CU_THREADLOCAL_INIT(name, private_name, static_name) ...
00092
00093 #elif defined(CUCONF_HAVE_THREAD_KEYWORD)
00094
00095 # define CU_THREADLOCAL_DECL(name, private_name) \
00096 extern __thread struct name private_name##_inst; \
00097 \
00098 \
00099 CU_SINLINE name##_t name(void) \
00100 { return &private_name##_inst; } \
00101 \
00102 CU_END_BOILERPLATE
00103
00104 # define CU_THREADLOCAL_DEF(name, private_name, static_name) \
00105 __thread struct name private_name##_inst; \
00106 \
00107 cu_clop_def0(static_name##_on_thread_entry, void) \
00108 { \
00109 name##_t tls = name(); \
00110 GC_add_roots((char *)tls, (char *)(tls + 1)); \
00111 static_name##_init(tls); \
00112 } \
00113 \
00114 cu_clop_def0(static_name##_on_thread_exit, void) \
00115 { \
00116 name##_t tls = name(); \
00117 static_name##_destruct(tls); \
00118 GC_remove_roots((char *)tls, (char *)(tls + 1)); \
00119 } \
00120 \
00121 CU_END_BOILERPLATE
00122
00123 # define CU_THREADLOCAL_INIT(name, private_name, static_name) \
00124 cu_register_thread_init(cu_clop_ref(static_name##_on_thread_entry), \
00125 cu_clop_ref(static_name##_on_thread_exit))
00126
00127 #else
00128
00129 # define CU_THREADLOCAL_DECL(name, private_name) \
00130 extern pthread_key_t private_name##_key; \
00131 \
00132 \
00133 CU_SINLINE name##_t name(void) \
00134 { return (name##_t)pthread_getspecific(private_name##_key); } \
00135 \
00136 CU_END_BOILERPLATE
00137
00138 # define CU_THREADLOCAL_DEF(name, private_name, static_name) \
00139 pthread_key_t private_name##_key; \
00140 \
00141 cu_clop_def0(static_name##_on_thread_entry, void) \
00142 { \
00143 int err_code; \
00144 name##_t tls = cu_unew(struct name); \
00145 static_name##_init(tls); \
00146 err_code = pthread_setspecific(private_name##_key, tls); \
00147 if (err_code) \
00148 cu_handle_syserror(err_code, "pthread_setspecific"); \
00149 } \
00150 \
00151 void static_name##_on_thread_exit(void *tls) \
00152 { \
00153 static_name##_destruct(tls); \
00154 cu_ufree(tls); \
00155 } \
00156 \
00157 CU_END_BOILERPLATE
00158
00159 # define CU_THREADLOCAL_INIT(name, private_name, static_name) \
00160 do { \
00161 cu_pthread_key_create(&private_name##_key, (void (*)(void *)) \
00162 CU_MARG(void (*)(name##_t), static_name##_destruct)); \
00163 cu_register_thread_init(cu_clop_ref(static_name##_on_thread_entry), \
00164 cu_clop_null); \
00165 } while (0)
00166
00167 #endif
00168
00169
00170 CU_END_DECLARATIONS
00171
00172 #endif