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

FdMultiPool.h

Go to the documentation of this file.
00001 #ifndef FdMultiPool_H 00002 #define FdMultiPool_H 00003 00004 #include "common.h" 00005 #include "Exceptions.h" 00006 #include "PtMutex.h" 00007 #include <map> 00008 #include <list> 00009 #include <set> 00010 #include <string> 00011 #include <fcntl.h> 00012 00013 BOREALIS_NAMESPACE_BEGIN; 00014 00015 /* 00016 This manages a pool of file descriptors, which covers a registered set of files. 00017 This pool will try to minimize the number of open(...) calls that needs to be 00018 made to satisfy all of the requests. 00019 */ 00020 class FdMultiPool 00021 { 00022 public: 00023 FdMultiPool(int maxFdsInPool) 00024 throw (std::exception); 00025 00026 virtual ~FdMultiPool(); 00027 00028 //----------------------------------------------------------------------------- 00029 00030 // Makes the pool capable of offering file handles for the specified file. 00031 void registerFile(string pathname, int openFlags) 00032 throw (std::exception); 00033 00034 // Makes the pool capable of offering file handles for the specified file. 00035 void registerFile(string pathname, int openFlags, mode_t permissions) 00036 throw (std::exception); 00037 00038 // If no FD is currently leased, this closes all FDs and disables this object 00039 // from future operations. Otherwise, this throws an exception. 00040 void close() 00041 throw (std::exception); 00042 00043 //----------------------------------------------------------------------------- 00044 00045 // Leases an FD from the pool. Throws an exception of if there was a problem 00046 // opening the file, or if the pool's maxFD's would be exceeded, or if something 00047 // else goes wrong. 00048 // 00049 // Returns ./the FD to be leased. 00050 int acquireFdNonBlocking(string pathname) 00051 throw (std::exception); 00052 00053 void releaseFd(string pathname, int releasedFd) 00054 throw (std::exception); 00055 00056 //----------------------------------------------------------------------------- 00057 00058 void printDebugInfo(ostream & o) 00059 throw (std::exception); 00060 00061 private: 00062 PtMutex _mtx; 00063 00064 const int _maxFdsInPool; 00065 bool _closed; 00066 00067 // Gives all the data we track on a per-file basis in this FdMultiPool object. 00068 struct FileInfo 00069 { 00070 int _openFlags; 00071 00072 mode_t _permissions; 00073 bool _permissionsSpecified; 00074 00075 set<int> _leasedFds; // Set of FDs that are currently in use for this file 00076 list<int> _nonLeasedFds; // Set of FDs that are open for this file but aren't currently in use 00077 }; 00078 00079 map<string, FileInfo> _registeredFiles; // key = pathname 00080 00081 // Given the pathname of a file in _registeredFiles, this opens another FD for 00082 // that file and returns it. This method doesn't modify the FileInfo object 00083 // associated with the file. 00084 int openFile(string pathname) 00085 throw (std::exception); 00086 00087 // Evicts one FD, that's not currently being used, from the pool. If all FDs 00088 // in the pool are currently leased out, this throws an exception. 00089 void evictOneFd() 00090 throw (std::exception); 00091 00092 // Returns the number of FDs that are presently allocated in this pool. This is 00093 // the number that are leased + the number that are open but not leased. 00094 int getNumOpenFds() 00095 throw (std::exception); 00096 }; 00097 00098 BOREALIS_NAMESPACE_END; 00099 00100 #endif

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