From 5bef9626a3ab5e70fe002f3237761150a5e59787 Mon Sep 17 00:00:00 2001 From: dhinton Date: Mon, 4 Aug 2003 03:53:54 +0000 Subject: ChangeLogTag:Mon Aug 4 03:26:30 UTC 2003 Don Hinton --- ace/Mutex.h | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 ace/Mutex.h (limited to 'ace/Mutex.h') diff --git a/ace/Mutex.h b/ace/Mutex.h new file mode 100644 index 00000000000..610b1880bbe --- /dev/null +++ b/ace/Mutex.h @@ -0,0 +1,168 @@ +// -*- C++ -*- + +//========================================================================== +/** + * @file Mutex.h + * + * $Id$ + * + * Moved from Synch.h. + * + * @author Douglas C. Schmidt + */ +//========================================================================== + +#ifndef ACE_MUTEX_H +#define ACE_MUTEX_H +#include /**/ "ace/pre.h" + +#include "ace/ACE_export.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +#include "ace/OS.h" + +class ACE_Time_Value; + +/** + * @class ACE_Mutex + * + * @brief wrapper (valid in same process or across + * processes (depending on TYPE flag)). + */ +class ACE_Export ACE_Mutex +{ +public: + /// Initialize the mutex. + ACE_Mutex (int type = USYNC_THREAD, + const ACE_TCHAR *name = 0, + ACE_mutexattr_t *arg = 0, + mode_t mode = ACE_DEFAULT_FILE_PERMS); + + /// Implicitly destroy the mutex. + ~ACE_Mutex (void); + + /** + * Explicitly destroy the mutex. Note that only one thread should + * call this method since it doesn't protect against race + * conditions. + */ + int remove (void); + + /// Acquire lock ownership (wait on queue if necessary). + int acquire (void); + + /** + * Block the thread until the mutex is acquired or times out, + * in which case -1 is returned and == . Note that + * is assumed to be in "absolute" rather than "relative" time. + * The value of is updated upon return to show the actual + * (absolute) acquisition time. + */ + int acquire (ACE_Time_Value &tv); + + /** + * If == 0 then call directly. Otherwise, block + * the thread until the mutex is acquired or times out, in + * which case -1 is returned and == . Note that + * <*tv> is assumed to be in "absolute" rather than "relative" time. + * The value of <*tv> is updated upon return to show the actual + * (absolute) acquisition time. */ + int acquire (ACE_Time_Value *tv); + + /** + * Conditionally acquire lock (i.e., don't wait on queue). Returns + * -1 on failure. If we "failed" because someone else already had + * the lock, is set to . + */ + int tryacquire (void); + + /// Release lock and unblock a thread at head of queue. + int release (void); + + /** + * Acquire mutex ownership. This calls and is only + * here to make the interface consistent with the + * other synchronization APIs. + */ + int acquire_read (void); + + /** + * Acquire mutex ownership. This calls and is only + * here to make the interface consistent with the + * other synchronization APIs. + */ + int acquire_write (void); + + /** + * Conditionally acquire mutex (i.e., won't block). This calls + * and is only here to make the interface + * consistent with the other synchronization APIs. Returns -1 on + * failure. If we "failed" because someone else already had the + * lock, is set to . + */ + int tryacquire_read (void); + + /** + * Conditionally acquire mutex (i.e., won't block). This calls + * and is only here to make the interface + * consistent with the other synchronization APIs. Returns -1 on + * failure. If we "failed" because someone else already had the + * lock, is set to . + */ + int tryacquire_write (void); + + /** + * This is only here for consistency with the other synchronization + * APIs and usability with Lock adapters. Assumes the caller already has + * acquired the mutex and returns 0 in all cases. + */ + int tryacquire_write_upgrade (void); + + /// Return the underlying mutex. + const ACE_mutex_t &lock (void) const; + + /// Dump the state of an object. + void dump (void) const; + + /// Declare the dynamic allocation hooks. + ACE_ALLOC_HOOK_DECLARE; + + // = This should be protected but some C++ compilers complain... +public: +#if defined (CHORUS) || defined(ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) + /// This lock resides in shared memory. + ACE_mutex_t *process_lock_; + + /** + * Remember the name of the mutex if we created it so we can unlink + * it when we go away (only the actor that initialized the memory + * can destroy it). + */ + const ACE_TCHAR *lockname_; +#endif /* CHORUS || ACE_HAS_PTHREADS */ + + /// Mutex type supported by the OS. + ACE_mutex_t lock_; + + /// Keeps track of whether has been called yet to avoid + /// multiple calls, e.g., explicitly and implicitly in the + /// destructor. This flag isn't protected by a lock, so make sure + /// that you don't have multiple threads simultaneously calling + /// on the same object, which is a bad idea anyway... + int removed_; + +private: + // = Prevent assignment and initialization. + void operator= (const ACE_Mutex &); + ACE_Mutex (const ACE_Mutex &); +}; + +#if defined (__ACE_INLINE__) +#include "ace/Mutex.inl" +#endif /* __ACE_INLINE__ */ + +#include /**/ "ace/post.h" +#endif /* ACE_MUTEX_H */ -- cgit v1.2.1