|
From: Carlo W. <li...@us...> - 2002-01-21 03:20:35
|
CVSROOT : /cvsroot/libcw
Module : src
Commit time: 2002-00-21 03:20:34 UTC
Modified files:
libcwd/debug.cc libcwd/include/libcw/class_debug.h
libcwd/include/libcw/private_threading.h
libcwd/include/libcw/struct_debug_tsd.h libcwd/tests/threads.cc
Log message:
debug_ct is now thread-safe.
---------------------- diff included ----------------------
Index: src/libcwd/debug.cc
diff -u src/libcwd/debug.cc:1.57 src/libcwd/debug.cc:1.58
--- src/libcwd/debug.cc:1.57 Sun Jan 20 15:31:18 2002
+++ src/libcwd/debug.cc Sun Jan 20 19:20:24 2002
@@ -1,4 +1,4 @@
-// $Header: /cvsroot/l/li/libcw/src/libcwd/debug.cc,v 1.57 2002/01/20 23:31:18 libcw Exp $
+// $Header: /cvsroot/l/li/libcw/src/libcwd/debug.cc,v 1.58 2002/01/21 03:20:24 libcw Exp $
//
// Copyright (C) 2000 - 2001, by
//
@@ -263,6 +263,24 @@
#ifdef LIBCWD_THREAD_SAFE
_private_::initialize_global_mutexes();
#endif
+
+ // Fatal channels need to be marked fatal, otherwise we get into an endless loop
+ // when they are used before they are created.
+ channels::dc::core.NS_initialize("COREDUMP", coredump_maskbit);
+ channels::dc::fatal.NS_initialize("FATAL", fatal_maskbit);
+ // Initialize other debug channels that might be used before we reach main().
+ channels::dc::debug.NS_initialize("DEBUG");
+ channels::dc::malloc.NS_initialize("MALLOC");
+ channels::dc::continued.NS_initialize(continued_maskbit);
+ channels::dc::finish.NS_initialize(finish_maskbit);
+#ifdef DEBUGUSEBFD
+ channels::dc::bfd.NS_initialize("BFD");
+#endif
+ // What the heck, initialize all other debug channels too
+ channels::dc::warning.NS_initialize("WARNING");
+ channels::dc::notice.NS_initialize("NOTICE");
+ channels::dc::system.NS_initialize("SYSTEM");
+
libcw_do.NS_init(); // Initialize debug code.
#ifdef DEBUGUSEBFD
cwbfd::ST_init(); // Initialize BFD code.
@@ -842,6 +860,8 @@
DoutFatal( dc::core, "Don't use `DoutFatal' together with `continued_cf', use `Dout' instead. (This message can also occur when using DoutFatal correctly but from the constructor of a global object)." );
}
+ int debug_ct::S_index_count = 0;
+
void debug_ct::NS_init(void)
{
ST_initialize_globals(); // Because all allocations for global objects are internal these days, we use
@@ -871,6 +891,9 @@
new (_private_::WST_dummy_laf) laf_ct(0, channels::dc::debug.get_label(), 0); // Leaks 24 bytes of memory
#ifndef LIBCWD_THREAD_SAFE
tsd.init();
+#else
+ WNS_index = ++S_index_count;
+ __libcwd_tsd.do_array[WNS_index].init();
#endif
set_alloc_checking_on(LIBCWD_TSD);
@@ -905,32 +928,17 @@
#endif
}
+ debug_tsd_st::debug_tsd_st(void) : _off(0), tsd_initialized(false) { } // Turn off all debugging until initialization is completed.
+
void debug_tsd_st::init(void)
{
#ifdef DEBUGDEBUGMALLOC
LIBCWD_TSD_DECLARATION
LIBCWD_ASSERT( __libcwd_tsd.internal );
#endif
- _off = 0; // Turn off all debugging until initialization is completed.
- DEBUGDEBUG_CERR( "In debug_tsd_st::NS_init(void), _off set to 0" );
+ DEBUGDEBUG_CERR( "In debug_tsd_st::init(void), _off set to 0" );
start_expected = true; // Of course, we start with expecting the beginning of a debug output.
- // Fatal channels need to be marked fatal, otherwise we get into an endless loop
- // when they are used before they are created.
- channels::dc::core.NS_initialize("COREDUMP", coredump_maskbit);
- channels::dc::fatal.NS_initialize("FATAL", fatal_maskbit);
- // Initialize other debug channels that might be used before we reach main().
- channels::dc::debug.NS_initialize("DEBUG");
- channels::dc::malloc.NS_initialize("MALLOC");
- channels::dc::continued.NS_initialize(continued_maskbit);
- channels::dc::finish.NS_initialize(finish_maskbit);
-#ifdef DEBUGUSEBFD
- channels::dc::bfd.NS_initialize("BFD");
-#endif
- // What the heck, initialize all other debug channels too
- channels::dc::warning.NS_initialize("WARNING");
- channels::dc::notice.NS_initialize("NOTICE");
- channels::dc::system.NS_initialize("SYSTEM");
// `current' needs to be non-zero (saving us a check in start()) and
// current.mask needs to be 0 to avoid a crash in start():
current = reinterpret_cast<laf_ct*>(_private_::WST_dummy_laf);
@@ -951,10 +959,22 @@
_off = 0; // Don't print debug output till the REAL initialization of the debug system has been performed
// (ie, the _application_ start (don't confuse that with the constructor - which does nothing)).
#endif
- DEBUGDEBUG_CERR( "After debug_tsd_st::NS_init(void), _off set to " << _off );
+ DEBUGDEBUG_CERR( "After debug_tsd_st::init(void), _off set to " << _off );
tsd_initialized = true;
}
+ namespace _private_ {
+ void debug_tsd_init(void)
+ {
+ LIBCWD_TSD_DECLARATION
+ ForAllDebugObjects(
+ set_alloc_checking_off(LIBCWD_TSD);
+ LIBCWD_DO_TSD(debugObject).init();
+ set_alloc_checking_on(LIBCWD_TSD);
+ );
+ }
+ }
+
debug_tsd_st::~debug_tsd_st()
{
if (!tsd_initialized) // Don't de-initialize when it wasn't initialized.
@@ -1083,9 +1103,6 @@
DEBUGDEBUG_CERR( "Entering `channel_ct::NS_initialize(\"" << label << "\")'" );
- // Of course, dc::debug is off - so this won't do anything unless DEBUGDEBUG is #defined.
- Dout( dc::debug, "Initializing channel_ct(\"" << label << "\")" );
-
size_t label_len = strlen(label);
if (label_len > max_label_len_c) // Only happens for customized channels
@@ -1162,9 +1179,6 @@
WNS_maskbit = maskbit;
DEBUGDEBUG_CERR( "Entering `fatal_channel_ct::NS_initialize(\"" << label << "\")'" );
-
- // Of course, dc::debug is off - so this won't do anything unless DEBUGDEBUG is #defined.
- Dout( dc::debug, "Initializing fatal_channel_ct(\"" << label << "\")" );
size_t label_len = strlen(label);
Index: src/libcwd/include/libcw/class_debug.h
diff -u src/libcwd/include/libcw/class_debug.h:1.6 src/libcwd/include/libcw/class_debug.h:1.7
--- src/libcwd/include/libcw/class_debug.h:1.6 Sun Jan 20 08:25:11 2002
+++ src/libcwd/include/libcw/class_debug.h Sun Jan 20 19:20:24 2002
@@ -1,4 +1,4 @@
-// $Header: /cvsroot/l/li/libcw/src/libcwd/include/libcw/class_debug.h,v 1.6 2002/01/20 16:25:11 libcw Exp $
+// $Header: /cvsroot/l/li/libcw/src/libcwd/include/libcw/class_debug.h,v 1.7 2002/01/21 03:20:24 libcw Exp $
//
// Copyright (C) 2000 - 2002, by
//
@@ -74,6 +74,7 @@
debug_tsd_st tsd;
#else
int WNS_index;
+ static int S_index_count;
#endif
protected:
Index: src/libcwd/include/libcw/private_threading.h
diff -u src/libcwd/include/libcw/private_threading.h:1.8 src/libcwd/include/libcw/private_threading.h:1.9
--- src/libcwd/include/libcw/private_threading.h:1.8 Fri Jan 18 21:08:54 2002
+++ src/libcwd/include/libcw/private_threading.h Sun Jan 20 19:20:24 2002
@@ -1,4 +1,4 @@
-// $Header: /cvsroot/l/li/libcw/src/libcwd/include/libcw/private_threading.h,v 1.8 2002/01/19 05:08:54 libcw Exp $
+// $Header: /cvsroot/l/li/libcw/src/libcwd/include/libcw/private_threading.h,v 1.9 2002/01/21 03:20:24 libcw Exp $
//
// Copyright (C) 2001, by
//
@@ -24,6 +24,10 @@
#ifndef LIBCW_PRIVATE_SET_ALLOC_CHECKING_H
#include <libcw/private_set_alloc_checking.h>
#endif
+#ifndef LIBCW_CSTRING
+#define LIBCW_CSTRING
+#include <cstring> // Needed for std::memset and std::memcpy.
+#endif
#ifdef LIBCWD_HAVE_PTHREAD
#include <pthread.h>
@@ -51,10 +55,6 @@
namespace debug {
namespace _private_ {
-//===================================================================================================
-// Thread Specific Data
-//
-
#ifdef DEBUGDEBUG
extern bool WST_multi_threaded;
#endif
@@ -467,6 +467,8 @@
pthread_key_create(&S_key, S_destroy);
}
+extern void debug_tsd_init(void);
+
template<class TSD>
TSD* thread_specific_data_tct<TSD>::S_initialize(void) throw()
{
@@ -477,21 +479,24 @@
mutex_tct<tsd_initialization_instance>::unlock();
return S_temporary_instance;
}
- TSD new_TSD;
- S_temporary_instance = &new_TSD;
+ char new_TSD_space[sizeof(TSD)]; // Allocate space on the stack.
+ S_temporary_instance = new (new_TSD_space) TSD; // Create a temporary TSD.
S_initializing = true;
- set_alloc_checking_off(new_TSD);
+ LIBCWD_TSD_DECLARATION // This will 'return' the temporary TSD if TSD == TSD_st.
+ set_alloc_checking_off(LIBCWD_TSD);
TSD* instance = new TSD;
- set_alloc_checking_on(new_TSD);
+ set_alloc_checking_on(LIBCWD_TSD);
pthread_setspecific(S_key, instance);
// Because pthread_setspecific calls calloc, it is possible that in
// the mean time all of libcwd was initialized. Therefore we need
// to copy the temporary TSD to the real TSD because it might
// contain relevant information.
- *instance = new_TSD; // This will not call malloc() et al, and thus not change new_TSD while we're copying it.
+ std::memcpy((void*)instance, new_TSD_space, sizeof(TSD)); // Put the temporary TSD in its final place.
S_initializing = false;
S_WNS_initialized = true;
mutex_tct<tsd_initialization_instance>::unlock();
+ if (WST_multi_threaded) // Is this a second (or later) thread?
+ debug_tsd_init(); // Initialize the TSD of existing debug objects.
return instance;
}
#endif // defined(LIBCWD_USE_POSIX_THREADS) || defined(LIBCWD_USE_LINUXTHREADS)
Index: src/libcwd/include/libcw/struct_debug_tsd.h
diff -u src/libcwd/include/libcw/struct_debug_tsd.h:1.3 src/libcwd/include/libcw/struct_debug_tsd.h:1.4
--- src/libcwd/include/libcw/struct_debug_tsd.h:1.3 Sun Jan 20 08:25:11 2002
+++ src/libcwd/include/libcw/struct_debug_tsd.h Sun Jan 20 19:20:24 2002
@@ -1,4 +1,4 @@
-// $Header: /cvsroot/l/li/libcw/src/libcwd/include/libcw/struct_debug_tsd.h,v 1.3 2002/01/20 16:25:11 libcw Exp $
+// $Header: /cvsroot/l/li/libcw/src/libcwd/include/libcw/struct_debug_tsd.h,v 1.4 2002/01/21 03:20:24 libcw Exp $
//
// Copyright (C) 2002, by
//
@@ -58,6 +58,9 @@
int _off;
// Debug output is turned on when this variable is -1, otherwise it is off.
+ bool tsd_initialized;
+ // Set after initialization is completed.
+
#ifdef DEBUGDEBUGOUTPUT
// Since with DEBUGDEBUG defined we start with _off is -1 instead of 0,
// we need to ignore the call to on() the first time it is called.
@@ -108,8 +111,8 @@
void fatal_finish(debug_ct&, channel_set_data_st& LIBCWD_COMMA_TSD_PARAM) __attribute__ ((__noreturn__));
// Initialization and de-initialization.
- bool tsd_initialized;
void init(void);
+ debug_tsd_st(void);
~debug_tsd_st();
};
Index: src/libcwd/tests/threads.cc
diff -u src/libcwd/tests/threads.cc:1.7 src/libcwd/tests/threads.cc:1.8
--- src/libcwd/tests/threads.cc:1.7 Sat Dec 29 20:17:50 2001
+++ src/libcwd/tests/threads.cc Sun Jan 20 19:20:24 2002
@@ -4,7 +4,6 @@
//#include "sys.h"
#include "../include/sys.h"
#include "threads_debug.h"
-#include <libcw/threading.h>
#include <vector>
#include <iostream>
@@ -94,6 +93,8 @@
// Set Thread Specific on/off flags of the debug channels.
ForAllDebugChannels( if (!debugChannel.is_on()) debugChannel.on(); );
+ // And for the debug object.
+ Debug( libcw_do.on() );
// Serialize incrementation.
mutex_tct<test_instance0>::lock();
----------------------- End of diff -----------------------
|