22#ifndef _HDF5SERIE_FILE_H_
23#define _HDF5SERIE_FILE_H_
25#include <hdf5serie/group.h>
26#include <boost/filesystem.hpp>
27#include <boost/interprocess/sync/interprocess_mutex.hpp>
28#include <boost/interprocess/sync/scoped_lock.hpp>
30 #include <boost/interprocess/windows_shared_memory.hpp>
32 #include <boost/interprocess/shared_memory_object.hpp>
34#include <boost/interprocess/mapped_region.hpp>
35#include <boost/container/static_vector.hpp>
36#include <boost/uuid/uuid.hpp>
37#include <boost/date_time/posix_time/posix_time.hpp>
39#include <boost/thread/thread.hpp>
47 using SharedMemory = boost::interprocess::windows_shared_memory;
48 inline void SharedMemoryRemove(
const char* shmName) {
50 inline SharedMemory SharedMemoryCreate(
const char *shmName, boost::interprocess::mode_t mode,
52 auto shm=SharedMemory(boost::interprocess::create_only, shmName, mode, size);
56 using SharedMemory = boost::interprocess::shared_memory_object;
57 inline void SharedMemoryRemove(
const char* shmName) {
58 SharedMemory::remove(shmName);
60 inline SharedMemory SharedMemoryCreate(
const char *shmName, boost::interprocess::mode_t mode,
62 auto shm=SharedMemory(boost::interprocess::create_only, shmName, mode);
85 void wait(boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> &externLock,
86 const std::function<
bool()> &pred);
87 bool wait_for(boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> &externLock,
88 const std::chrono::milliseconds& relTime,
89 const std::function<
bool()> &pred);
92 boost::container::static_vector<boost::uuids::uuid, N> waiter;
93 boost::interprocess::interprocess_mutex waiterMutex;
133 const std::function<
void()> &closeRequestCallback_={},
134 const std::function<void()> &refreshCallback_={},
135 const std::function<void()> &renameAtomicFunc_={});
142 static int getDefaultCompression() {
return defaultCompression; }
143 static void setDefaultCompression(
int comp) { defaultCompression=comp; }
144 static int getDefaultChunkSize() {
return defaultChunkSize; }
145 static void setDefaultChunkSize(
int chunk) { defaultChunkSize=chunk; }
146 static int getDefaultCacheSize() {
return defaultCacheSize; }
147 static void setDefaultCacheSize(
int cache) { defaultCacheSize=cache; }
168 static int defaultCompression;
169 static int defaultChunkSize;
170 static int defaultCacheSize;
172 void close()
override;
176 boost::filesystem::path getFilename(
bool originalFilename=
false);
206 size_t shmUseCount { 0 };
208 boost::interprocess::interprocess_mutex mutex;
213 size_t activeReaders { 0 };
215 boost::container::static_vector<ProcessInfo, MAXREADERS+1> processes;
217 bool flushRequest {
false };
224 const std::function<void()> renameAtomicFunc;
229 const boost::uuids::uuid processUUID;
233 Internal::SharedMemory
shm;
236 boost::interprocess::mapped_region
region;
255 void postCloseReader();
262 void postCloseWriter();
270 std::string_view blockingMsg,
const std::function<
bool()> &pred);
273 boost::thread stillAlivePingThread;
274 void stillAlivePing();
292 std::string &
shmName, Internal::SharedMemory &
shm, boost::interprocess::mapped_region &
region,
Definition: interface.h:292
static constexpr size_t MAXREADERS
the maximal number of readers which can access the file simultanously
Definition: file.h:192
FileAccess
Definition: file.h:109
@ writeWithRename
Definition: file.h:113
@ read
Open file for reading with SWMR reading mode enabled.
Definition: file.h:110
@ write
Definition: file.h:111
const std::function< void()> closeRequestCallback
This callback is called when a writer requested a close of all readers.
Definition: file.h:221
void deinitProcessInfo()
Remove process information of this process from the shared memory.
Definition: file.cc:476
bool exitThread
Flag which is set to true to enforce the thread to exit (on the next condition notify signal)
Definition: file.h:283
void closeReader()
Helper function to close the file as a reader.
Definition: file.cc:656
static constexpr size_t MAXWRITERS
the maximal number of writers. Just need since only a fixed amount of readers+writers can wait for th...
Definition: file.h:194
void openReader()
Helper function to open the file as a reader. preOpenReader must be called before.
Definition: file.cc:551
void listenForRequest()
The worker function for the thread listenForRequestThread.
Definition: file.cc:803
void flushIfRequested()
Definition: file.cc:688
static std::string createShmName(const boost::filesystem::path &filename)
transform filename to a valid boost interprocess name.
Definition: file.cc:909
void preOpenWriter()
Helper function to prepare for openWriter.
Definition: file.cc:415
WriterState lastWriterState
The last wrtierState known by this object.
Definition: file.h:244
void enableSWMR() override
Definition: file.cc:713
void closeWriter()
Helper function to close the file as a writer.
Definition: file.cc:639
std::string shmName
Name of the shared memory.
Definition: file.h:227
static void dumpSharedMemory(const boost::filesystem::path &filename)
Definition: file.cc:853
bool flushRequested
True if this reader has requested a flush.
Definition: file.h:242
static void openOrCreateShm(const boost::filesystem::path &filename, File *self, std::string &shmName, Internal::SharedMemory &shm, boost::interprocess::mapped_region ®ion, SharedMemObject *&sharedData)
open or create the shared memory atomically (guarded by a global named mutex)
Definition: file.cc:364
WriterState
A writer can be in the following state:
Definition: file.h:185
bool preSWMR
Definition: file.h:182
void wait(Internal::ScopedLock &lock, const std::chrono::milliseconds &relTime, std::string_view blockingMsg, const std::function< bool()> &pred)
Definition: file.cc:782
void refresh() override
Refresh the dataset of a reader.
Definition: file.cc:672
FileAccess type
Flag if this instance is a writer or reader.
Definition: file.h:179
void initProcessInfo()
Write process information of this process to the shared memory.
Definition: file.cc:463
Internal::SharedMemory shm
Definition: file.h:233
static void removeSharedMemory(const boost::filesystem::path &filename)
Internal helper function which removes the shared memory associated with filename,...
Definition: file.cc:914
void requestFlush()
Definition: file.cc:679
~File() override
Closes the HDF5 file.
Definition: file.cc:588
void openWriter()
Helper function to open the file as a writer. preOpenWriter must be called before.
Definition: file.cc:440
File(const boost::filesystem::path &filename_, FileAccess type_, const std::function< void()> &closeRequestCallback_={}, const std::function< void()> &refreshCallback_={}, const std::function< void()> &renameAtomicFunc_={})
Definition: file.cc:321
std::thread listenForRequestThread
Definition: file.h:279
boost::interprocess::mapped_region region
Definition: file.h:236
SharedMemObject * sharedData
Pointer to the shared memory object.
Definition: file.h:239
boost::filesystem::path filename
The name of the file.
Definition: file.h:175
void preOpenReader()
Helper function to prepare for openReader.
Definition: file.cc:532
const std::function< void()> refreshCallback
This callback is called when a writer has flushed.
Definition: file.h:223
Information about a process accessing the shared memory (a process means here an instance of a File c...
Definition: file.h:196
boost::posix_time::ptime lastAliveTime
the last still alive timestamp of the process
Definition: file.h:198
boost::uuids::uuid processUUID
a globally unique identifier for each process
Definition: file.h:197
FileAccess type
the type of the process read or write
Definition: file.h:199