shm would fail with multi-thread accessing

This commit is contained in:
mutouyun 2019-01-16 14:01:12 +08:00
parent 7e44b2dd4d
commit d1822e9fc9
2 changed files with 16 additions and 9 deletions

View File

@ -14,8 +14,7 @@ INCLUDEPATH += \
../test \ ../test \
../test/capo \ ../test/capo \
../include \ ../include \
../src \ ../src
../src/platform
HEADERS += \ HEADERS += \
../test/test.h ../test/test.h

View File

@ -4,17 +4,21 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <mutex>
#include "def.h" #include "def.h"
#include "tls_pointer.h"
#include "platform/to_tchar.h"
#include "memory/resource.hpp" #include "memory/resource.hpp"
#include "platform/to_tchar.h"
namespace { namespace {
inline auto& m2h() { inline auto* m2h() {
static ipc::tls::pointer<ipc::mem::unordered_map<void*, HANDLE>> cache; static struct {
return *cache.create(); std::mutex lc_;
ipc::mem::unordered_map<void*, HANDLE> cache_;
} m2h_;
return &m2h_;
} }
} // internal-linkage } // internal-linkage
@ -38,7 +42,10 @@ void* acquire(char const * name, std::size_t size) {
::CloseHandle(h); ::CloseHandle(h);
return nullptr; return nullptr;
} }
m2h().emplace(mem, h); {
[[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ };
m2h()->cache_.emplace(mem, h);
}
return mem; return mem;
} }
@ -46,7 +53,8 @@ void release(void* mem, std::size_t /*size*/) {
if (mem == nullptr) { if (mem == nullptr) {
return; return;
} }
auto& cc = m2h(); [[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ };
auto& cc = m2h()->cache_;
auto it = cc.find(mem); auto it = cc.find(mem);
if (it == cc.end()) { if (it == cc.end()) {
return; return;