Semaphore.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 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    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_Semaphore_hxx
00024 #define __ccxx_Semaphore_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/Lock.h++>
00028 #include <commonc++/Permissions.h++>
00029 #include <commonc++/String.h++>
00030 #include <commonc++/SystemException.h++>
00031 
00032 #ifdef CCXX_OS_POSIX
00033 #include <fcntl.h>
00034 #include <semaphore.h>
00035 #endif
00036 
00037 namespace ccxx {
00038 
00052 class COMMONCPP_API Semaphore : public Lock
00053 {
00054   public:
00055 
00066   Semaphore(const String &name, uint_t value = 1,
00067             const Permissions& perm = Permissions::USER_READ_WRITE);
00068 
00072   ~Semaphore() throw();
00073 
00079   void init() throw(SystemException);
00080 
00088   bool wait() throw();
00089 
00091   inline void lock() throw()
00092   { wait(); }
00093 
00100   bool tryWait() throw();
00101 
00108   bool signal() throw();
00109 
00112   inline void unlock() throw()
00113   { signal(); }
00114 
00116   int getValue() const throw();
00117 
00119   inline String getName() const
00120   { return(_name); }
00121 
00122   private:
00123 
00124   String _name;
00125   uint_t _value;
00126   Permissions _perm;
00127   bool _initialized;
00128 
00129 #ifdef CCXX_OS_WINDOWS
00130   HANDLE _sem;
00131 #else
00132   sem_t *_sem;
00133 #endif
00134 };
00135 
00136 }; // namespace ccxx
00137 
00138 #endif // __ccxx_Semaphore_hxx
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3