Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SemaphoreManager.h

Go to the documentation of this file.
00001 //-*-c++-*-
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 //! initializes, manages, and releases a set of System V style semaphores
00018 /*! Should be initialized pre-fork into a shared region */
00019 class SemaphoreManager {
00020 protected:
00021   static const unsigned int MAX_SEM=SYSTEM_MAX_SEM; //!wouldn't want to claim the entire system's worth, even if we could
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 /*! @file
00060  * @brief Defines SemaphoreManager, which initializes, manages, and releases a set of System V style semaphores
00061  * @author ejt (Creator)
00062  *
00063  * $Author: ejt $
00064  * $Name: tekkotsu-2_4_1 $
00065  * $Revision: 1.5 $
00066  * $State: Exp $
00067  * $Date: 2005/06/15 03:40:05 $
00068  */
00069 
00070 #endif //Aperios check
00071 
00072 #endif //INCLUDED
00073 

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4