00001
00002 #ifndef INCLUDED_SemaphoreManager_h_
00003 #define INCLUDED_SemaphoreManager_h_
00004
00005 #ifdef PLATFORM_APERIOS
00006 # warning SemaphoreManager is not Aperios compatable, this is not going to compile
00007 #else
00008
00009 #include "ListMemBuf.h"
00010 #include <sys/types.h>
00011 #include <sys/sem.h>
00012
00013 #ifndef SYSTEM_MAX_SEM
00014 #define SYSTEM_MAX_SEM 250
00015 #endif
00016
00017
00018
00019 class SemaphoreManager {
00020 protected:
00021 static const unsigned int MAX_SEM=SYSTEM_MAX_SEM;
00022 typedef ListMemBuf<bool,MAX_SEM> sems_t;
00023 sems_t sems;
00024
00025 public:
00026 typedef sems_t::index_t semid_t;
00027
00028 SemaphoreManager();
00029 SemaphoreManager(const SemaphoreManager& mm);
00030 SemaphoreManager& operator=(const SemaphoreManager& mm);
00031 ~SemaphoreManager();
00032
00033 void aboutToFork();
00034 void faultShutdown();
00035 bool hadFault() const { return semid==-1; }
00036
00037 semid_t getSemaphore();
00038 void releaseSemaphore(semid_t id);
00039
00040 bool lower(semid_t id, unsigned int x, bool block=true) const;
00041 void raise(semid_t id, unsigned int x) const;
00042 int getValue(semid_t id) const;
00043 void setValue(semid_t id, int x) const;
00044 bool testZero(semid_t id, bool block=true) const;
00045 bool testZero_add(semid_t id, unsigned int x, bool testblock=true, bool addblock=true) const;
00046 bool add_testZero(semid_t id, unsigned int x, bool addblock=true, bool testblock=true) const;
00047
00048 unsigned int available() const { return sems_t::MAX_ENTRIES-sems.size(); }
00049 unsigned int used() const { return sems.size()-(sems_t::MAX_ENTRIES-nsem); }
00050 semid_t invalid() const { return sems.end(); }
00051
00052 protected:
00053 unsigned int nsem;
00054 int semid;
00055 semid_t mysem;
00056 semid_t refc;
00057 };
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #endif //Aperios check
00071
00072 #endif //INCLUDED
00073