c++/mutex.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 Beeyond Software Holding BV
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00025 #ifndef _CLASS_BEECRYPT_MUTEX_H
00026 #define _CLASS_BEECRYPT_MUTEX_H
00027 
00028 #include "beecrypt/api.h"
00029 
00030 #ifdef __cplusplus
00031 
00032 #if HAVE_ERRNO_H
00033 # include <errno.h>
00034 #endif
00035 
00036 namespace beecrypt {
00037     class BEECRYPTCXXAPI mutex
00038     {
00039     private:
00040         bc_mutex_t _lock;
00041 
00042     public:
00043         inline void init() throw (char*)
00044         {
00045             #if WIN32
00046             _lock = CreateMutex((LPSECURITY_ATTRIBUTES) 0, FALSE, (LPCSTR) 0);
00047             if (!_lock)
00048                 throw "CreateMutex failed";
00049             #else
00050             register int rc;
00051             # if HAVE_SYNCH_H
00052             if ((rc = mutex_init(&_lock, USYNC_THREAD, 0)))
00053                 throw strerror(rc);
00054             # elif HAVE_PTHREAD_H
00055             if ((rc = pthread_mutex_init(&_lock, 0)))
00056                 throw strerror(rc);
00057             # else
00058             #  error
00059             # endif
00060             #endif
00061         }
00062 
00063         inline void lock() throw (char*)
00064         {
00065             #if WIN32
00066             if (WaitForSingleObject(_lock, INFINITE) == WAIT_OBJECT_0)
00067                 return;
00068             throw "WaitForSingleObject failed";
00069             #else
00070             register int rc;
00071             # if HAVE_SYNCH_H
00072             if ((rc = mutex_lock(&_lock)))
00073                 throw strerror(rc);
00074             # elif HAVE_PTHREAD_H
00075             if ((rc = pthread_mutex_lock(&_lock)))
00076                 throw strerror(rc);
00077             # else
00078             #  error
00079             # endif
00080             #endif
00081         }
00082 
00083         inline bool trylock() throw (char*)
00084         {
00085             #if WIN32
00086             switch (WaitForSingleObject(_lock, 0))
00087             {
00088             case WAIT_TIMEOUT:
00089                 return false;
00090             case WAIT_OBJECT_0:
00091                 return true;
00092             default:
00093                 throw "WaitForSingleObbject failed";
00094             }
00095             #else
00096             register int rc;
00097             # if HAVE_SYNCH_H
00098             if ((rc = mutex_trylock(&_lock)) == 0)
00099                 return true;
00100             if (rc == EBUSY)
00101                 return false;
00102             throw strerror(rc);
00103             # elif HAVE_PTHREAD_H
00104             if ((rc = pthread_mutex_trylock(&_lock)) == 0)
00105                 return true;
00106             if (rc == EBUSY)
00107                 return false;
00108             throw strerror(rc);
00109             # else
00110             #  error
00111             # endif
00112             #endif
00113         }
00114 
00115         inline void unlock() throw (char*)
00116         {
00117             #if WIN32
00118             if (!ReleaseMutex(_lock))
00119                 throw "ReleaseMutex failed";
00120             #else
00121             register int rc;
00122             # if HAVE_SYNCH_H
00123             if ((rc = mutex_unlock(&_lock)))
00124                 throw strerror(rc);
00125             # elif HAVE_PTHREAD_H
00126             if ((rc = pthread_mutex_unlock(&_lock)))
00127                 throw strerror(rc);
00128             # else
00129             #  error
00130             # endif
00131             #endif
00132         }
00133 
00134         inline void destroy() throw (char*)
00135         {
00136             #if WIN32
00137             if (!CloseHandle(_lock))
00138                 throw "CloseHandle failed";
00139             #else
00140             register int rc;
00141             # if HAVE_SYNCH_H
00142             if ((rc = mutex_destroy(&_lock)))
00143                 throw strerror(rc);
00144             # elif HAVE_PTHREAD_H
00145             if ((rc = pthread_mutex_destroy(&_lock)))
00146                 throw strerror(rc);
00147             # else
00148             #  error
00149             # endif
00150             #endif
00151         }
00152     };
00153 }
00154 
00155 #endif
00156 
00157 #endif

Generated on Wed Aug 22 06:28:00 2007 for BeeCrypt by  doxygen 1.5.2