S3FC project page | S3FC home page |
00001 /* 00002 * Stone Three Foundation Class (s3fc) provides a number of utility classes. 00003 * Copyright (C) 2001 by Stone Three Signal Processing (Pty) Ltd. 00004 * 00005 * Authored by Stone Three Signal Processing (Pty) Ltd. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * Please see the file 'COPYING' in the source root directory. 00022 */ 00023 00033 #include <s3fc/s3_mutex.h> 00034 #include <s3fc/s3_exception.h> 00035 #include <sstream> 00036 #include <errno.h> 00037 00038 #ifndef CONST_OVERRIDE_MUTEX_T 00039 //#define CONST_OVERRIDE_MUTEX_T(x) const_cast<pthread_mutex_t*>(x) 00040 #define CONST_OVERRIDE_MUTEX_T(x) (x) 00041 #endif 00042 00043 // init mutex 00044 s3_mutex::s3_mutex() 00045 { 00046 // default init (fast mutex) 00047 pthread_mutex_init(CONST_OVERRIDE_MUTEX_T(&mutex), 0); 00048 } 00049 00050 // destroy mutex 00051 s3_mutex::~s3_mutex() 00052 { 00053 if ( pthread_mutex_destroy(CONST_OVERRIDE_MUTEX_T(&mutex)) != 0 ) 00054 { 00055 // To throw an exception here would be naughty. we rather unlock and leave. 00056 s3_mutex::unlock(); 00057 // throw s3_generic_exception("s3_mutex::~s3_mutex()", 00058 // "Attempted to destroy locked mutex"); 00059 } 00060 } 00061 00062 // this should NEVER be called 00063 s3_mutex::s3_mutex(const s3_mutex& src) 00064 { 00065 throw s3_generic_exception("s3_mutex::s3_mutex(const s3_mutex&)", 00066 "Function should *never* be called."); 00067 } 00068 00069 // this should NEVER be called 00070 s3_mutex& s3_mutex::operator=(const s3_mutex& src) 00071 { 00072 throw s3_generic_exception("s3_mutex::operator=(const s3_mutex&)", 00073 "Function should *never* be called."); 00074 } 00075 00076 // call pthread_mutex_lock 00077 void s3_mutex::lock() const 00078 { 00079 if ( pthread_mutex_lock(CONST_OVERRIDE_MUTEX_T(&mutex)) != 0 ) 00080 { 00081 throw s3_generic_exception("s3_mutex::lock()", 00082 "Mutex uninitialised."); 00083 } 00084 } 00085 00086 // call pthread_mutex_trylock 00087 bool s3_mutex::try_lock() const 00088 { 00089 int ev = pthread_mutex_trylock(CONST_OVERRIDE_MUTEX_T(&mutex)); 00090 bool rv = false; 00091 if ( ev == 0 ) 00092 { 00093 rv = true; // lock succeeded 00094 } 00095 else if ( ev == EBUSY ) 00096 { 00097 rv = false; // could not acquire lock 00098 } 00099 else if ( ev == EINVAL ) 00100 { 00101 throw s3_generic_exception("s3_mutex::trylock()", 00102 "Mutex uninitialised."); 00103 } 00104 else 00105 { 00106 std::stringstream str; 00107 str << "Unknown error: '" << strerror(rv) << "'"; 00108 throw s3_generic_exception("s3_mutex::trylock()", 00109 str.str()); 00110 } 00111 00112 return rv; 00113 } 00114 00115 // call pthread_mutex_unlock 00116 void s3_mutex::unlock() const 00117 { 00118 if ( pthread_mutex_unlock(CONST_OVERRIDE_MUTEX_T(&mutex)) != 0 ) 00119 { 00120 throw s3_generic_exception("s3_mutex::unlock()", 00121 "Mutex uninitialised."); 00122 } 00123 }
Send comments to: s3fc@stonethree.com |
|