Fixed typos
[ACE_TAO.git] / ACE / ace / Mutex.h
blob159f57da0386928eb0ffdc28f6e4d408ad83f1cf
1 // -*- C++ -*-
3 //==========================================================================
4 /**
5 * @file Mutex.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
9 //==========================================================================
11 #ifndef ACE_MUTEX_H
12 #define ACE_MUTEX_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # 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
38 class ACE_Time_Value;
40 /**
41 * @class ACE_Mutex
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
50 public:
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.
58 ~ACE_Mutex (void);
60 /// Explicitly destroy the mutex.
61 /**
62 * @note Only one thread should call this method since it doesn't
63 * protect against race conditions.
65 int remove (void);
67 /// Acquire lock ownership (wait on queue if necessary).
68 int acquire (void);
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.
72 /**
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.
81 /**
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).
93 /**
94 * @return -1 on failure. If we "failed" because someone
95 * else already had the lock, @c errno is set to @c EBUSY.
97 int tryacquire (void);
99 /// Release lock and unblock a thread at head of queue.
100 int release (void);
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.
107 int acquire_read (void);
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 (void);
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 (void);
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 (void);
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 (void);
143 /// Return the underlying mutex.
144 const ACE_mutex_t &lock (void) const;
145 ACE_mutex_t &lock (void);
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.
152 void dump (void) const;
154 /// Declare the dynamic allocation hooks.
155 ACE_ALLOC_HOOK_DECLARE;
157 // = This should be protected but some C++ compilers complain...
158 public:
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 || defined ACE_HAS_STHREADS
165 # define ACE_MUTEX_USE_PROCESS_LOCK
166 # define ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
167 typedef ACE_mutex_t Process_Lock;
168 #endif
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
177 * can destroy it).
179 const ACE_TCHAR *lockname_;
180 #endif /* ACE_MUTEX_USE_PROCESS_LOCK */
182 /// Mutex type supported by the OS.
183 ACE_mutex_t lock_;
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.
190 bool removed_;
192 private:
193 // Prevent assignment and initialization.
194 ACE_Mutex &operator= (const ACE_Mutex &);
195 ACE_Mutex (const ACE_Mutex &);
198 ACE_END_VERSIONED_NAMESPACE_DECL
200 #if defined (__ACE_INLINE__)
201 #include "ace/Mutex.inl"
202 #endif /* __ACE_INLINE__ */
204 #include /**/ "ace/post.h"
206 #endif /* ACE_MUTEX_H */