Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:25:33

0001 // This is a template class which defines a thread-safe integral
0002 // library, from a possibly thread-unsafe one.  This class is derived
0003 // from the one defined by its template parameter (which in turn
0004 // should be derived from IntegralLibrary).  We use mutexes (defined
0005 // by POSIX threads) to control access to the integral library
0006 // routines.
0007 
0008 
0009 #ifndef NINJA_THREAD_SAFE_INTEGRAL_LIBRARY
0010 #define NINJA_THREAD_SAFE_INTEGRAL_LIBRARY
0011 
0012 #include <pthread.h>
0013 #include <ninja/integral_library.hh>
0014 
0015 
0016 namespace ninja {
0017 
0018   // simple class for a mutex
0019   class SimpleMutex {
0020   public:
0021 
0022     SimpleMutex()
0023     {
0024       pthread_mutex_init(& mutex_, NULL);
0025     }
0026 
0027     ~SimpleMutex()
0028     {
0029       pthread_mutex_destroy(& mutex_);
0030     }
0031 
0032     void lock()
0033     {
0034       pthread_mutex_lock(& mutex_);
0035     }
0036 
0037     void unlock()
0038     {
0039       pthread_mutex_unlock(& mutex_);
0040     }
0041 
0042   private:
0043     pthread_mutex_t mutex_;    
0044   };
0045 
0046 
0047   // this class unlocks a mutex when goes out of scope (but, unlike a
0048   // lock guard, it doesn't lock it in the constructor)
0049   class SimpleMutexUnlocker {
0050   public:
0051 
0052     SimpleMutexUnlocker(SimpleMutex & mutex): mutex_(mutex) {}
0053 
0054     ~SimpleMutexUnlocker()
0055     {
0056       mutex_.unlock();
0057     }
0058 
0059   private:
0060     SimpleMutex & mutex_;
0061   };
0062 
0063 
0064   template <typename BaseLib_>
0065   class ThreadSafeIntegralLibrary : public BaseLib_ {
0066   public:
0067 
0068     ThreadSafeIntegralLibrary(): BaseLib_(), mutex_() {}
0069 
0070     virtual void init(Real muRsq)
0071     {
0072       mutex_.lock();
0073       BaseLib_::init(muRsq);
0074     }
0075 
0076     virtual void exit()
0077     {
0078       SimpleMutexUnlocker unlocker(mutex_);
0079       BaseLib_::exit();
0080     }
0081 
0082   private:
0083     SimpleMutex mutex_;
0084   };
0085 
0086 } // namespace ninja
0087 
0088 #endif // NINJA_THREAD_SAFE_INTEGRAL_LIBRARY