Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

FileDescPool.h

Go to the documentation of this file.
00001 #ifndef FileDescPool_H 00002 #define FileDescPool_H 00003 00004 #include "PtMutex.h" 00005 #include "PtCondition.h" 00006 #include <list> 00007 #include <string> 00008 #include <set> 00009 #include <sys/types.h> 00010 #include <sys/stat.h> 00011 #include <fcntl.h> 00012 00013 BOREALIS_NAMESPACE_BEGIN; 00014 00015 class FileDescPool 00016 { 00017 public: 00018 // Hint: Better make sure that the file open flags are ammenable to multiple 00019 // invocations of open(2) on the specified file. 00020 FileDescPool(string filepath, int openFlags, int minFDs, int maxFDs) 00021 throw (std::exception, 00022 SmIllegalParameterValueException, 00023 SmFilesystemException); 00024 00025 // Hint: Better make sure that the file open flags are ammenable to multiple 00026 // invocations of open(2) on the specified file. 00027 FileDescPool(string filepath, int openFlags, mode_t permissions, int minFDs, int maxFDs) 00028 throw (std::exception, 00029 SmIllegalParameterValueException, 00030 SmFilesystemException); 00031 00032 virtual ~FileDescPool(); 00033 00034 unsigned int getMinFDs() 00035 throw (std::exception); 00036 00037 unsigned int getMaxFDs() 00038 throw (std::exception); 00039 00040 unsigned int getTotalFDsInPool() 00041 throw (std::exception); 00042 00043 unsigned int getNumLeasedFDs() 00044 throw (std::exception); 00045 00046 // Fails if the number of currently leased FDs is greater than newMaxFDs. 00047 // Also fails if the parameters have silly values. '0' is not a silly value. 00048 void setFDLimits(unsigned int newMinFDs, unsigned int newMaxFDs) 00049 throw (std::exception); 00050 00051 // Blocks until an FD is available in the pool, or until the closeAll() 00052 // method has been called on the pool. If this returns without an exception, 00053 // then the return value is a valid FD from this pool. 00054 // 00055 // Caller may do anything with the FD he pleases except to close it. 00056 int acquireFdBlocking() 00057 throw (std::exception, 00058 SmClosedException); 00059 00060 // Returns a valid FD from this pool, or -1 if none is available. 00061 // 00062 // Caller may do anything with the FD he pleases except to close it. 00063 int acquireFdNonBlocking() 00064 throw (std::exception); 00065 00066 // Releases the specified FD back into the pool. 00067 void releaseFd(int fd) 00068 throw (std::exception); 00069 00070 // Closes all of the file handles and sets the minimum number of open 00071 // FDs to 0. Throws an exception is one or more FDs is still leased. 00072 // 00073 // In all normal circumstances, this should be called before destroying 00074 // this object. 00075 void closeAll() 00076 throw (std::exception); 00077 00078 private: 00079 // Closes or opens FDs until the total number managed by this object is 00080 // numDesiredFDs. However, this will not close any FDs that are presently 00081 // leased (not a failure condition). 00082 void adjustFdCount(int numDesiredFDs) 00083 throw (std::exception); 00084 00085 // Removes and closes one FD from _freeFDs. 00086 void deallocateOneFD() 00087 throw (std::exception); 00088 00089 // Opens an FD and adds it to _freeFDs. 00090 void allocateOneFD() 00091 throw (std::exception); 00092 00093 PtMutex _mtx; 00094 PtCondition _condAvailableFD; // Signal whenever another FD is available. 00095 list<int> _freeFDs; // The FDs that are allocated but aren't in use 00096 set<int> _leasedFDs; // The FDs that are in use 00097 00098 string _filepath; 00099 int _openFlags; 00100 unsigned int _minFDs; 00101 unsigned int _maxFDs; 00102 00103 mode_t _permissions; // Only specified with one version of the c'tor 00104 bool _permissionsSet; // true iff the version of the c'tor that specifies file permissions was called. 00105 }; 00106 00107 BOREALIS_NAMESPACE_END; 00108 00109 #endif

Generated on Fri Nov 12 15:15:21 2004 for Borealis by doxygen 1.3.8