void Mutex::unlock() { if (!is_held_by_current_thread()) { throw MutexError("thread does not own mutex"); } _tid = INVALID_ID; _lock_depth -= 1; _real_mutex.unlock(); }
void Mutex::lock() { if (is_held_by_current_thread()) { throw MutexError("thread already owns mutex"); } _real_mutex.lock(); _tid = boost::this_thread::get_id(); _lock_depth += 1; }
Mutex::Mutex(void) { #if defined(DUNE_SYS_HAS_PTHREAD_MUTEX) int rv = 0; if ((rv = pthread_mutexattr_init(&m_attr)) != 0) throw MutexError(rv); // Enable priority inheritance if supported. # if (_POSIX_THREAD_PRIO_INHERIT != -1) && defined(PTHREAD_PRIO_INHERIT) if ((rv = pthread_mutexattr_setprotocol(&m_attr, PTHREAD_PRIO_INHERIT)) != 0) throw MutexError(rv); # endif if ((rv = pthread_mutex_init(&m_mutex, &m_attr)) != 0) throw MutexError(rv); #endif }
void Mutex::Init(MutexType _type) { mutex = (pthread_mutex_t*) new pthread_mutex_t; type = _type; pthread_mutexattr_t attr; if(pthread_mutexattr_init(&attr) != 0) { std::cerr << "pthread_attr_init: " << strerror(errno); throw MutexError(); } switch(type) { case NORMAL_MUTEX: //#ifdef DEBUG if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) { std::cerr << "pthread_mutexattr_settype: " << strerror(errno) << std::endl; throw MutexError(); } //#else // pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); //#endif break; case RECURSIVE_MUTEX: if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) { std::cerr << "pthread_mutexattr_settype: " << strerror(errno) << std::endl; throw MutexError(); } break; default: assert(false); } if(pthread_mutex_init(mutex, &attr) != 0) { std::cerr << "pthread_mutex_init: " << strerror(errno) << std::endl; throw MutexError(); } pthread_mutexattr_destroy(&attr); }
void Mutex::tryLock(void) { #if defined(DUNE_SYS_HAS_PTHREAD_MUTEX) int rv = pthread_mutex_trylock(&m_mutex); if (rv != 0) throw MutexError("tryLock", rv); #endif }
void Mutex::destroy(void) { #if defined(DUNE_SYS_HAS_PTHREAD_MUTEX) int rv = pthread_mutex_destroy(&m_mutex); if (rv != 0) throw MutexError("destroy", rv); #endif }