ACE_Mutex wrapper (valid in same process or across processes (depending on TYPE flag)). In general, however, we recommend using ACE_Process_Mutex or ACE_Thread_Mutex rather than ACE_Mutex.
More...
#include <Mutex.h>
Public Member Functions | |
| ACE_Mutex (int type=USYNC_THREAD, const ACE_TCHAR *name=0, ACE_mutexattr_t *arg=0, mode_t mode=ACE_DEFAULT_FILE_PERMS) | |
| Initialize the mutex. | |
| ~ACE_Mutex (void) | |
| Implicitly destroy the mutex. | |
| int | remove (void) |
| Explicitly destroy the mutex. | |
| int | acquire (void) |
| Acquire lock ownership (wait on queue if necessary). | |
| int | acquire (ACE_Time_Value &tv) |
| int | acquire (ACE_Time_Value *tv) |
| int | tryacquire (void) |
| Conditionally acquire lock (i.e., don't wait on queue). | |
| int | release (void) |
| Release lock and unblock a thread at head of queue. | |
| int | acquire_read (void) |
| Acquire mutex ownership. | |
| int | acquire_write (void) |
| Acquire mutex ownership. | |
| int | tryacquire_read (void) |
| Conditionally acquire mutex (i.e., won't block). | |
| int | tryacquire_write (void) |
| Conditionally acquire mutex (i.e., won't block). | |
| int | tryacquire_write_upgrade (void) |
| const ACE_mutex_t & | lock (void) const |
| Return the underlying mutex. | |
| void | dump (void) const |
| Dump the state of an object. | |
Public Attributes | |
| ACE_ALLOC_HOOK_DECLARE | |
| Declare the dynamic allocation hooks. | |
| ACE_mutex_t | lock_ |
| Mutex type supported by the OS. | |
| bool | removed_ |
Private Member Functions | |
| void | operator= (const ACE_Mutex &) |
| ACE_Mutex (const ACE_Mutex &) | |
ACE_Mutex wrapper (valid in same process or across processes (depending on TYPE flag)). In general, however, we recommend using ACE_Process_Mutex or ACE_Thread_Mutex rather than ACE_Mutex.
Definition at line 50 of file Mutex.h.
| ACE_Mutex::ACE_Mutex | ( | int | type = USYNC_THREAD, |
|
| const ACE_TCHAR * | name = 0, |
|||
| ACE_mutexattr_t * | arg = 0, |
|||
| mode_t | mode = ACE_DEFAULT_FILE_PERMS | |||
| ) |
Initialize the mutex.
Definition at line 38 of file Mutex.cpp.
: #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) process_lock_ (0), lockname_ (0), #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ removed_ (false) { // ACE_TRACE ("ACE_Mutex::ACE_Mutex"); // These platforms need process-wide mutex to be in shared memory. #if defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS) if (type == USYNC_PROCESS) { // Let's see if the shared memory entity already exists. ACE_HANDLE fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT | O_EXCL, mode); if (fd == ACE_INVALID_HANDLE) { if (errno == EEXIST) fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT, mode); else return; } else { // We own this shared memory object! Let's set its size. if (ACE_OS::ftruncate (fd, sizeof (ACE_mutex_t)) == -1) { ACE_OS::close (fd); return; } this->lockname_ = ACE_OS::strdup (name); if (this->lockname_ == 0) { ACE_OS::close (fd); return; } } this->process_lock_ = (ACE_mutex_t *) ACE_OS::mmap (0, sizeof (ACE_mutex_t), PROT_RDWR, MAP_SHARED, fd, 0); ACE_OS::close (fd); if (this->process_lock_ == MAP_FAILED) return; if (this->lockname_ && ACE_OS::mutex_init (this->process_lock_, type, name, arg) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_Mutex::ACE_Mutex"))); return; } } else { // local mutex init if USYNC_PROCESS flag is not enabled. #else ACE_UNUSED_ARG (mode); #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ if (ACE_OS::mutex_init (&this->lock_, type, name, arg) != 0) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_Mutex::ACE_Mutex"))); #if defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS) } #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ }
| ACE_Mutex::~ACE_Mutex | ( | void | ) |
| ACE_Mutex::ACE_Mutex | ( | const ACE_Mutex & | ) | [private] |
| int ACE_Mutex::acquire | ( | void | ) | [inline] |
Acquire lock ownership (wait on queue if necessary).
Definition at line 73 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::acquire");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_lock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_lock (&this->lock_);
}
| int ACE_Mutex::acquire | ( | ACE_Time_Value * | tv | ) | [inline] |
Block the thread until the mutex is acquired or *tv times out, in which case -1 is returned and errno == ETIME. If tv == 0 then call acquire() directly. Otherwise, block the thread until the mutex is acquired or tv times out, in which case -1 is returned and errno == ETIME.
Definition at line 95 of file Mutex.inl.
{
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_lock (this->process_lock_, tv);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS*/
return ACE_OS::mutex_lock (&this->lock_, tv);
}
| int ACE_Mutex::acquire | ( | ACE_Time_Value & | tv | ) | [inline] |
Block the thread until the mutex is acquired or tv times out, in which case -1 is returned and errno == ETIME.
Definition at line 84 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::acquire");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_lock (this->process_lock_, tv);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS*/
return ACE_OS::mutex_lock (&this->lock_, tv);
}
| int ACE_Mutex::acquire_read | ( | void | ) | [inline] |
Acquire mutex ownership.
This calls acquire and is only here to make the ACE_Mutex interface consistent with the other synchronization APIs.
Definition at line 11 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::acquire_read");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_lock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_lock (&this->lock_);
}
| int ACE_Mutex::acquire_write | ( | void | ) | [inline] |
Acquire mutex ownership.
This calls acquire and is only here to make the ACE_Mutex interface consistent with the other synchronization APIs.
Definition at line 22 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::acquire_write");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_lock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_lock (&this->lock_);
}
| void ACE_Mutex::dump | ( | void | ) | const |
Dump the state of an object.
| const ACE_mutex_t & ACE_Mutex::lock | ( | void | ) | const [inline] |
| void ACE_Mutex::operator= | ( | const ACE_Mutex & | ) | [private] |
| int ACE_Mutex::release | ( | void | ) | [inline] |
Release lock and unblock a thread at head of queue.
Definition at line 116 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::release");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_unlock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_unlock (&this->lock_);
}
| int ACE_Mutex::remove | ( | void | ) | [inline] |
Explicitly destroy the mutex.
Definition at line 127 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::remove");
int result = 0;
#if defined (ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
// 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.
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_);
ACE_OS::free (
static_cast<void *> (
const_cast<ACE_TCHAR *> (this->lockname_)));
}
}
}
else
{
#else /* !ACE_HAS_PTHREADS && !ACE_HAS_STHREADS */
if (this->removed_ == false)
{
this->removed_ = true;
result = ACE_OS::mutex_destroy (&this->lock_);
}
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
#if defined (ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
}
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return result;
}
| int ACE_Mutex::tryacquire | ( | void | ) | [inline] |
Conditionally acquire lock (i.e., don't wait on queue).
errno is set to EBUSY. Definition at line 105 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::tryacquire");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_trylock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_trylock (&this->lock_);
}
| int ACE_Mutex::tryacquire_read | ( | void | ) | [inline] |
Conditionally acquire mutex (i.e., won't block).
This calls tryacquire and is only here to make the ACE_Mutex interface consistent with the other synchronization APIs.
errno is set to EBUSY. Definition at line 33 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::tryacquire_read");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_trylock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_trylock (&this->lock_);
}
| int ACE_Mutex::tryacquire_write | ( | void | ) | [inline] |
Conditionally acquire mutex (i.e., won't block).
This calls tryacquire and is only here to make the ACE_Mutex interface consistent with the other synchronization APIs.
errno is set to EBUSY. Definition at line 55 of file Mutex.inl.
{
// ACE_TRACE ("ACE_Mutex::tryacquire_write");
#if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
if (this->process_lock_)
return ACE_OS::mutex_trylock (this->process_lock_);
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
return ACE_OS::mutex_trylock (&this->lock_);
}
| int ACE_Mutex::tryacquire_write_upgrade | ( | void | ) | [inline] |
| bool ACE_Mutex::removed_ |
Keeps track of whether remove has been called yet to avoid multiple remove 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 remove on the same object, which is a bad idea anyway.
1.7.0