• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/threading/Mutex.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL 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
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef JMUTEX_H
00018 #define JMUTEX_H
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 
00023 #ifdef _WINDOWS
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028 
00029 #include <spl/Memory.h>
00030 #include <spl/RefCountPtr.h>
00031 
00032 #ifdef _WINDOWS
00033 #define _WINSOCKAPI_
00034 #include <spl/cleanwindows.h>
00035 #endif
00036 
00037 #ifdef HAVE_SPTHREAD_H
00038 #include <sys/types.h>
00039 #include <spt_types.h>
00040 #include <sys/time.h>
00041 #include <wchar.h>
00042 #include <signal.h>
00043 #include <spt_extensions.h>
00044 /* also need LDFLAGS="-spthread" */
00045 #endif
00046 
00047 #ifdef HAVE_PTHREAD_H
00048 #include <pthread.h>
00049 #endif
00050 
00051 #ifdef HAVE_SEMAPHORE_H
00052 #include <semaphore.h>
00053 #endif
00054 
00055 #include <spl/Debug.h>
00056 #include <spl/WeakReference.h>
00057 
00058 #ifndef INFINITE
00059 #define INFINITE 0
00060 #endif
00061 
00068 class Mutex;
00069 typedef RefCountPtr<Mutex> MutexPtr;
00070 typedef WeakReference<Mutex, MutexPtr> MutexRef;
00071 
00072 REGISTER_TYPEOF( 460, MutexPtr );
00073 REGISTER_TYPEOF( 462, MutexRef );
00074 
00076 class Mutex
00077 {
00078 private:
00079         // forbid copy constructor
00080         inline Mutex(const Mutex& m) {}
00081         inline void operator =(const Mutex& m) {}
00082 
00083 private:
00084 
00085 #ifdef _WINDOWS
00086         HANDLE mutex;
00087 #else 
00088         // pthread mutex
00089         pthread_mutex_t mutex;
00090 #endif 
00091 
00092         bool initialized;
00093         int m_timeoutMs;
00094 
00095         void Init();
00096 
00097 public:
00098         Mutex();
00099         Mutex(int timeoutMs);
00100         virtual ~Mutex();
00101 
00102         bool Lock();
00103         void Unlock();
00104 
00105         inline bool IsInitialized() { return initialized; }
00106 
00108         inline void SetTimeout( int timeoutMs ) { m_timeoutMs = timeoutMs; }
00109 };
00110 
00111 inline void ValidateType(const Mutex& mtx)
00112 {
00113 }
00114 
00115 inline void ValidateType(const Mutex *mtx)
00116 {
00117         if ( NULL != mtx )
00118         {
00119                 DEBUG_NOTE_MEM(mtx);
00120                 ASSERT_MEM(mtx, sizeof(Mutex));
00121         }
00122 }
00123 
00124 REGISTER_TYPEOF( 464, Mutex );
00125 
00128 #endif