// ThreadCredential.cxx #include "dataset_util/ThreadCredential.h" #include #include #include "dataset_util/PThreadMutex.h" using std::ostream; using std::setw; using std::hex; using std::setfill; typedef ThreadCredential::Name Name; typedef ThreadCredential::NameMap NameMap; typedef ThreadCredential::FunPtr FunPtr; //********************************************************************** // Local definitions. //********************************************************************** PThreadMutex& local_mutex() { static PThreadMutex val; return val; } FunPtr& owner_lookup() { static FunPtr val = 0; return val; } Name& local_default_owner() { static Name val = ""; return val; } //********************************************************************** // Methods. //********************************************************************** // Return the map. NameMap& ThreadCredential::names() { static ThreadCredential::NameMap val; return val; } //********************************************************************** // Register owner lookup. int ThreadCredential::register_owner_locator(FunPtr pfun) { owner_lookup() = pfun; return 0; } //********************************************************************** // Constructor. ThreadCredential::ThreadCredential() { local_mutex().lock(); } //********************************************************************** // Destructor. ThreadCredential::~ThreadCredential() { local_mutex().unlock(); } //********************************************************************** // Set default owner. int ThreadCredential::set_default_owner(Name owner) { local_default_owner() = owner; return 0; } //********************************************************************** // Get default owner. Name ThreadCredential::default_owner() const { return local_default_owner(); } //********************************************************************** // Return the name for the current thread. ThreadCredential::Name ThreadCredential::name() const { ThreadCredential& mcred = const_cast(*this); NameMap::const_iterator inam = mcred.names().find(pthread_self()); if ( inam == mcred.names().end() ) { return ""; } return inam->second; } //********************************************************************** // Return the owner for the current thread. ThreadCredential::Name ThreadCredential::owner() const { Name credname = name(); if ( credname == "" ) return default_owner(); if ( owner_lookup() == 0 ) return ""; return owner_lookup()(credname); } //********************************************************************** // Return the owner for a given credential name. ThreadCredential::Name ThreadCredential::owner(Name credname) const { if ( credname == "" ) return ""; if ( owner_lookup() == 0 ) return ""; return owner_lookup()(credname); } //********************************************************************** // Return the mutex. const PThreadMutex& ThreadCredential::mutex() { return local_mutex(); } //********************************************************************** // Output stream. ostream& ThreadCredential::ostr(ostream& lhs) const { lhs << "Thread credential default owner is \"" << default_owner() << "\"\n"; ThreadCredential& mcred = const_cast(*this); int ncred = mcred.names().size(); if ( ncred == 1 ) { lhs << "There is 1 thread credential"; } else { lhs << "There are " << ncred << " thread credentials"; } if ( ncred > 0 ) lhs << ":"; for ( NameMap::const_iterator inam=mcred.names().begin(); inam!=mcred.names().end(); ++inam ) { Name credname = inam->second; Name credowner = owner(credname); lhs << "\n 0x" << hex << setw(8) << setfill('0') << inam->first << setfill(' ') << " " << setw(16) << credowner << " " << credname; } return lhs; } //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const ThreadCredential& rhs) { return rhs.ostr(lhs); } //**********************************************************************