Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / ace / Mutex.inl
blob7adfceaa12c261512c44bd7e87bc25002cc8d5e6
1 // -*- C++ -*-
3 #include "ace/OS_NS_sys_mman.h"
5 #if defined (ACE_HAS_ALLOC_HOOKS)
6 # include "ace/Malloc_Base.h"
7 #endif /* ACE_HAS_ALLOC_HOOKS */
9 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
11 ACE_INLINE int
12 ACE_Mutex::acquire_read ()
14 // ACE_TRACE ("ACE_Mutex::acquire_read");
15 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
16   if (this->process_lock_)
17     return ACE_OS::mutex_lock (this->process_lock_);
18 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
19   if (this->process_lock_)
20     return ACE_OS::sema_wait (this->process_lock_);
21 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
22   return ACE_OS::mutex_lock (&this->lock_);
25 ACE_INLINE int
26 ACE_Mutex::acquire_write ()
28 // ACE_TRACE ("ACE_Mutex::acquire_write");
29 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
30   if (this->process_lock_)
31     return ACE_OS::mutex_lock (this->process_lock_);
32 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
33   if (this->process_lock_)
34     return ACE_OS::sema_wait (this->process_lock_);
35 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
36   return ACE_OS::mutex_lock (&this->lock_);
39 ACE_INLINE int
40 ACE_Mutex::tryacquire_read ()
42 // ACE_TRACE ("ACE_Mutex::tryacquire_read");
43 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
44   if (this->process_lock_)
45     return ACE_OS::mutex_trylock (this->process_lock_);
46 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
47   if (this->process_lock_)
48     return ACE_OS::sema_trywait (this->process_lock_);
49 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
50   return ACE_OS::mutex_trylock (&this->lock_);
53 ACE_INLINE const ACE_mutex_t &
54 ACE_Mutex::lock () const
56 // ACE_TRACE ("ACE_Mutex::lock");
57 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
58   if (this->process_lock_)
59     return *this->process_lock_;
60 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
61   return this->lock_;
64 ACE_INLINE ACE_mutex_t &
65 ACE_Mutex::lock ()
67 // ACE_TRACE ("ACE_Mutex::lock");
68 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
69   if (this->process_lock_)
70     return *this->process_lock_;
71 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
72   return this->lock_;
75 ACE_INLINE int
76 ACE_Mutex::tryacquire_write ()
78 // ACE_TRACE ("ACE_Mutex::tryacquire_write");
79 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
80   if (this->process_lock_)
81     return ACE_OS::mutex_trylock (this->process_lock_);
82 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
83   if (this->process_lock_)
84     return ACE_OS::sema_trywait (this->process_lock_);
85 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
86   return ACE_OS::mutex_trylock (&this->lock_);
89 ACE_INLINE int
90 ACE_Mutex::tryacquire_write_upgrade ()
92 // ACE_TRACE ("ACE_Mutex::tryacquire_write_upgrade");
93   return 0;
96 ACE_INLINE int
97 ACE_Mutex::acquire ()
99 // ACE_TRACE ("ACE_Mutex::acquire");
100 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
101   if (this->process_lock_)
102     return ACE_OS::mutex_lock (this->process_lock_);
103 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
104   if (this->process_lock_)
105     return ACE_OS::sema_wait (this->process_lock_);
106 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
107   return ACE_OS::mutex_lock (&this->lock_);
110 ACE_INLINE int
111 ACE_Mutex::acquire (ACE_Time_Value &tv)
113   // ACE_TRACE ("ACE_Mutex::acquire");
114 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
115   if (this->process_lock_)
116     return ACE_OS::mutex_lock (this->process_lock_, tv);
117 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
118   if (this->process_lock_)
119     return ACE_OS::sema_wait (this->process_lock_, tv);
120 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
121   return ACE_OS::mutex_lock (&this->lock_, tv);
124 ACE_INLINE int
125 ACE_Mutex::acquire (ACE_Time_Value *tv)
127 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
128   if (this->process_lock_)
129     return ACE_OS::mutex_lock (this->process_lock_, tv);
130 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
131   if (this->process_lock_)
132     return ACE_OS::sema_wait (this->process_lock_, tv);
133 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
134   return ACE_OS::mutex_lock (&this->lock_, tv);
137 ACE_INLINE int
138 ACE_Mutex::tryacquire ()
140 // ACE_TRACE ("ACE_Mutex::tryacquire");
141 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
142   if (this->process_lock_)
143     return ACE_OS::mutex_trylock (this->process_lock_);
144 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
145   if (this->process_lock_)
146     return ACE_OS::sema_trywait (this->process_lock_);
147 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
148   return ACE_OS::mutex_trylock (&this->lock_);
151 ACE_INLINE int
152 ACE_Mutex::release ()
154 // ACE_TRACE ("ACE_Mutex::release");
155 #ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
156   if (this->process_lock_)
157     return ACE_OS::mutex_unlock (this->process_lock_);
158 #elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
159   if (this->process_lock_)
160     return ACE_OS::sema_post (this->process_lock_);
161 #endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
162   return ACE_OS::mutex_unlock (&this->lock_);
165 ACE_INLINE int
166 ACE_Mutex::remove ()
168 // ACE_TRACE ("ACE_Mutex::remove");
169   int result = 0;
170 #ifdef ACE_MUTEX_USE_PROCESS_LOCK
171   // In the case of a interprocess mutex, the owner is the first
172   // process that created the shared memory object. In this case, the
173   // lockname_ pointer will be non-zero (points to allocated memory
174   // for the name).  Owner or not, the memory needs to be unmapped
175   // from the process.  If we are the owner, the file used for
176   // shm_open needs to be deleted as well.
177   if (this->process_lock_)
178     {
179       if (this->removed_ == false)
180         {
181           this->removed_ = true;
182           // Only destroy the lock if we're the ones who initialized
183           // it.
184 # ifdef ACE_MUTEX_PROCESS_LOCK_IS_MUTEX
185           if (!this->lockname_)
186             ACE_OS::munmap ((void *) this->process_lock_,
187                             sizeof (ACE_mutex_t));
188           else
189             {
190               result = ACE_OS::mutex_destroy (this->process_lock_);
191               ACE_OS::munmap ((void *) this->process_lock_,
192                               sizeof (ACE_mutex_t));
193               ACE_OS::shm_unlink (this->lockname_);
194             }
195 # elif defined ACE_MUTEX_PROCESS_LOCK_IS_SEMA
196           result = ACE_OS::sema_destroy (this->process_lock_);
197 # endif /* ACE_MUTEX_PROCESS_LOCK_IS_MUTEX */
199 # ifdef ACE_HAS_ALLOC_HOOKS
200           ACE_Allocator::instance ()->free (const_cast<ACE_TCHAR *> (
201                                                 this->lockname_));
202 # else
203           ACE_OS::free (const_cast<ACE_TCHAR *> (this->lockname_));
204 # endif /* ACE_HAS_ALLOC_HOOKS */
205         }
206       return result;
207     }
208 #endif /* ACE_MUTEX_USE_PROCESS_LOCK */
210   if (this->removed_ == false)
211     {
212       this->removed_ = true;
213       result = ACE_OS::mutex_destroy (&this->lock_);
214     }
216   return result;
219 ACE_END_VERSIONED_NAMESPACE_DECL