Belle II Software development
NSMDataStore Class Reference

Classes

struct  Entry
 
struct  Header
 

Public Member Functions

bool isOpend ()
 
bool open (unsigned short max=0)
 
void init ()
 
Entryadd (unsigned int addr, unsigned int size, unsigned int revision, const std::string &name, const std::string &format, unsigned int rid)
 
void unlink ()
 
HeadergetHeader ()
 
Entryget (const std::string &name)
 
Entryget (unsigned int id)
 
Entryget (unsigned int addr, unsigned int rid)
 
void lock ()
 
void unlock ()
 
bool wait (int second)
 
void signal ()
 

Static Public Member Functions

static NSMDataStoregetStore ()
 

Private Attributes

char * m_buf
 
Headerm_header
 
Entrym_entries
 
SharedMemory m_mem
 
MMutex m_mutex
 
MCond m_cond
 

Static Private Attributes

static NSMDataStore g_store
 

Detailed Description

Definition at line 21 of file NSMDataStore.h.

Constructor & Destructor Documentation

◆ NSMDataStore()

NSMDataStore ( )
inlineprivate

Definition at line 51 of file NSMDataStore.h.

52 {
53 m_buf = NULL;
54 m_header = NULL;
55 m_entries = NULL;
56 }

Member Function Documentation

◆ add()

NSMDataStore::Entry * add ( unsigned int  addr,
unsigned int  size,
unsigned int  revision,
const std::string &  name,
const std::string &  format,
unsigned int  rid 
)

Definition at line 58 of file NSMDataStore.cc.

64{
65 MLockGuard lockGuard(m_mutex);
66 unsigned int n = m_header->nentries;
67 for (unsigned int i = 0; i < n; i++) {
68 if (m_entries[i].id > 0 && name == m_entries[i].name) {
69 return &m_entries[i];
70 }
71 }
72 memset(&m_entries[n], 0, sizeof(Entry));
73 m_entries[n].id = n + 1;
74 m_entries[n].rid = rid;
75 m_entries[n].addr = addr;
76 m_entries[n].size = size;
77 m_entries[n].revision = revision;
78 m_entries[n].utime = (unsigned int)Time().getSecond();
79 strcpy(m_entries[n].name, name.c_str());
80 strcpy(m_entries[n].format, format.c_str());
81 m_header->nentries++;
82 return &m_entries[n];
83}
Lock Guard for a Mutex instance.
Definition: LockGuard.h:47

◆ get() [1/3]

NSMDataStore::Entry * get ( const std::string &  name)

Definition at line 85 of file NSMDataStore.cc.

86{
87 MLockGuard lockGuard(m_mutex);
88 const unsigned int n = m_header->nentries;
89 for (unsigned int i = 0; i < n; i++) {
90 if (m_entries[i].id > 0 && name == m_entries[i].name) {
91 return &m_entries[i];
92 }
93 }
94 return NULL;
95}

◆ get() [2/3]

NSMDataStore::Entry * get ( unsigned int  addr,
unsigned int  rid 
)

Definition at line 111 of file NSMDataStore.cc.

113{
114 if (id == 0) return NULL;
115 MLockGuard lockGuard(m_mutex);
116 const unsigned int n = m_header->nentries;
117 for (unsigned int i = 0; i < n; i++) {
118 if (m_entries[i].rid > 0 &&
119 m_entries[i].addr == (unsigned int)addr &&
120 m_entries[i].rid == (unsigned int)id) {
121 return &m_entries[i];
122 }
123 }
124 return NULL;
125}

◆ get() [3/3]

NSMDataStore::Entry * get ( unsigned int  id)

Definition at line 97 of file NSMDataStore.cc.

98{
99 if (id == 0) return NULL;
100 MLockGuard lockGuard(m_mutex);
101 const unsigned int n = m_header->nentries;
102 for (unsigned int i = 0; i < n; i++) {
103 if (m_entries[i].id > 0 &&
104 m_entries[i].id == (unsigned int)id) {
105 return &m_entries[i];
106 }
107 }
108 return NULL;
109}

