You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.0 KiB

14 years ago
#include "Mutex.hpp"
#include "Common.hpp"
#include <time.h>
14 years ago
namespace {
pthread_mutex_t& MutexCtor( pthread_mutex_t& mutex )
{
pthread_mutex_init( &mutex, 0 );
return mutex;
}
} // anonym namespace
Mutex::Mutex( MutexType type )
: m_mutex( MutexCtor( m_mutex ) ) // init with function
, m_type( type )
14 years ago
{
TRACE;
if ( type != PTHREAD_MUTEX_DEFAULT ) {
pthread_mutexattr_init( &m_attr );
pthread_mutexattr_settype( &m_attr, type );
pthread_mutex_init( &m_mutex, &m_attr );
}
14 years ago
}
Mutex::~Mutex()
{
TRACE;
pthread_mutex_destroy ( &m_mutex );
14 years ago
}
int Mutex::lock()
14 years ago
{
TRACE;
return pthread_mutex_lock( &m_mutex );
14 years ago
}
int Mutex::unlock()
14 years ago
{
TRACE;
return pthread_mutex_unlock ( &m_mutex );
}
int Mutex::tryLock( const long int intervalSec, const long int intervalNSec )
{
TRACE;
if ( intervalSec == 0 and intervalNSec == 0 ) {
return pthread_mutex_trylock( &m_mutex );
} else {
timespec tspec = addTotimespec( intervalSec, intervalSec );
return pthread_mutex_timedlock( &m_mutex, &tspec );
}
}