00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00018 #ifndef LIBCWD_PRIVATE_STRUCT_TSD_H
00019 #define LIBCWD_PRIVATE_STRUCT_TSD_H
00020
00021 #ifndef LIBCWD_CONFIG_H
00022 #include <libcwd/config.h>
00023 #endif
00024 #ifndef LIBCWD_PRIVATE_ASSERT_H
00025 #include <libcwd/private_assert.h>
00026 #endif
00027 #ifndef LIBCWD_PRIVATE_MUTEX_INSTANCES_H
00028 #include <libcwd/private_mutex_instances.h>
00029 #endif
00030 #ifndef LIBCW_CSTRING
00031 #define LIBCW_CSTRING
00032 #include <cstring>
00033 #endif
00034 #ifndef LIBCW_LIMITS_H
00035 #define LIBCW_LIMITS_H
00036 #include <limits.h>
00037 #endif
00038 #if LIBCWD_THREAD_SAFE
00039 #include <libcwd/private_mutex.h>
00040 #ifdef LIBCWD_HAVE_PTHREAD
00041 #ifndef LIBCW_PTHREAD_H
00042 #define LIBCW_PTHREAD_H
00043 #include <pthread.h>
00044 #endif
00045 #endif
00046 #endif
00047
00048 namespace libcwd {
00049 namespace _private_ {
00050 struct TSD_st;
00051 }
00052 }
00053
00054
00055
00056
00057
00058 #if LIBCWD_THREAD_SAFE
00059
00060 #define LIBCWD_TSD __libcwd_tsd // Optional `__libcwd_tsd' parameter (foo() or foo(__libcwd_tsd)).
00061 #define LIBCWD_COMMA_TSD , LIBCWD_TSD // Idem, but as second or higher parameter.
00062 #define LIBCWD_TSD_PARAM ::libcwd::_private_::TSD_st& __libcwd_tsd
00063
00064 #define LIBCWD_COMMA_TSD_PARAM , LIBCWD_TSD_PARAM // Idem, but as second or higher parameter.
00065 #define LIBCWD_TSD_INSTANCE ::libcwd::_private_::TSD_st::instance()
00066
00067 #define LIBCWD_COMMA_TSD_INSTANCE , LIBCWD_TSD_INSTANCE // Idem, but as second or higher parameter.
00068 #define LIBCWD_TSD_DECLARATION ::libcwd::_private_::TSD_st& __libcwd_tsd(::libcwd::_private_::TSD_st::instance())
00069
00070 #define LIBCWD_DO_TSD(debug_object) (*__libcwd_tsd.do_array[(debug_object).WNS_index])
00071
00072 #define LIBCWD_TSD_MEMBER_OFF (__libcwd_tsd.do_off_array[WNS_index])
00073
00074 #define LIBCWD_DO_TSD_MEMBER_OFF(debug_object) (__libcwd_tsd.do_off_array[(debug_object).WNS_index])
00075
00076
00077 #else // !LIBCWD_THREAD_SAFE
00078
00079 #define LIBCWD_TSD
00080 #define LIBCWD_COMMA_TSD
00081 #define LIBCWD_TSD_PARAM void
00082 #define LIBCWD_COMMA_TSD_PARAM
00083 #define LIBCWD_TSD_INSTANCE
00084 #define LIBCWD_COMMA_TSD_INSTANCE
00085 #define LIBCWD_TSD_DECLARATION
00086 #define LIBCWD_DO_TSD(debug_object) ((debug_object).tsd)
00087 #define LIBCWD_TSD_MEMBER_OFF (tsd._off)
00088 #define LIBCWD_DO_TSD_MEMBER_OFF(debug_object) ((debug_object).tsd._off)
00089
00090 #endif // !LIBCWD_THREAD_SAFE
00091
00092 #define LIBCWD_DO_TSD_MEMBER(debug_object, m) (LIBCWD_DO_TSD(debug_object).m)
00093 #define LIBCWD_TSD_MEMBER(m) LIBCWD_DO_TSD_MEMBER(*this, m)
00094
00095
00096 #ifndef LIBCWD_STRUCT_DEBUG_TSD_H
00097 #include <libcwd/struct_debug_tsd.h>
00098 #endif
00099 #if LIBCWD_THREAD_SAFE
00100 #ifndef LIBCWD_PRIVATE_THREAD_H
00101 #include <libcwd/private_thread.h>
00102 #endif
00103 #endif
00104
00105 namespace libcwd {
00106
00107 #if CWDEBUG_LOCATION
00108
00116 typedef unsigned short int location_format_t;
00117
00119 #endif
00120
00121 namespace _private_ {
00122
00123 extern int WST_initializing_TSD;
00124 class thread_ct;
00125
00126 struct TSD_st {
00127 public:
00128 #if CWDEBUG_ALLOC
00129 int internal;
00130 int library_call;
00131 int inside_malloc_or_free;
00132 int invisible;
00133 #endif // CWDEBUG_ALLOC
00134 #if CWDEBUG_LOCATION
00135 location_format_t format;
00136 #endif
00137 #if LIBCWD_THREAD_SAFE
00138 threadlist_t::iterator thread_iter;
00139 bool thread_iter_valid;
00140 thread_ct* target_thread;
00141 int terminating;
00142 bool pthread_lock_interface_is_locked;
00143 bool list_allocations_on_show_allthreads;
00144 int inside_free;
00145 #endif
00146 #if CWDEBUG_DEBUGM
00147 int marker;
00148 #if CWDEBUG_MAGIC
00149 int annotation;
00150 #endif
00151 #endif
00152 bool recursive_fatal;
00153 #if CWDEBUG_DEBUG
00154 bool recursive_assert;
00155 #endif
00156 #if CWDEBUG_DEBUGT
00157 int cancel_explicitely_deferred;
00158 int cancel_explicitely_disabled;
00159 int inside_critical_area;
00160 int cleanup_handler_installed;
00161 int internal_debugging_code;
00162 mutex_ct* waiting_for_mutex;
00163 int waiting_for_lock;
00164 int waiting_for_rdlock;
00165 int instance_rdlocked[instance_rdlocked_size];
00166 pthread_t rdlocked_by1[instance_rdlocked_size];
00167 pthread_t rdlocked_by2[instance_rdlocked_size];
00168 void const* rdlocked_from1[instance_rdlocked_size];
00169 void const* rdlocked_from2[instance_rdlocked_size];
00170 #endif
00171 #if LIBCWD_THREAD_SAFE
00172 pthread_t tid;
00173 pid_t pid;
00174 int do_off_array[LIBCWD_DO_MAX];
00175 debug_tsd_st* do_array[LIBCWD_DO_MAX];
00176 void cleanup_routine(void);
00177 int off_cnt_array[LIBCWD_DC_MAX];
00178 private:
00179 int tsd_destructor_count;
00180 #endif
00181
00182 public:
00183 void thread_destructed(void);
00184
00185 #if LIBCWD_THREAD_SAFE
00186
00187
00188 private:
00189 static TSD_st& S_create(int from_free);
00190 static pthread_key_t S_tsd_key;
00191 static pthread_once_t S_tsd_key_once;
00192 static void S_tsd_key_alloc(void);
00193 static void S_cleanup_routine(void* arg);
00194
00195 public:
00196 static TSD_st& instance(void);
00197 static TSD_st& instance_free(void);
00198 static void free_instance(TSD_st&);
00199 #endif // LIBCWD_THREAD_SAFE
00200 };
00201
00202
00203
00204
00205 #if !LIBCWD_THREAD_SAFE
00206
00207
00208
00209 extern TSD_st __libcwd_tsd;
00210 #else
00211 extern bool WST_tsd_key_created;
00212
00213 inline
00214 TSD_st& TSD_st::instance(void)
00215 {
00216 TSD_st* instance;
00217 if (!WST_tsd_key_created || !(instance = (TSD_st*)pthread_getspecific(S_tsd_key)))
00218 return S_create(0);
00219 return *instance;
00220 }
00221
00222
00223 inline
00224 TSD_st& TSD_st::instance_free(void)
00225 {
00226 TSD_st* instance;
00227 if (!WST_tsd_key_created || !(instance = (TSD_st*)pthread_getspecific(S_tsd_key)))
00228 return S_create(1);
00229 else
00230 instance->inside_free++;
00231 return *instance;
00232 }
00233 #endif
00234
00235 }
00236 }
00237
00238 #if !LIBCWD_THREAD_SAFE
00239
00240
00241 using ::libcwd::_private_::__libcwd_tsd;
00242 #endif
00243
00244 #endif // LIBCWD_PRIVATE_STRUCT_TSD_H