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

spl/threading/Semaphore.h

00001 /* Original source attribution */
00002 /* ---------------------------------------------------------------------------
00003    commonc++ - A C++ Common Class Library
00004    Copyright (C) 2005-2009  Mark A Lindner
00005 
00006    Ported to SPL from commonc++.
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public
00019    License along with this library; if not, write to the Free
00020    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021    ---------------------------------------------------------------------------
00022 */
00023 
00024 #ifndef _semaphore_h
00025 #define _semaphore_h
00026 
00027 #include <spl/types.h>
00028 #include <spl/Exception.h>
00029 #include <spl/Memory.h>
00030 #include <spl/threading/Mutex.h>
00031 #include <spl/io/Permissions.h>
00032 #include <spl/String.h>
00033 #include <spl/RefCountPtr.h>
00034 
00035 #ifndef _WINDOWS
00036 #include <fcntl.h>
00037 #include <semaphore.h>
00038 #endif
00039 
00046 class Semaphore;
00047 typedef RefCountPtr<Semaphore> SemaphorePtr;
00048 typedef WeakReference<Semaphore, SemaphorePtr> SemaphoreRef;
00049 
00050 REGISTER_TYPEOF( 472, SemaphorePtr );
00051 REGISTER_TYPEOF( 475, SemaphoreRef );
00052 
00062 class Semaphore : public IMemoryValidate
00063 {
00064 private:
00065         String _name;
00066 
00067 #ifdef _WINDOWS
00068         HANDLE _sem;
00069 #else
00070         sem_t *_sem;
00071 #endif
00072 
00073 public:
00074 
00086         Semaphore
00087         (
00088                 const String& name, 
00089                 uint32 value = 1,
00090                 const Permissions& perm = Permissions::USER_READ_WRITE
00091         );
00092 
00095         ~Semaphore();
00096 
00104         void Lock();
00105 
00112         bool TryWait();
00113 
00120         void Unlock();
00121 
00124         int GetValue() const;
00125 
00127         inline const String& GetName() const
00128         { 
00129                 return(_name); 
00130         }
00131 
00132 #ifdef DEBUG
00133         virtual void ValidateMem() const;
00134         virtual void CheckMem() const;
00135 #endif
00136 };
00137 
00138 REGISTER_TYPEOF( 476, Semaphore );
00139 
00142 #endif