3 //==========================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //==========================================================================
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/OS_NS_Thread.h"
23 #include "ace/OS_NS_unistd.h"
24 #include "ace/os_include/os_fcntl.h"
26 #if !defined (ACE_DEFAULT_MUTEX_A)
27 # define ACE_DEFAULT_MUTEX_A "ACE_MUTEX"
28 #endif /* ACE_DEFAULT_MUTEX_A */
30 #if defined (ACE_HAS_WCHAR)
31 # define ACE_DEFAULT_MUTEX_W ACE_TEXT_WIDE (ACE_DEFAULT_MUTEX_A)
32 #endif /* ACE_HAS_WCHAR */
34 #define ACE_DEFAULT_MUTEX ACE_TEXT (ACE_DEFAULT_MUTEX_A)
36 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
43 * @brief @c ACE_Mutex wrapper (valid in same process or across
44 * processes (depending on @a TYPE flag)). In general,
45 * however, we recommend using @a ACE_Process_Mutex or @a
46 * ACE_Thread_Mutex rather than @a ACE_Mutex.
48 class ACE_Export ACE_Mutex
51 /// Initialize the mutex.
52 ACE_Mutex (int type
= USYNC_THREAD
,
53 const ACE_TCHAR
*name
= 0,
54 ACE_mutexattr_t
*arg
= 0,
55 mode_t mode
= ACE_DEFAULT_FILE_PERMS
);
57 /// Implicitly destroy the mutex.
60 /// Explicitly destroy the mutex.
62 * @note Only one thread should call this method since it doesn't
63 * protect against race conditions.
67 /// Acquire lock ownership (wait on queue if necessary).
70 /// Block the thread until the mutex is acquired or @a tv times out,
71 /// in which case -1 is returned and @c errno == @c ETIME.
73 * @note @a tv is assumed to be in "absolute" rather than
74 * " relative" time. The value of @a tv is updated upon return
75 * to show the actual(absolute) acquisition time.
77 int acquire (ACE_Time_Value
&tv
);
79 /// Block the thread until the mutex is acquired or @a *tv times
80 /// out, in which case -1 is returned and @c errno == @c ETIME.
82 * If @a tv == 0 then call @c acquire() directly. Otherwise, block
83 * the thread until the mutex is acquired or @a tv times out, in
84 * which case -1 is returned and @c errno == @c ETIME.
86 * @note @a *tv is assumed to be in "absolute" rather than
87 * "relative" time. The value of @a *tv is updated upon
88 * return to show the actual (absolute) acquisition time.
90 int acquire (ACE_Time_Value
*tv
);
92 /// Conditionally acquire lock (i.e., don't wait on queue).
94 * @return -1 on failure. If we "failed" because someone
95 * else already had the lock, @c errno is set to @c EBUSY.
99 /// Release lock and unblock a thread at head of queue.
102 /// Acquire mutex ownership.
104 * This calls @c acquire and is only here to make the @c ACE_Mutex
105 * interface consistent with the other synchronization APIs.
109 /// Acquire mutex ownership.
111 * This calls @c acquire and is only here to make the @c ACE_Mutex
112 * interface consistent with the other synchronization APIs.
114 int acquire_write ();
116 /// Conditionally acquire mutex (i.e., won't block).
118 * This calls @c tryacquire and is only here to make the @c ACE_Mutex
119 * interface consistent with the other synchronization APIs.
121 * @return -1 on failure. If we "failed" because someone else
122 * already had the lock, @c errno is set to @c EBUSY.
124 int tryacquire_read ();
126 /// Conditionally acquire mutex (i.e., won't block).
128 * This calls @c tryacquire and is only here to make the @c ACE_Mutex
129 * interface consistent with the other synchronization APIs.
131 * @return -1 on failure. If we "failed" because someone else
132 * already had the lock, @c errno is set to @c EBUSY.
134 int tryacquire_write ();
137 * This is only here for consistency with the other synchronization
138 * APIs and usability with Lock adapters. Assumes the caller already has
139 * acquired the mutex and returns 0 in all cases.
141 int tryacquire_write_upgrade ();
143 /// Return the underlying mutex.
144 const ACE_mutex_t
&lock () const;
145 ACE_mutex_t
&lock ();
147 /// If a file was created as the underlying storage for the mutex,
148 /// remove it from the filesystem (for process-shared mutexes).
149 static int unlink (const ACE_TCHAR
*name
);
151 /// Dump the state of an object.
154 /// Declare the dynamic allocation hooks.
155 ACE_ALLOC_HOOK_DECLARE
;
157 // = This should be protected but some C++ compilers complain...
159 #if defined ACE_HAS_PTHREADS && defined ACE_LACKS_MUTEXATTR_PSHARED
160 # define ACE_MUTEX_USE_PROCESS_LOCK
161 # define ACE_MUTEX_PROCESS_LOCK_IS_SEMA
162 ACE_sema_t process_sema_
;
163 typedef ACE_sema_t Process_Lock
;
164 #elif defined ACE_HAS_PTHREADS
165 # define ACE_MUTEX_USE_PROCESS_LOCK
166 # define ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
167 typedef ACE_mutex_t Process_Lock
;
170 #ifdef ACE_MUTEX_USE_PROCESS_LOCK
171 /// This lock resides in shared memory.
172 Process_Lock
*process_lock_
;
175 * Remember the name of the mutex if we created it so we can unlink
176 * it when we go away (only the actor that initialized the memory
179 const ACE_TCHAR
*lockname_
;
180 #endif /* ACE_MUTEX_USE_PROCESS_LOCK */
182 /// Mutex type supported by the OS.
185 /// Keeps track of whether @c remove has been called yet to avoid
186 /// multiple @c remove calls, e.g., explicitly and implicitly in the
187 /// destructor. This flag isn't protected by a lock, so make sure
188 /// that you don't have multiple threads simultaneously calling
189 /// @c remove on the same object, which is a bad idea anyway.
193 ACE_Mutex
&operator= (const ACE_Mutex
&) = delete;
194 ACE_Mutex (const ACE_Mutex
&) = delete;
197 ACE_END_VERSIONED_NAMESPACE_DECL
199 #if defined (__ACE_INLINE__)
200 #include "ace/Mutex.inl"
201 #endif /* __ACE_INLINE__ */
203 #include /**/ "ace/post.h"
205 #endif /* ACE_MUTEX_H */