Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

/home/pauldt/projects/IDEA/IDEA-ALL/IDEA-DEV/Core/Utilities/Includes/ThreadTools.hh

Go to the documentation of this file.
00001 // -*- Mode: c++ -*-
00002 //     File: $Source: /home/cvs/ISG-Repository/IDEA-DEV/Core/Utilities/Includes/ThreadTools.hh,v $
00003 //      Tag: $Name: DR_01_11_06 $
00004 //      CVS: $Id: ThreadTools.hh,v 1.10 2005/12/30 22:06:06 rijsman Exp $
00005 //     Info: $CVSROOT/IDEA-DEV/COPYRIGHT
00006 
00007 #ifndef THREAD_TOOLS_HH
00008 #define THREAD_TOOLS_HH
00009 
00010 #include <ace/OS.h>
00011 #include <ace/Synch.h>
00012 #include <ace/Process_Mutex.h>
00013 #include <ace/Process_Semaphore.h>
00014 #include <ace/Thread_Manager.h>
00015 #include <ace/Singleton.h>
00016 #include <ace/Task.h>
00017 
00018 
00019 #include <string>
00020 #include <iostream>
00021 #include <map>
00022 
00023 #include "Singleton.hh"
00024 
00025 #ifdef TRACE_LOCKS
00026  #include "LockTrace.hh"
00027 #endif
00028 
00029 namespace IDEA
00030 {
00044   class ThreadNames:
00045     public Singleton<ThreadNames>
00046   {
00047     friend class Singleton<ThreadNames>;
00048   public:
00052     ~ThreadNames();
00056     static void registerThread( const std::string & name);
00060     static const std::string getName();
00061   private:
00065     ThreadNames();
00069     typedef std::map< ACE_thread_t, std::string > Thread2String;
00070 
00071     static Thread2String m_RegisteredThreads;
00072   };
00073 
00083   template<class LOCK> 
00084   void acquire( LOCK& lock, const std::string& locator )
00085   {
00086 #ifdef TRACE_LOCKS
00087    LockTrace::instance()->lockInvoked( locator, (long) &lock );
00088 #endif
00089     if( -1 == lock.acquire() )
00090         {
00091           std::cerr << "Error: acquiring a lock failed at location '" << locator << "'" << std::endl;
00092           ACE_OS::abort();
00093         }
00094 #ifdef TRACE_LOCKS
00095    LockTrace::instance()->lockAcquired( locator, (long)  &lock );
00096 #endif
00097   }
00098 
00108   template<class LOCK>
00109   void release( LOCK& lock, const std::string& locator)
00110   {
00111 #ifdef TRACE_LOCKS
00112  LockTrace::instance()->unLockInvoked( locator, (long)  &lock );
00113 #endif
00114     if( -1 == lock.release() )
00115       {
00116         std::cerr << "Error: releasing a lock failed at location '" << locator << "'" << std::endl;
00117         ACE_OS::abort();
00118       }
00119 #ifdef TRACE_LOCKS
00120   LockTrace::instance()->unLockReleased( locator,  (long) &lock );
00121 #endif
00122   }
00123 
00124 
00131  template<class LOCK> 
00132   class Guard
00133   {
00134   public:
00138     Guard( LOCK& lock, const std::string& locator = "unknown"):
00139       m_Lock( lock )
00140     {
00141       acquire<LOCK>( m_Lock, locator );
00142     }
00146     ~Guard()
00147     {
00148       release<LOCK>( m_Lock,"guard");
00149     }
00150   private:
00152     Guard(); 
00154     Guard( const Guard&); 
00156     Guard& operator=(const Guard& ); 
00157 
00158     LOCK& m_Lock;
00159   };
00160   
00164   typedef ACE_Thread_Mutex ThreadMutex;
00168   typedef ACE_Recursive_Thread_Mutex RecursiveThreadMutex;
00172   typedef ACE_Process_Mutex ProcessMutex;
00176   typedef ACE_Thread_Semaphore ThreadSemaphore;
00180   typedef ACE_Process_Semaphore ProcessSemaphore;
00181 
00182   /*
00183     }@
00184   */
00185 }
00186 
00187 #endif // ifndef THREAD_TOOLS_HH

Contact information
© IDEA
Generated on Fri Feb 3 17:09:41 2006 for IDEA.