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 #ifndef LIBCWD_PRIVATE_THREAD_H
00100 #include <libcwd/private_thread.h>
00101 #endif
00102
00103 namespace libcwd {
00104
00105 #if CWDEBUG_LOCATION
00106
00114 typedef unsigned short int location_format_t;
00115
00117 #endif
00118
00119 namespace _private_ {
00120
00121 extern int WST_initializing_TSD;
00122 class thread_ct;
00123
00124 struct TSD_st {
00125 public:
00126 #if CWDEBUG_ALLOC
00127 int internal;
00128 int library_call;
00129 int inside_malloc_or_free;
00130 int invisible;
00131 #endif // CWDEBUG_ALLOC
00132 #if CWDEBUG_LOCATION
00133 location_format_t format;
00134 #endif
00135 #if LIBCWD_THREAD_SAFE
00136 threadlist_t::iterator thread_iter;
00137 bool thread_iter_valid;
00138 thread_ct* target_thread;
00139 int terminating;
00140 bool pthread_lock_interface_is_locked;
00141 bool list_allocations_on_show_allthreads;
00142 int inside_free;
00143 #endif
00144 #if CWDEBUG_DEBUGM
00145 int marker;
00146 #if CWDEBUG_MAGIC
00147 int annotation;
00148 #endif
00149 #endif
00150 bool recursive_fatal;
00151 #if CWDEBUG_DEBUG
00152 bool recursive_assert;
00153 #endif
00154 #if CWDEBUG_DEBUGT
00155 int cancel_explicitely_deferred;
00156 int cancel_explicitely_disabled;
00157 int inside_critical_area;
00158 int cleanup_handler_installed;
00159 int internal_debugging_code;
00160 mutex_ct* waiting_for_mutex;
00161 int waiting_for_lock;
00162 int waiting_for_rdlock;
00163 int instance_rdlocked[instance_rdlocked_size];
00164 pthread_t rdlocked_by1[instance_rdlocked_size];
00165 pthread_t rdlocked_by2[instance_rdlocked_size];
00166 void const* rdlocked_from1[instance_rdlocked_size];
00167 void const* rdlocked_from2[instance_rdlocked_size];
00168 #endif
00169 #if LIBCWD_THREAD_SAFE
00170 pthread_t tid;
00171 pid_t pid;
00172 int do_off_array[LIBCWD_DO_MAX];
00173 debug_tsd_st* do_array[LIBCWD_DO_MAX];
00174 void cleanup_routine(void);
00175 int off_cnt_array[LIBCWD_DC_MAX];
00176 private:
00177 int tsd_destructor_count;
00178 #endif
00179
00180 public:
00181 void thread_destructed(void);
00182
00183 #if LIBCWD_THREAD_SAFE
00184
00185
00186 private:
00187 static TSD_st& S_create(int from_free);
00188 static pthread_key_t S_tsd_key;
00189 static pthread_once_t S_tsd_key_once;
00190 static void S_tsd_key_alloc(void);
00191 static void S_cleanup_routine(void* arg);
00192
00193 public:
00194 static TSD_st& instance(void);
00195 static TSD_st& instance_free(void);
00196 static void free_instance(TSD_st&);
00197 #endif // LIBCWD_THREAD_SAFE
00198 };
00199
00200
00201
00202
00203 #if !LIBCWD_THREAD_SAFE
00204
00205
00206
00207 extern TSD_st __libcwd_tsd;
00208 #else
00209 extern bool WST_tsd_key_created;
00210
00211 inline
00212 TSD_st& TSD_st::instance(void)
00213 {
00214 TSD_st* instance;
00215 if (!WST_tsd_key_created || !(instance = (TSD_st*)pthread_getspecific(S_tsd_key)))
00216 return S_create(0);
00217 return *instance;
00218 }
00219
00220
00221 inline
00222 TSD_st& TSD_st::instance_free(void)
00223 {
00224 TSD_st* instance;
00225 if (!WST_tsd_key_created || !(instance = (TSD_st*)pthread_getspecific(S_tsd_key)))
00226 return S_create(1);
00227 else
00228 instance->inside_free++;
00229 return *instance;
00230 }
00231 #endif
00232
00233 }
00234 }
00235
00236 #if !LIBCWD_THREAD_SAFE
00237
00238
00239 using ::libcwd::_private_::__libcwd_tsd;
00240 #endif
00241
00242 #endif // LIBCWD_PRIVATE_STRUCT_TSD_H