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>
45 using SharedMemory = boost::interprocess::windows_shared_memory;
46 inline void SharedMemoryRemove(
const char* shmName) {
48 inline SharedMemory SharedMemoryCreate(
const char *shmName, boost::interprocess::mode_t mode,
50 auto shm=SharedMemory(boost::interprocess::create_only, shmName, mode, size);
54 using SharedMemory = boost::interprocess::shared_memory_object;
55 inline void SharedMemoryRemove(
const char* shmName) {
56 SharedMemory::remove(shmName);
58 inline SharedMemory SharedMemoryCreate(
const char *shmName, boost::interprocess::mode_t mode,
60 auto shm=SharedMemory(boost::interprocess::create_only, shmName, mode);
80 void wait(boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> &externLock,
81 const std::function<
bool()> &pred);
84 boost::container::static_vector<boost::uuids::uuid, N> waiter;
85 boost::interprocess::interprocess_mutex waiterMutex;
115 const std::function<
void()> &closeRequestCallback_=std::function<
void()>(),
116 const std::function<
void()> &refreshCallback_=std::function<
void()>());
123 static int getDefaultCompression() {
return defaultCompression; }
124 static void setDefaultCompression(
int comp) { defaultCompression=comp; }
125 static int getDefaultChunkSize() {
return defaultChunkSize; }
126 static void setDefaultChunkSize(
int chunk) { defaultChunkSize=chunk; }
127 static int getDefaultCacheSize() {
return defaultCacheSize; }
128 static void setDefaultCacheSize(
int cache) { defaultCacheSize=cache; }
147 static int defaultCompression;
148 static int defaultChunkSize;
149 static int defaultCacheSize;
151 void close()
override;
165 const boost::uuids::uuid processUUID;
189 size_t shmUseCount { 0 };
191 boost::interprocess::interprocess_mutex mutex;
196 size_t activeReaders { 0 };
198 boost::container::static_vector<ProcessInfo, MAXREADERS+1> processes;
200 bool flushRequest {
false };
205 Internal::SharedMemory
shm;
208 boost::interprocess::mapped_region
region;
235 std::string_view blockingMsg,
const std::function<
bool()> &pred);
238 boost::thread stillAlivePingThread;
239 void stillAlivePing();
Definition: interface.h:284
static constexpr size_t MAXREADERS
the maximal number of readers which can access the file simultanously
Definition: file.h:175
FileAccess
Definition: file.h:101
@ read
open file for reading
Definition: file.h:102
@ write
open file for writing
Definition: file.h:103
const FileAccess type
Flag if this instance is a writer or reader.
Definition: file.h:156
const std::function< void()> closeRequestCallback
This callback is called when a writer requested a close of all readers.
Definition: file.h:158
void deinitProcessInfo()
Remove process information of this process from the shared memory.
Definition: file.cc:300
bool exitThread
Flag which is set to true to enforce the thread to exit (on the next condition notify signal)
Definition: file.h:248
void closeReader()
Helper function to close the file as a reader.
Definition: file.cc:450
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:177
void openReader()
Helper function to open the file as a reader.
Definition: file.cc:353
void listenForRequest()
The worker function for the thread listenForRequestThread.
Definition: file.cc:548
File(const boost::filesystem::path &filename_, FileAccess type_, const std::function< void()> &closeRequestCallback_=std::function< void()>(), const std::function< void()> &refreshCallback_=std::function< void()>())
Definition: file.cc:160
void flushIfRequested()
Definition: file.cc:490
static std::string createShmName(const boost::filesystem::path &filename)
transform filename to a valid boost interprocess name.
Definition: file.cc:649
void openOrCreateShm()
open or create the shared memory atomically (process with using file lock)
Definition: file.cc:185
WriterState lastWriterState
The last wrtierState known by this object.
Definition: file.h:216
void enableSWMR() override
Definition: file.cc:515
void closeWriter()
Helper function to close the file as a writer.
Definition: file.cc:434
std::string shmName
Name of the shared memory.
Definition: file.h:163
static void dumpSharedMemory(const boost::filesystem::path &filename)
Definition: file.cc:598
bool flushRequested
True if this reader has requested a flush.
Definition: file.h:214
WriterState
A writer can be in the following state:
Definition: file.h:168
void refresh() override
Refresh the dataset of a reader.
Definition: file.cc:472
const boost::filesystem::path filename
The name of the file.
Definition: file.h:154
void initProcessInfo()
Write process information of this process to the shared memory.
Definition: file.cc:288
Internal::SharedMemory shm
Definition: file.h:205
static void removeSharedMemory(const boost::filesystem::path &filename)
Internal helper function which removes the shared memory associated with filename,...
Definition: file.cc:663
void requestFlush()
Definition: file.cc:479
~File() override
Closes the HDF5 file.
Definition: file.cc:392
void openWriter()
Helper function to open the file as a writer.
Definition: file.cc:240
std::thread listenForRequestThread
Definition: file.h:244
void wait(Internal::ScopedLock &lock, std::string_view blockingMsg, const std::function< bool()> &pred)
Definition: file.cc:535
boost::interprocess::mapped_region region
Definition: file.h:208
SharedMemObject * sharedData
Pointer to the shared memory object.
Definition: file.h:211
const std::function< void()> refreshCallback
This callback is called when a writer has flushed.
Definition: file.h:160
Information about a process accessing the shared memory (a process means here an instance of a File c...
Definition: file.h:179
boost::posix_time::ptime lastAliveTime
the last still alive timestamp of the process
Definition: file.h:181
boost::uuids::uuid processUUID
a globally unique identifier for each process
Definition: file.h:180
FileAccess type
the type of the process read or write
Definition: file.h:182