Belle II Software development
RFSharedMem Class Reference
Inheritance diagram for RFSharedMem:
SharedMem RFNodeManager

Public Member Functions

 RFSharedMem (const char *name)
 
RfShm_CellGetCell (int id)
 
void * ptr (void)
 
int shmid (void)
 
bool IsCreated (void)
 
void lock (void)
 
void unlock (void)
 
bool isLocked (void)
 

Static Public Member Functions

static std::string getTmpFileName (std::string user, std::string name)
 
static bool getIdFromTmpFileName (std::string filename, int &shmid, int &semid)
 

Private Attributes

bool m_new {false}
 True if we created the ring buffer ourselves (and need to clean it).
 
key_t m_shmkey
 SHM key, see shmget(2).
 
key_t m_semkey
 Semaphore key.
 
int m_shmid {-1}
 shared memory id
 
int m_semid {-1}
 semaphore id
 
void * m_shmadr {nullptr}
 

Detailed Description

Definition at line 51 of file RFSharedMem.h.

Constructor & Destructor Documentation

◆ RFSharedMem()

RFSharedMem ( const char *  name)

Definition at line 16 of file RFSharedMem.cc.

16 : SharedMem(name, sizeof(RfShm_Data))
17{
18 RfShm_Data* rfshm = (RfShm_Data*) ptr();
19 if (SharedMem::IsCreated()) {
20 bzero((char*)rfshm, sizeof(RfShm_Data));
21 for (int i = 0; i < RFSHM_MAX_PROCESS; i++) {
22 (rfshm->cell[i]).pid = 0;
23 (rfshm->cell[i]).type = 0;
24 (rfshm->cell[i]).command = -1;
25 }
26 }
27}

◆ ~RFSharedMem()

Definition at line 29 of file RFSharedMem.cc.

30{
31}

Member Function Documentation

◆ GetCell()

RfShm_Cell & GetCell ( int  id)

Definition at line 33 of file RFSharedMem.cc.

34{
35 RfShm_Data* rfshm = (RfShm_Data*) ptr();
36 return rfshm->cell[id];
37}

◆ getIdFromTmpFileName()

bool getIdFromTmpFileName ( std::string  filename,
int &  shmid,
int &  semid 
)
staticinherited

Definition at line 177 of file SharedMem.cc.

178{
179 char shminfo[256];
180 int fd = open(filename.c_str(), O_RDONLY);
181 if (fd < 0) {
182 printf("SharedMem: error to reopen tmp file %s\n", filename.c_str());
183 return false;
184 }
185 shmid = -1;
186 semid = -1;
187 memset(shminfo, 0, sizeof(shminfo));
188 int n = read(fd, shminfo, sizeof(shminfo));
189 close(fd);
190 sscanf(shminfo, "%d %d", &shmid, &semid);
191 return (n >= 3 && shmid >= 0 && semid >= 0);
192}

◆ getTmpFileName()

std::string getTmpFileName ( std::string  user,
std::string  name 
)
staticinherited

Definition at line 172 of file SharedMem.cc.

173{
174 return string("/tmp/") + user + string("_SHM_") + name;
175}

◆ IsCreated()

bool IsCreated ( void  )
inherited

Definition at line 167 of file SharedMem.cc.

168{
169 return m_new;
170}
bool m_new
True if we created the ring buffer ourselves (and need to clean it).
Definition: SharedMem.h:38

◆ isLocked()

bool isLocked ( void  )
inherited

Definition at line 228 of file SharedMem.cc.

229{
230 int ignored = 0;
231 return (semctl(m_semid, 0, GETVAL, ignored) == 0); //0: locked, 1: unlocked
232}
int m_semid
semaphore id
Definition: SharedMem.h:43

◆ lock()

void lock ( void  )
inherited

Definition at line 194 of file SharedMem.cc.

195{
196 struct sembuf sb;
197 sb.sem_num = 0;
198 sb.sem_op = -1;
199 sb.sem_flg = 0;
200 while (semop(m_semid, &sb, 1) == -1) {
201 if (errno == EINTR) {
202 //interrupted by signal (e.g. window size changed), try again
203 continue;
204 } else {
205 perror("lock:semop");
206 exit(-1);
207 }
208 }
209}

◆ ptr()

void * ptr ( void  )
inherited

Definition at line 157 of file SharedMem.cc.

158{
159 return (void*) m_shmadr;
160}

◆ shmid()

int shmid ( void  )
inherited

Definition at line 162 of file SharedMem.cc.

163{
164 return m_shmid;
165}
int m_shmid
shared memory id
Definition: SharedMem.h:42

◆ unlock()

void unlock ( void  )
inherited

Definition at line 211 of file SharedMem.cc.

212{
213 struct sembuf sb;
214 sb.sem_num = 0;
215 sb.sem_op = 1;
216 sb.sem_flg = 0;
217 while (semop(m_semid, &sb, 1) == -1) {
218 if (errno == EINTR) {
219 //interrupted by signal (e.g. window size changed), try again
220 continue;
221 } else {
222 perror("unlock:semop");
223 exit(-1);
224 }
225 }
226}

Member Data Documentation

◆ m_new

bool m_new {false}
privateinherited

True if we created the ring buffer ourselves (and need to clean it).

Definition at line 38 of file SharedMem.h.

◆ m_semid

int m_semid {-1}
privateinherited

semaphore id

Definition at line 43 of file SharedMem.h.

◆ m_semkey

key_t m_semkey
privateinherited

Semaphore key.

Definition at line 40 of file SharedMem.h.

◆ m_shmadr

void* m_shmadr {nullptr}
privateinherited

Definition at line 44 of file SharedMem.h.

◆ m_shmid

int m_shmid {-1}
privateinherited

shared memory id

Definition at line 42 of file SharedMem.h.

◆ m_shmkey

key_t m_shmkey
privateinherited

SHM key, see shmget(2).

Definition at line 39 of file SharedMem.h.


The documentation for this class was generated from the following files: