00001 //====================================================================== 00002 // File: mutex.cpp 00003 // Author: Matthias Toussaint 00004 // Created: Sat Nov 25 18:56:16 CET 2006 00005 // Project: QtDMM 00006 // Description: Simplest possible posix mutex class 00007 //---------------------------------------------------------------------- 00008 // This file may be used under the terms of the GNU General Public 00009 // License version 2.0 as published by the Free Software Foundation 00010 // and appearing in the file LICENSE.GPL included in the packaging of 00011 // this file. 00012 // 00013 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING 00014 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00015 // PURPOSE. 00016 //---------------------------------------------------------------------- 00017 // Copyright 2006 Matthias Toussaint 00018 //====================================================================== 00019 00020 #include <mutex.h> 00021 #include <errno.h> 00022 00023 Mutex::Mutex() 00024 { 00025 pthread_mutex_init( &m_mutex, 0 ); 00026 } 00027 00028 Mutex::~Mutex() 00029 { 00030 } 00031 00032 int Mutex::lock() 00033 { 00034 return ::pthread_mutex_lock( &m_mutex ); 00035 } 00036 00037 int Mutex::unlock() 00038 { 00039 return ::pthread_mutex_unlock( &m_mutex ); 00040 } 00041 00042 bool Mutex::tryLock() 00043 { 00044 return (::pthread_mutex_trylock( &m_mutex ) != EBUSY); 00045 } 00046 00047 bool Mutex::isLocked() 00048 { 00049 bool locked = (::pthread_mutex_trylock( &m_mutex ) == EBUSY); 00050 00051 if (locked) ::pthread_mutex_unlock( &m_mutex ); 00052 00053 return locked; 00054 }