Main Page   Reference Manual   Namespace List   Compound List   Namespace Members   Compound Members   File Members  

private_struct_TSD.h
Go to the documentation of this file.
1 // $Header$
2 //
3 // Copyright (C) 2001 - 2004, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 
18 #ifndef LIBCWD_PRIVATE_STRUCT_TSD_H
19 #define LIBCWD_PRIVATE_STRUCT_TSD_H
20 
21 #ifndef LIBCWD_CONFIG_H
22 #include "config.h"
23 #endif
24 #ifndef LIBCWD_PRIVATE_ASSERT_H
25 #include "private_assert.h"
26 #endif
27 #ifndef LIBCWD_PRIVATE_MUTEX_INSTANCES_H
29 #endif
30 #ifndef LIBCW_CSTRING
31 #define LIBCW_CSTRING
32 #include <cstring> // Needed for std::memset.
33 #endif
34 #ifndef LIBCW_LIMITS_H
35 #define LIBCW_LIMITS_H
36 #include <limits.h> // For PTHREAD_THREADS_MAX
37 #endif
38 #if LIBCWD_THREAD_SAFE
39 #include "private_mutex.h"
40 #ifdef LIBCWD_HAVE_PTHREAD
41 #ifndef LIBCW_PTHREAD_H
42 #define LIBCW_PTHREAD_H
43 #include <pthread.h>
44 #endif
45 #endif
46 #endif
47 
48 namespace libcwd {
49  namespace _private_ {
50  struct TSD_st;
51  } // namespace _private_
52 } // namespace libcwd
53 
54 // When LIBCWD_THREAD_SAFE is set then `__libcwd_tsd' is a local variable
55 // (see LIBCWD_TSD_DECLARATION) or function parameter (LIBCWD_TSD_PARAM and LIBCWD_COMMA_TSD_PARAM).
56 // This approach means that many function signatures are different because with thread support a
57 // `__libcwd_tsd' reference needs to be passed. We use several helper macros for this:
58 #if LIBCWD_THREAD_SAFE
59 
60 #define LIBCWD_TSD __libcwd_tsd // Optional `__libcwd_tsd' parameter (foo() or foo(__libcwd_tsd)).
61 #define LIBCWD_COMMA_TSD , LIBCWD_TSD // Idem, but as second or higher parameter.
62 #define LIBCWD_TSD_PARAM ::libcwd::_private_::TSD_st& __libcwd_tsd
63  // Optional function parameter (foo() or foo(TSD_st& __libcwd_tsd)).
64 #define LIBCWD_TSD_PARAM_UNUSED ::libcwd::_private_::TSD_st&
65  // Same without parameter.
66 #define LIBCWD_COMMA_TSD_PARAM , LIBCWD_TSD_PARAM // Idem, but as second or higher parameter.
67 #define LIBCWD_COMMA_TSD_PARAM_UNUSED , LIBCWD_TSD_PARAM_UNUSED
68  // Idem, without parameter.
69 #define LIBCWD_TSD_INSTANCE ::libcwd::_private_::TSD_st::instance()
70  // For directly passing the `__libcwd_tsd' instance to a function (foo(TSD::instance())).
71 #define LIBCWD_COMMA_TSD_INSTANCE , LIBCWD_TSD_INSTANCE // Idem, but as second or higher parameter.
72 #define LIBCWD_TSD_DECLARATION ::libcwd::_private_::TSD_st& __libcwd_tsd(::libcwd::_private_::TSD_st::instance())
73  // Declaration of local `__libcwd_tsd' structure reference.
74 #define LIBCWD_DO_TSD(debug_object) (*__libcwd_tsd.do_array[(debug_object).WNS_index])
75  // For use inside class debug_ct to access member `m'.
76 #define LIBCWD_TSD_MEMBER_OFF (__libcwd_tsd.do_off_array[WNS_index])
77  // For use inside class debug_ct to access member `_off'.
78 #define LIBCWD_DO_TSD_MEMBER_OFF(debug_object) (__libcwd_tsd.do_off_array[(debug_object).WNS_index])
79  // To access member _off of debug object.
80 
81 #else // !LIBCWD_THREAD_SAFE
82 
83 #define LIBCWD_TSD
84 #define LIBCWD_COMMA_TSD
85 #define LIBCWD_TSD_PARAM void
86 #define LIBCWD_TSD_PARAM_UNUSED void
87 #define LIBCWD_COMMA_TSD_PARAM
88 #define LIBCWD_COMMA_TSD_PARAM_UNUSED
89 #define LIBCWD_TSD_INSTANCE
90 #define LIBCWD_COMMA_TSD_INSTANCE
91 #define LIBCWD_TSD_DECLARATION
92 #define LIBCWD_DO_TSD(debug_object) ((debug_object).tsd)
93 #define LIBCWD_TSD_MEMBER_OFF (tsd._off)
94 #define LIBCWD_DO_TSD_MEMBER_OFF(debug_object) ((debug_object).tsd._off)
95 
96 #endif // !LIBCWD_THREAD_SAFE
97 
98 #define LIBCWD_DO_TSD_MEMBER(debug_object, m) (LIBCWD_DO_TSD(debug_object).m)
99 #define LIBCWD_TSD_MEMBER(m) LIBCWD_DO_TSD_MEMBER(*this, m)
100 
101 // These includes use the above macros.
102 #ifndef LIBCWD_STRUCT_DEBUG_TSD_H
103 #include "struct_debug_tsd.h"
104 #endif
105 #if LIBCWD_THREAD_SAFE
106 #ifndef LIBCWD_PRIVATE_THREAD_H
107 #include "private_thread.h"
108 #endif
109 #endif
110 
111 namespace libcwd {
112 
113 #if CWDEBUG_LOCATION
122 typedef unsigned short int location_format_t;
123  // End of group 'group_locations'
125 #endif
126 
127  namespace _private_ {
128 
129 extern int WST_initializing_TSD;
130 class thread_ct;
131 
132 struct TSD_st {
133 public:
134 #if CWDEBUG_ALLOC
135  // The volatile members are incremented/decremented by inline functions, possibly around
136  // calls to malloc et al, which are treated special by the compiler in that it assumes
137  // that a change to a global variable can't have influence-- but these members DO have influence.
138  int volatile internal; // libsysrecord.so relies on this being the first element.
139  int volatile library_call; // libsysrecord.so relies on this being the second element.
140  int inside_malloc_or_free; // Set when entering a (de)allocation routine non-internal.
141  int volatile invisible; // When set, allocation done must be invisible.
142 #endif // CWDEBUG_ALLOC
143 #if CWDEBUG_LOCATION
144  location_format_t format; // Determines how to print location_ct to an ostream.
145 #endif
146 #if LIBCWD_THREAD_SAFE
147  threadlist_t::iterator thread_iter; // Persistant thread specific data (might even stay after this object is destructed).
148  bool thread_iter_valid;
149  thread_ct* target_thread;
150  int terminating;
151  bool pthread_lock_interface_is_locked;// Set while writing debugout to the final ostream.
152  bool list_allocations_on_show_allthreads;
153  int inside_free; // Set when entering free().
154 #endif
155 #if CWDEBUG_DEBUGM
156  int marker;
157 #if CWDEBUG_MAGIC
158  int annotation;
159 #endif
160 #endif
161  bool recursive_fatal; // Detect loop involving dc::fatal or dc::core.
162 #if CWDEBUG_DEBUG
163  bool recursive_assert; // Detect loop involving LIBCWD_ASSERT.
164 #endif
165 #if CWDEBUG_DEBUGT
166  int cancel_explicitely_deferred;
167  int cancel_explicitely_disabled;
168  int inside_critical_area;
169  int cleanup_handler_installed;
170  int internal_debugging_code;
171  mutex_ct* waiting_for_mutex; // mutex_ct that this thread is waiting for.
172  int waiting_for_lock; // The instance of the lock that this thread is waiting for.
173  int waiting_for_rdlock; // The instance of the rdlock that this thread is waiting for.
174  int instance_rdlocked[instance_rdlocked_size];
175  pthread_t rdlocked_by1[instance_rdlocked_size];
176  pthread_t rdlocked_by2[instance_rdlocked_size];
177  void const* rdlocked_from1[instance_rdlocked_size];
178  void const* rdlocked_from2[instance_rdlocked_size];
179 #endif
180 #if LIBCWD_THREAD_SAFE
181  pthread_t tid; // Thread ID.
182  pid_t pid; // Process ID.
183  int do_off_array[LIBCWD_DO_MAX]; // Thread Specific on/off counter for Debug Objects.
184  debug_tsd_st* do_array[LIBCWD_DO_MAX];// Thread Specific Data of Debug Objects or NULL when no debug object.
185  void cleanup_routine();
186  int off_cnt_array[LIBCWD_DC_MAX]; // Thread Specific Data of Debug Channels.
187 private:
188  int tsd_destructor_count;
189 #endif
190 
191 public:
192  void thread_destructed();
193 
194 #if LIBCWD_THREAD_SAFE
195 //-------------------------------------------------------
196 // Static data and methods.
197 private:
198  static TSD_st& S_create(int from_free);
199  static pthread_key_t S_tsd_key;
200  static pthread_once_t S_tsd_key_once;
201  static void S_tsd_key_alloc();
202  static void S_cleanup_routine(void* arg);
203 
204 public:
205  static TSD_st& instance();
206  static TSD_st& instance_free();
207  static void free_instance(TSD_st&);
208 #endif // LIBCWD_THREAD_SAFE
209 };
210 
211 // Thread Specific Data (TSD) is stored in a structure TSD_st
212 // and is accessed through a reference to `__libcwd_tsd'.
213 
214 #if !LIBCWD_THREAD_SAFE
215 // When LIBCWD_THREAD_SAFE is set then `__libcwd_tsd' is a local variable that references
216 // the Thread Specific Data as returned by TSD_st::instance(), otherwise it is simply a
217 // global object in namespace _private_:
218 extern TSD_st __libcwd_tsd;
219 #else
220 extern bool WST_tsd_key_created;
221 #endif
222 
223  } // namespace _private_
224 } // namespace libcwd
225 
226 #if !LIBCWD_THREAD_SAFE
227 // Put __libcwd_tsd in global namespace because anywhere we always refer to it
228 // as `__libcwd_tsd' because when LIBCWD_THREAD_SAFE is set it is local variable.
229 using ::libcwd::_private_::__libcwd_tsd;
230 #endif
231 
232 #endif // LIBCWD_PRIVATE_STRUCT_TSD_H
namespace for libcwd.
Definition: debug.cc:87
Copyright © 2001 - 2004 Carlo Wood.  All rights reserved.