Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

LOW_thread_mutex_POSIX.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           LOW_thread_mutex_POSIX.cpp  -  description
00003                              -------------------
00004     begin                : Fri Oct 3 2003
00005     copyright            : (C) 2003 by Harald Roelle
00006     email                : roelle@informatik.uni-muenchen.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018  
00019 #include <errno.h>
00020 #include <pthread.h>
00021 
00022 
00023 #include "LOW_thread_mutex.h"
00024 #include "LOW_thread_mutex_POSIX.h"
00025 #include "LOW_helper_msglog.h"
00026 
00027 
00028 //=====================================================================================
00029 //
00030 // constructors
00031 //
00032 
00033 
00034 LOW_thread_mutex_POSIX::LOW_thread_mutex_POSIX( mutexKind_t inMutexType) : LOW_thread_mutex( inMutexType)
00035 {
00036   if ( int errVal=pthread_mutexattr_init( &mutexAttr) != 0 )
00037    throw thread_mutex_error( errVal, "pthread_mutexattr_init() failed", __FILE__, __LINE__);
00038 
00039   switch ( inMutexType ) {
00040 
00041     case mutexKind_fast:
00042       if ( int errVal=pthread_mutexattr_settype( &mutexAttr, PTHREAD_MUTEX_ERRORCHECK_NP) != 0 )
00043         throw thread_mutex_error( errVal, "pthread_mutexattr_setkind_np() failed", __FILE__, __LINE__);
00044       break;
00045 
00046     case mutexKind_recursive:
00047       if ( int errVal=pthread_mutexattr_settype( &mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) != 0 )
00048         throw thread_mutex_error( errVal, "pthread_mutexattr_setkind_np() failed", __FILE__, __LINE__);
00049       break;
00050 
00051     case mutexKind_errorCheck:
00052       if ( int errVal=pthread_mutexattr_settype( &mutexAttr, PTHREAD_MUTEX_ERRORCHECK_NP) != 0 )
00053         throw thread_mutex_error( errVal, "pthread_mutexattr_setkind_np() failed", __FILE__, __LINE__);
00054       break;
00055 
00056     default:
00057       throw thread_mutex_error( "unknown value of mutexKind_t", __FILE__, __LINE__);
00058   }
00059   
00060   if ( int errVal=pthread_mutex_init( &theMutex, &mutexAttr) != 0 )
00061     throw thread_mutex_error( errVal, "pthread_mutex_init() failed", __FILE__, __LINE__);
00062 }
00063 
00064 
00065 LOW_thread_mutex_POSIX::~LOW_thread_mutex_POSIX()
00066 {
00067   pthread_mutexattr_destroy( &mutexAttr); // ignore errors
00068 
00069   int errVal = pthread_mutex_destroy( &theMutex);
00070   if ( errVal == EBUSY ) // ignore other errors
00071     LOW_helper_msglog::printPerror( errVal, "~LOW_thread_mutex_POSIX: pthread_mutex_destroy() failed");
00072 }
00073 
00074 
00075 
00076 //=====================================================================================
00077 //
00078 // public methods
00079 //
00080 
00081 
00082 void LOW_thread_mutex_POSIX::lock()
00083 {
00084   int errVal = pthread_mutex_lock( &theMutex);
00085 
00086   if      ( errVal == EDEADLK )
00087     throw thread_mutex_locked( errVal, "mutex already locked", __FILE__, __LINE__);
00088   else if ( errVal != 0 )
00089     throw thread_mutex_error( errVal, "pthread_mutex_lock() failed", __FILE__, __LINE__);
00090 }
00091   
00092 
00093 void LOW_thread_mutex_POSIX::tryLock()
00094 {
00095   int errVal = pthread_mutex_trylock( &theMutex);
00096 
00097   if      ( errVal == EDEADLK )
00098     throw thread_mutex_locked( errVal, "mutex already locked", __FILE__, __LINE__);
00099   else if ( errVal == EBUSY )
00100     throw thread_mutex_busy( errVal, "mutex already locked by another thread", __FILE__, __LINE__);
00101   else if ( errVal != 0 )
00102     throw thread_mutex_error( errVal, "pthread_mutex_lock() failed", __FILE__, __LINE__);
00103 }
00104 
00105 
00106 void LOW_thread_mutex_POSIX::unlock()
00107 {
00108   int errVal = pthread_mutex_unlock( &theMutex);
00109 
00110   if      ( errVal == EPERM )
00111     throw thread_mutex_busy( errVal, "mutex already locked by another thread", __FILE__, __LINE__);
00112   else if ( errVal != 0 )
00113     throw thread_mutex_error( errVal, "pthread_mutex_lock() failed", __FILE__, __LINE__);
00114 }

Generated on Tue Feb 3 11:30:26 2004 for OneWireLibrary++ by doxygen 1.3.2