◆ getHeader()

Header * getHeader ( )
inline

Definition at line 69 of file NSMDataStore.h.

69{ return m_header; }

◆ getStore()

static NSMDataStore & getStore ( )
inlinestatic

Definition at line 24 of file NSMDataStore.h.

25 {
26 return g_store;
27 }

◆ init()

void init ( )

Definition at line 48 of file NSMDataStore.cc.

49{
50 if (m_header == NULL) return;
51 int max = m_header->maxentries;
52 memset(m_buf, 0, m_mem.size());
53 m_header->maxentries = max;
54 m_mutex.init();
55 m_cond.init();
56}

◆ isOpend()

bool isOpend ( )
inline

Definition at line 59 of file NSMDataStore.h.

59{ return m_buf != NULL; }

◆ lock()

void lock ( )
inline

Definition at line 73 of file NSMDataStore.h.

73{ m_mutex.lock(); }

◆ open()

bool open ( unsigned short  max = 0)

Definition at line 25 of file NSMDataStore.cc.

26{
27 size_t size = (max > 0) ? m_mutex.size() + m_cond.size() +
28 sizeof(Header) + sizeof(Entry) * max : 0;
29 if (!m_mem.open("NSMDataStore", size)) {
30 return false;
31 }
32 m_mem.truncate(0);
33 char* buf = (char*)m_mem.map();
34 m_buf = buf;
35 m_mutex = MMutex(buf);
36 buf += m_mutex.size();
37 m_cond = MCond(buf);
38 buf += m_cond.size();
39 m_header = reinterpret_cast<Header*>(buf);
40 buf += sizeof(Header);
41 m_entries = reinterpret_cast<Entry*>(buf);
42 if (max > 0) {
43 m_header->maxentries = max;
44 }
45 return true;
46}

◆ signal()

void signal ( )
inline

Definition at line 79 of file NSMDataStore.h.

79{ m_cond.broadcast(); }

◆ unlink()

void unlink ( )

Definition at line 127 of file NSMDataStore.cc.

128{
129 const unsigned int n = m_header->nentries;
130 for (unsigned int i = 0; i < n; i++) {
131 char* name = m_entries[i].name;
132 if (strlen(name) > 0) {
133 std::string path = "";
134 sockaddr_in sa;
135 sa.sin_addr.s_addr = m_entries[i].addr;
136 if (m_entries[i].addr > 0) {
137 path = StringUtil::form("%s:%s",
138 inet_ntoa(sa.sin_addr), name);
139 } else {
140 path = StringUtil::form("127.0.0.1:%s", name);
141 }
142 shm_unlink(path.c_str());
143 }
144 }
145 m_mem.unlink();
146}

◆ unlock()

void unlock ( )
inline

Definition at line 74 of file NSMDataStore.h.

74{ m_mutex.unlock(); }

◆ wait()

bool wait ( int  second)
inline

Definition at line 75 of file NSMDataStore.h.

76 {
77 return m_cond.wait(m_mutex, second, 0);
78 }

Member Data Documentation

◆ g_store

NSMDataStore g_store
staticprivate

Definition at line 30 of file NSMDataStore.h.

◆ m_buf

char* m_buf
private

Definition at line 82 of file NSMDataStore.h.

◆ m_cond

MCond m_cond
private

Definition at line 87 of file NSMDataStore.h.

◆ m_entries

Entry* m_entries
private

Definition at line 84 of file NSMDataStore.h.

◆ m_header

Header* m_header
private

Definition at line 83 of file NSMDataStore.h.

◆ m_mem

SharedMemory m_mem
private

Definition at line 85 of file NSMDataStore.h.

◆ m_mutex

MMutex m_mutex
private

Definition at line 86 of file NSMDataStore.h.


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