reSIProcate/rutil  9694
Public Member Functions | Private Member Functions | Private Attributes | Friends
resip::Mutex Class Reference

A semaphore that can be locked by only one thread at a time. More...

#include <Mutex.hxx>

Inheritance diagram for resip::Mutex:
Inheritance graph
[legend]
Collaboration diagram for resip::Mutex:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Mutex ()
virtual ~Mutex ()
virtual void lock ()
virtual void unlock ()

Private Member Functions

 Mutex (const Mutex &)
Mutexoperator= (const Mutex &)
pthread_mutex_t * getId () const

Private Attributes

pthread_mutex_t mId

Friends

class Condition

Detailed Description

A semaphore that can be locked by only one thread at a time.

Used to serialize access to some resource (such as a data member). Here's an example:

     void
     Log::setLevel(Level level)
     {
        Lock lock(_mutex);
        _level = level; 
     }

As indicated in the example, you will probably never use any member function in this class. The 'Lock' class provides this functionality.

See also:
Lock
Condition

Definition at line 34 of file Mutex.hxx.


Constructor & Destructor Documentation

Mutex::Mutex ( )

Definition at line 22 of file Mutex.cxx.

References mId.

{
#ifndef WIN32
    int  rc = pthread_mutex_init(&mId,0);
    (void)rc;
    assert( rc == 0 );
#else
        // Note:  Windows Critical sections are recursive in nature and perhaps
        //        this implementation calls for a non-recursive implementation
        //        (since there also exists a RecursiveMutex class).  The effort
        //        to make this non-recursive just doesn't make sense though. (SLG)
        InitializeCriticalSection(&mId);
#endif
}
Mutex::~Mutex ( ) [virtual]

Definition at line 38 of file Mutex.cxx.

References mId.

{
#ifndef WIN32
    int  rc = pthread_mutex_destroy(&mId);
    (void)rc;
    assert( rc != EBUSY );  // currently locked 
    assert( rc == 0 );
#else
        DeleteCriticalSection(&mId);
#endif
}
resip::Mutex::Mutex ( const Mutex ) [private]

Member Function Documentation

pthread_mutex_t * Mutex::getId ( ) const [private]

Definition at line 81 of file Mutex.cxx.

References mId.

Referenced by resip::Condition::wait().

{
    return ( &mId );
}
void Mutex::lock ( ) [virtual]

Implements resip::Lockable.

Definition at line 52 of file Mutex.cxx.

References mId.

Referenced by resip::Condition::wait().

{
#ifndef WIN32
    int  rc = pthread_mutex_lock(&mId);
    (void)rc;
    assert( rc != EINVAL );
    assert( rc != EDEADLK );
    assert( rc == 0 );
#else
        EnterCriticalSection(&mId);
#endif
}
Mutex& resip::Mutex::operator= ( const Mutex ) [private]
void Mutex::unlock ( ) [virtual]

Implements resip::Lockable.

Definition at line 66 of file Mutex.cxx.

References mId.

Referenced by resip::Condition::wait().

{
#ifndef WIN32
    int  rc = pthread_mutex_unlock(&mId);
    (void)rc;
    assert( rc != EINVAL );
    assert( rc != EPERM );
    assert( rc == 0 );
#else
        LeaveCriticalSection(&mId);
#endif
}

Friends And Related Function Documentation

friend class Condition [friend]

Definition at line 36 of file Mutex.hxx.


Member Data Documentation

pthread_mutex_t resip::Mutex::mId [mutable, private]

Definition at line 54 of file Mutex.hxx.

Referenced by getId(), lock(), Mutex(), unlock(), and ~Mutex().


The documentation for this class was generated from the following files: