mutex.cpp

Go to the documentation of this file.
00001 
00008 /**************************************************************************
00009 
00010    begin                : Sun Sep 10 2007
00011    copyright            : (C) 2007 by Ewald Arnold
00012    email                : log4sendpp at ewald-arnold dot de
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU Lesser General Public License as
00016    published by the Free Software Foundation; either version 2 of the License,
00017    or (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU Lesser General Public License
00025    along with this program; if not, write to the Free Software
00026    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027 
00028  **/
00029 
00031 #define LOG4SENDPP_NEED_EXPORTS
00032 #include <log4sendpp/log4sendpp.h>  // always first header
00033 
00034 #include <cerrno>
00035 
00036 #ifdef __unix__
00037 #include <pthread.h>
00038 #endif
00039 
00040 #include <log4sendpp/mutex.h>
00041 #include <log4sendpp/exception.h>
00042 
00043 
00044 LOG4SENDPP_NS_START
00045 
00046 
00047 LOG4SENDPP_API_IMPL0 Mutex::Mutex()
00048 {
00049 #if defined __WIN32__
00050   if ((handle = CreateMutex(0, false, 0)) < 0)
00051 #else
00052   if (pthread_mutex_init(&handle, 0) != 0)
00053 #endif
00054     LOG4SENDPP_THROW(LOG4SENDPP_NS::Exception(__LINE__, __FILE__, "Could not create mutex"));
00055 }
00056 
00057 
00058 LOG4SENDPP_API_IMPL0 Mutex::~Mutex()
00059 {
00060 #if defined __WIN32__
00061   CloseHandle(handle);
00062 #elif _THREAD
00063   pthread_mutex_destroy(&handle);
00064 #endif
00065 }
00066 
00067 
00068 LOG4SENDPP_API_IMPL(void) Mutex::lock()
00069 {
00070 #if defined __WIN32__
00071   if (WaitForSingleObject(handle, INFINITE) == WAIT_TIMEOUT)
00072 #else
00073   if (pthread_mutex_lock(&handle) != 0)
00074 #endif
00075     LOG4SENDPP_THROW(LOG4SENDPP_NS::Exception(__LINE__, __FILE__, "Could not lock mutex"));
00076 }
00077 
00078 
00079 LOG4SENDPP_API_IMPL(void) Mutex::unlock()
00080 {
00081 #if defined __WIN32__
00082   if (!ReleaseMutex(handle))
00083 #else
00084   if (pthread_mutex_unlock(&handle)!=0)
00085 #endif
00086     LOG4SENDPP_THROW(LOG4SENDPP_NS::Exception(__LINE__, __FILE__, "Could not unlock mutex"));
00087 }
00088 
00089 
00090 LOG4SENDPP_API_IMPL(bool) Mutex::tryLock()
00091 {
00092 #if defined __WIN32__
00093 
00094   DWORD ret = WaitForSingleObject(handle, 1);
00095 
00096   if (ret == WAIT_FAILED)
00097     return false;
00098 
00099   if (ret == WAIT_TIMEOUT)
00100     return false;
00101 
00102   if (ret == WAIT_ABANDONED)
00103     return false;
00104 
00105   if (ret != WAIT_OBJECT_0)
00106     return false;
00107 
00108 #else
00109 
00110   int ret = pthread_mutex_trylock(&handle);
00111   if (ret == EBUSY)
00112     return false;
00113 
00114   if (ret != 0)
00115     return false;
00116 
00117 #endif
00118 
00119   return true;
00120 }
00121 
00122 
00124 //
00125 
00126 
00127 LOG4SENDPP_API_IMPL0 Mutex::Locker::Locker(Mutex &mtx)
00128   : mutex(&mtx)
00129 {
00130   mutex->lock();
00131 }
00132 
00133 
00134 LOG4SENDPP_API_IMPL0 Mutex::Locker::~Locker()
00135 {
00136   LOG4SENDPP_TRY
00137   {
00138     mutex->unlock();  // might throw, forget it
00139   }
00140   LOG4SENDPP_CATCH_ALL
00141   {
00142   }
00143 }
00144 
00145 
00147 //
00148 
00149 
00150 LOG4SENDPP_API_IMPL0 Mutex::TryLocker::TryLocker(Mutex &mtx)
00151   : mutex(&mtx)
00152 {
00153   locked = mutex->tryLock();
00154 }
00155 
00156 
00157 LOG4SENDPP_API_IMPL0 Mutex::TryLocker::~TryLocker()
00158 {
00159   LOG4SENDPP_TRY
00160   {
00161     if (isLocked())
00162       mutex->unlock();  // might throw, forget it
00163   }
00164   LOG4SENDPP_CATCH_ALL
00165   {
00166   }
00167 }
00168 
00169 
00170 LOG4SENDPP_API_IMPL(bool) Mutex::TryLocker::isLocked()
00171 {
00172   return locked;
00173 }
00174 
00175 
00176 LOG4SENDPP_NS_END
00177 

Generated on Sat Nov 24 14:41:22 2007 for log4sendpp by  doxygen 1.5.3