00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
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