Mutex.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    Mutex.h
00006  *
00007  *  Mutex.h,v 4.5 2006/05/09 07:24:29 jwillemsen Exp
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //==========================================================================
00012 
00013 #ifndef ACE_MUTEX_H
00014 #define ACE_MUTEX_H
00015 
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "ace/ACE_export.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/OS_NS_Thread.h"
00025 #include "ace/OS_NS_unistd.h"
00026 #include "ace/os_include/os_fcntl.h"
00027 
00028 # if !defined (ACE_DEFAULT_MUTEX_A)
00029 #   define ACE_DEFAULT_MUTEX_A "ACE_MUTEX"
00030 # endif /* ACE_DEFAULT_MUTEX_A */
00031 
00032 # if defined (ACE_HAS_WCHAR)
00033 #   define ACE_DEFAULT_MUTEX_W ACE_TEXT_WIDE(ACE_DEFAULT_MUTEX_A)
00034 # endif /* ACE_HAS_WCHAR */
00035 
00036 # define ACE_DEFAULT_MUTEX ACE_LIB_TEXT (ACE_DEFAULT_MUTEX_A)
00037 
00038 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00039 
00040 class ACE_Time_Value;
00041 
00042 /**
00043  * @class ACE_Mutex
00044  *
00045  * @brief @c ACE_Mutex wrapper (valid in same process or across
00046  *        processes (depending on @a TYPE flag)).
00047  */
00048 class ACE_Export ACE_Mutex
00049 {
00050 public:
00051   /// Initialize the mutex.
00052   ACE_Mutex (int type = USYNC_THREAD,
00053              const ACE_TCHAR *name = 0,
00054              ACE_mutexattr_t *arg = 0,
00055              mode_t mode = ACE_DEFAULT_FILE_PERMS);
00056 
00057   /// Implicitly destroy the mutex.
00058   ~ACE_Mutex (void);
00059 
00060   /// Explicitly destroy the mutex.
00061   /**
00062    * @note Only one thread should call this method since it doesn't
00063    *        protect against race conditions.
00064    */
00065   int remove (void);
00066 
00067   /// Acquire lock ownership (wait on queue if necessary).
00068   int acquire (void);
00069 
00070   /// Block the thread until the mutex is acquired or @a tv times out,
00071   /// in which case -1 is returned and @c errno == @c ETIME.
00072   /**
00073    * @note @a tv is assumed  to be in "absolute" rather than
00074    * "     relative" time.  The value of @a tv is updated upon return
00075    *       to show the actual(absolute) acquisition time.
00076    */
00077   int acquire (ACE_Time_Value &tv);
00078 
00079   /// Block the thread until the mutex is acquired or @a *tv times
00080   /// out, in which case -1 is returned and @c errno == @c ETIME.
00081   /**
00082    * If @a tv == 0 then call @c acquire() directly.  Otherwise, block
00083    * the thread until the mutex is acquired or @a tv times out, in
00084    * which case -1 is returned and @c errno == @c ETIME.
00085    *
00086    * @note @a *tv is assumed to be in "absolute" rather than
00087    *       "relative" time.  The value of @a *tv is updated upon
00088    *       return to show the actual (absolute) acquisition time.
00089    */
00090   int acquire (ACE_Time_Value *tv);
00091 
00092   /// Conditionally acquire lock (i.e., don't wait on queue).
00093   /**
00094    * @return -1 on failure.  If we "failed" because someone
00095    *         else already had the lock, @c errno is set to @c EBUSY.
00096    */
00097   int tryacquire (void);
00098 
00099   /// Release lock and unblock a thread at head of queue.
00100   int release (void);
00101 
00102   /// Acquire mutex ownership.
00103   /**
00104    * This calls @c acquire and is only here to make the @c ACE_Mutex
00105    * interface consistent with the other synchronization APIs.
00106    */
00107   int acquire_read (void);
00108 
00109   /// Acquire mutex ownership.
00110   /**
00111    * This calls @c acquire and is only here to make the @c ACE_Mutex
00112    * interface consistent with the other synchronization APIs.
00113    */
00114   int acquire_write (void);
00115 
00116   /// Conditionally acquire mutex (i.e., won't block).
00117   /**
00118    * This calls @c tryacquire and is only here to make the @c ACE_Mutex
00119    * interface consistent with the other synchronization APIs.
00120    *
00121    * @return -1 on failure.  If we "failed" because someone else
00122    *         already had the lock, @c errno is set to @c EBUSY.
00123    */
00124   int tryacquire_read (void);
00125 
00126   /// Conditionally acquire mutex (i.e., won't block).
00127   /**
00128    * This calls @c tryacquire and is only here to make the @c ACE_Mutex
00129    * interface consistent with the other synchronization APIs.
00130    *
00131    * @return -1 on failure.  If we "failed" because someone else
00132    *         already had the lock, @c errno is set to @c EBUSY.
00133    */
00134   int tryacquire_write (void);
00135 
00136   /**
00137    * This is only here for consistency with the other synchronization
00138    * APIs and usability with Lock adapters. Assumes the caller already has
00139    * acquired the mutex and returns 0 in all cases.
00140    */
00141   int tryacquire_write_upgrade (void);
00142 
00143   /// Return the underlying mutex.
00144   const ACE_mutex_t &lock (void) const;
00145 
00146   /// Dump the state of an object.
00147   void dump (void) const;
00148 
00149   /// Declare the dynamic allocation hooks.
00150   ACE_ALLOC_HOOK_DECLARE;
00151 
00152   // = This should be protected but some C++ compilers complain...
00153 public:
00154 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
00155   /// This lock resides in shared memory.
00156   ACE_mutex_t *process_lock_;
00157 
00158   /**
00159    * Remember the name of the mutex if we created it so we can unlink
00160    * it when we go away (only the actor that initialized the memory
00161    * can destroy it).
00162    */
00163   const ACE_TCHAR *lockname_;
00164 #endif /* ACE_HAS_PTHREADS */
00165 
00166   /// Mutex type supported by the OS.
00167   ACE_mutex_t lock_;
00168 
00169   /// Keeps track of whether @c remove has been called yet to avoid
00170   /// multiple @c remove calls, e.g., explicitly and implicitly in the
00171   /// destructor.  This flag isn't protected by a lock, so make sure
00172   /// that you don't have multiple threads simultaneously calling
00173   /// @c remove on the same object, which is a bad idea anyway.
00174   int removed_;
00175 
00176 private:
00177   // Prevent assignment and initialization.
00178   void operator= (const ACE_Mutex &);
00179   ACE_Mutex (const ACE_Mutex &);
00180 };
00181 
00182 ACE_END_VERSIONED_NAMESPACE_DECL
00183 
00184 #if defined (__ACE_INLINE__)
00185 #include "ace/Mutex.inl"
00186 #endif /* __ACE_INLINE__ */
00187 
00188 #include /**/ "ace/post.h"
00189 
00190 #endif /* ACE_MUTEX_H */

Generated on Thu Nov 9 09:41:57 2006 for ACE by doxygen 1.3.6