Skip to content

Commit

Permalink
Fix DataShare and EventPublisher
Browse files Browse the repository at this point in the history
  • Loading branch information
doyaGu committed Jul 5, 2024
1 parent 963c9cd commit c8ae1bf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/DataShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

using namespace BML;

std::mutex DataShare::s_MapMutex;
std::unordered_map<std::string, DataShare *> DataShare::s_DataShares;

DataShare *DataShare::GetInstance(const std::string &name) {
auto it = s_DataShares.find(name);
if (it == s_DataShares.end()) {
s_DataShares[name] = Create(name);
return Create(name);
}
return it->second;
}
Expand All @@ -17,6 +18,7 @@ DataShare *DataShare::Create(std::string name) {
}

DataShare::~DataShare() {
std::lock_guard<std::mutex> lock{s_MapMutex};
s_DataShares.erase(m_Name);
}

Expand Down Expand Up @@ -117,8 +119,10 @@ void *DataShare::SetUserData(void *data, size_t type) {
}

DataShare::DataShare(std::string name) : m_Name(std::move(name)) {
s_DataShares[m_Name] = this;
AddRef();

std::lock_guard<std::mutex> lock{s_MapMutex};
s_DataShares[m_Name] = this;
}

bool DataShare::AddCallbacks(const char *key, DataShareCallback callback, void *userdata) const {
Expand Down
1 change: 1 addition & 0 deletions src/DataShare.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace BML {
mutable std::unordered_map<std::string, std::vector<Callback>> m_CallbackMap;
DataBox m_UserData;

static std::mutex s_MapMutex;
static std::unordered_map<std::string, DataShare *> s_DataShares;
};
}
Expand Down
8 changes: 6 additions & 2 deletions src/EventPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

using namespace BML;

std::mutex EventPublisher::s_MapMutex;
std::unordered_map<std::string, EventPublisher *> EventPublisher::s_EventPublishers;

EventPublisher *EventPublisher::GetInstance(const std::string &name) {
auto it = s_EventPublishers.find(name);
if (it == s_EventPublishers.end()) {
s_EventPublishers[name] = Create(name);
return Create(name);
}
return it->second;
}
Expand All @@ -19,6 +20,7 @@ EventPublisher *EventPublisher::Create(std::string name) {
}

EventPublisher::~EventPublisher() {
std::lock_guard<std::mutex> lock{s_MapMutex};
s_EventPublishers.erase(m_Name);
}

Expand Down Expand Up @@ -421,6 +423,8 @@ void EventPublisher::SortListeners(EventType eventType) {
}

EventPublisher::EventPublisher(std::string name) : m_Name(std::move(name)) {
s_EventPublishers[m_Name] = this;
AddRef();

std::lock_guard<std::mutex> lock{s_MapMutex};
s_EventPublishers[m_Name] = this;
}
1 change: 1 addition & 0 deletions src/EventPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace BML {
std::unordered_map<EventType, std::vector<EventListenerInfo>> m_EventListeners;
DataBox m_UserData;

static std::mutex s_MapMutex;
static std::unordered_map<std::string, EventPublisher *> s_EventPublishers;
};
}
Expand Down

0 comments on commit c8ae1bf

Please sign in to comment.