12 #ifndef SST_CORE_INTERPROCESS_TUNNEL_MMAP_PARENT_H
13 #define SST_CORE_INTERPROCESS_TUNNEL_MMAP_PARENT_H 1
29 namespace Interprocess {
37 template<
typename TunnelType>
50 MMAPParent(uint32_t comp_id,
size_t numBuffers,
size_t bufferSize, uint32_t expectedChildren = 1) : shmPtr(nullptr), fd(-1)
53 memset(key,
'\0',
sizeof(key));
55 snprintf(key,
sizeof(key),
"/tmp/sst_shmem_%u-%" PRIu32
"-%d", getpid(), comp_id, rand());
58 fd = open(filename.c_str(), O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
66 }
while ( (fd < 0) && (errno == EEXIST) );
70 fprintf(stderr,
"Failed to create IPC region '%s': %s\n", filename.c_str(), strerror(errno));
74 tunnel =
new TunnelType(numBuffers, bufferSize, expectedChildren);
75 shmSize = tunnel->getTunnelSize();
77 if ( ftruncate(fd, shmSize) ) {
79 fprintf(stderr,
"Resizing shared file '%s' failed: %s\n", filename.c_str(), strerror(errno));
83 shmPtr = mmap(
nullptr, shmSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
84 if ( shmPtr == MAP_FAILED ) {
86 fprintf(stderr,
"mmap failed: %s\n", strerror(errno));
91 memset(shmPtr,
'\0', shmSize);
92 tunnel->initialize(shmPtr);
100 munmap(shmPtr, shmSize);
101 if (
remove(filename.c_str()) != 0) {
102 fprintf(stderr,
"Error deleting tunnel file: %s\n", filename.c_str());
116 std::string filename;
virtual ~MMAPParent()
Destructor.
Definition: mmapparent.h:96
MMAPParent(uint32_t comp_id, size_t numBuffers, size_t bufferSize, uint32_t expectedChildren=1)
Parent/master manager for an IPC Tunnel Creates a memory-mapped file and initializes a TunnelType dat...
Definition: mmapparent.h:50
TunnelType * getTunnel()
return the created tunnel pointer
Definition: mmapparent.h:110
const std::string & getRegionName(void) const
returns name of the mmap'd file
Definition: mmapparent.h:107
Class supports an IPC tunnel between two or more processes, via an mmap'd file.
Definition: mmapparent.h:38