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