HDF5Serie

Project Page
/home/user/MBSimWindows/hdf5serie/hdf5serie/hdf5serie/file.h
00001 /* Copyright (C) 2009 Markus Friedrich
00002  *
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact:
00018  *   friedrich.at.gc@googlemail.com
00019  *
00020  */
00021 
00022 #ifndef _HDF5SERIE_FILE_H_
00023 #define _HDF5SERIE_FILE_H_
00024 
00025 #include <hdf5serie/group.h>
00026 #include <boost/filesystem.hpp>
00027 #include <boost/date_time/posix_time/posix_time_types.hpp>
00028 #include <boost/shared_ptr.hpp>
00029 
00030 namespace boost {
00031   namespace interprocess {
00032     class shared_memory_object;
00033     class mapped_region;
00034     class interprocess_mutex;
00035     class interprocess_condition;
00036   }
00037 }
00038 
00039 namespace H5 {
00040 
00041   class Dataset;
00042 
00043   class File : public GroupBase {
00044     friend class Dataset;
00045     friend class GroupBase;
00046     public:
00047       enum FileAccess {
00048         read,
00049         write
00050       };
00051       File(const boost::filesystem::path &filename, FileAccess type_);
00052       ~File();
00053       void reopenAsSWMR();
00054       static void reopenAllFilesAsSWMR();
00055       static int getDefaultCompression() { return defaultCompression; }
00056       static void setDefaultCompression(int comp) { defaultCompression=comp; }
00057       static int getDefaultChunkSize() { return defaultChunkSize; }
00058       static void setDefaultChunkSize(int chunk) { defaultChunkSize=chunk; }
00059       void refresh();
00060       void flush();
00061 
00062       void requestWriterFlush();
00063       bool waitForWriterFlush();
00064 
00065       void flushIfRequested();
00066       static void flushAllFiles();
00067       static void flushAllFilesIfRequested();
00068       void refreshAfterWriterFlush();
00069       static void refreshAllFiles();
00070       static void refreshFilesAfterWriterFlush(const std::set<H5::File*> &files);
00071       static void refreshAllFilesAfterWriterFlush();
00072 
00073       struct IPC {
00074         boost::filesystem::path filename; // the filename of this IPC
00075         std::string interprocessName; // the name of this IPC
00076         boost::shared_ptr<boost::interprocess::shared_memory_object> shm; // shared memory used for this IPC
00077         boost::shared_ptr<boost::interprocess::mapped_region> shmmap; // mapping of shared memory to real memory
00078         bool *flushVar; // true if flush by the writer is requested (lies in shared memory)
00079         boost::interprocess::interprocess_mutex *mutex; // mutex for access of flushVar and cond (lies in shared memory)
00080         boost::interprocess::interprocess_condition *cond; // condition to notify readers (lies in shared memory)
00081         boost::posix_time::ptime flushRequestTime; // the time of the flush request (only used on reader side)
00082       };
00083     protected:
00084       FileAccess type;
00085       bool isSWMR;
00086       void close();
00087       void open();
00088       static int defaultCompression;
00089       static int defaultChunkSize;
00090 
00091       static std::set<File*> writerFiles;
00092       static std::set<File*> readerFiles;
00093       std::string interprocessName;
00094       IPC ipc;
00095       void addFileToNotifyOnRefresh(const boost::filesystem::path &filename);
00096       std::vector<IPC> ipcAdd;
00097   };
00098 }
00099 
00100 #endif