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