|
9 | 9 | #include <vector>
|
10 | 10 | #include <array>
|
11 | 11 | #include <cassert>
|
| 12 | +#include <mutex> |
12 | 13 |
|
13 | 14 | #include "libipc/ipc.h"
|
14 | 15 | #include "libipc/def.h"
|
@@ -69,9 +70,21 @@ ipc::buff_t make_cache(T& data, std::size_t size) {
|
69 | 70 | return { ptr, size, ipc::mem::free };
|
70 | 71 | }
|
71 | 72 |
|
72 |
| -acc_t *cc_acc() { |
73 |
| - static ipc::shm::handle acc_h("__CA_CONN__", sizeof(acc_t)); |
74 |
| - return static_cast<acc_t *>(acc_h.get()); |
| 73 | +acc_t *cc_acc(ipc::string const &pref) { |
| 74 | + static ipc::unordered_map<ipc::string, ipc::shm::handle> handles; |
| 75 | + static std::mutex lock; |
| 76 | + std::lock_guard<std::mutex> guard {lock}; |
| 77 | + auto it = handles.find(pref); |
| 78 | + if (it == handles.end()) { |
| 79 | + ipc::string shm_name {ipc::make_prefix(pref, {"CA_CONN__"})}; |
| 80 | + ipc::shm::handle h; |
| 81 | + if (!h.acquire(shm_name.c_str(), sizeof(acc_t))) { |
| 82 | + ipc::error("[cc_acc] acquire failed: %s\n", shm_name.c_str()); |
| 83 | + return nullptr; |
| 84 | + } |
| 85 | + it = handles.emplace(pref, std::move(h)).first; |
| 86 | + } |
| 87 | + return static_cast<acc_t *>(it->second.get()); |
75 | 88 | }
|
76 | 89 |
|
77 | 90 | struct cache_t {
|
@@ -101,11 +114,15 @@ struct conn_info_head {
|
101 | 114 | conn_info_head(char const * prefix, char const * name)
|
102 | 115 | : prefix_ {ipc::make_string(prefix)}
|
103 | 116 | , name_ {ipc::make_string(name)}
|
104 |
| - , cc_id_ {(cc_acc() == nullptr) ? 0 : cc_acc()->fetch_add(1, std::memory_order_relaxed)} |
| 117 | + , cc_id_ {} |
105 | 118 | , cc_waiter_{ipc::make_prefix(prefix_, {"CC_CONN__", name_}).c_str()}
|
106 | 119 | , wt_waiter_{ipc::make_prefix(prefix_, {"WT_CONN__", name_}).c_str()}
|
107 | 120 | , rd_waiter_{ipc::make_prefix(prefix_, {"RD_CONN__", name_}).c_str()}
|
108 | 121 | , acc_h_ {ipc::make_prefix(prefix_, {"AC_CONN__", name_}).c_str(), sizeof(acc_t)} {
|
| 122 | + acc_t *pacc = cc_acc(prefix_); |
| 123 | + if (pacc != nullptr) { |
| 124 | + cc_id_ = pacc->fetch_add(1, std::memory_order_relaxed); |
| 125 | + } |
109 | 126 | }
|
110 | 127 |
|
111 | 128 | void quit_waiting() {
|
|
0 commit comments