@@ -182,6 +182,7 @@ struct chunk_info_t {
182182auto & chunk_storages () {
183183 class chunk_handle_t {
184184 ipc::unordered_map<ipc::string, ipc::shm::handle> handles_;
185+ std::mutex lock_;
185186
186187 static bool make_handle (ipc::shm::handle &h, ipc::string const &shm_name, std::size_t chunk_size) {
187188 if (!h.valid () &&
@@ -197,19 +198,25 @@ auto& chunk_storages() {
197198 chunk_info_t *get_info (conn_info_head *inf, std::size_t chunk_size) {
198199 ipc::string pref {(inf == nullptr ) ? ipc::string{} : inf->prefix_ };
199200 ipc::string shm_name {ipc::make_prefix (pref, {" CHUNK_INFO__" , ipc::to_string (chunk_size)})};
200- ipc::shm::handle &h = handles_[pref];
201- if (!make_handle (h, shm_name, chunk_size)) {
202- return nullptr ;
201+ ipc::shm::handle *h;
202+ {
203+ std::lock_guard<std::mutex> guard {lock_};
204+ h = &(handles_[pref]);
205+ if (!make_handle (*h, shm_name, chunk_size)) {
206+ return nullptr ;
207+ }
203208 }
204- auto *info = static_cast <chunk_info_t *>(h. get ());
209+ auto *info = static_cast <chunk_info_t *>(h-> get ());
205210 if (info == nullptr ) {
206211 ipc::error (" [chunk_storages] chunk_shm.id_info_.get failed: chunk_size = %zd\n " , chunk_size);
207212 return nullptr ;
208213 }
209214 return info;
210215 }
211216 };
212- static ipc::map<std::size_t , chunk_handle_t > chunk_hs;
217+ using deleter_t = void (*)(chunk_handle_t *);
218+ using chunk_handle_ptr_t = std::unique_ptr<chunk_handle_t , deleter_t >;
219+ static ipc::map<std::size_t , chunk_handle_ptr_t > chunk_hs;
213220 return chunk_hs;
214221}
215222
@@ -220,13 +227,17 @@ chunk_info_t *chunk_storage_info(conn_info_head *inf, std::size_t chunk_size) {
220227 static ipc::rw_lock lock;
221228 IPC_UNUSED_ std::shared_lock<ipc::rw_lock> guard {lock};
222229 if ((it = storages.find (chunk_size)) == storages.end ()) {
223- using chunk_handle_t = std::decay_t <decltype (storages)>::value_type::second_type;
230+ using chunk_handle_ptr_t = std::decay_t <decltype (storages)>::value_type::second_type;
231+ using chunk_handle_t = chunk_handle_ptr_t ::element_type;
224232 guard.unlock ();
225233 IPC_UNUSED_ std::lock_guard<ipc::rw_lock> guard {lock};
226- it = storages.emplace (chunk_size, chunk_handle_t {}).first ;
234+ it = storages.emplace (chunk_size, chunk_handle_ptr_t {
235+ ipc::mem::alloc<chunk_handle_t >(), [](chunk_handle_t *p) {
236+ ipc::mem::destruct (p);
237+ }}).first ;
227238 }
228239 }
229- return it->second . get_info (inf, chunk_size);
240+ return it->second -> get_info (inf, chunk_size);
230241}
231242
232243std::pair<ipc::storage_id_t , void *> acquire_storage (conn_info_head *inf, std::size_t size, ipc::circ::cc_t conns) {
@@ -356,8 +367,7 @@ struct queue_generator {
356367 " QU_CONN__" ,
357368 ipc::to_string (DataSize), " __" ,
358369 ipc::to_string (AlignSize), " __" ,
359- name}).c_str ()} {
360- }
370+ name}).c_str ()} {}
361371
362372 void disconnect_receiver () {
363373 bool dis = que_.disconnect ();
0 commit comments