Skip to content

Commit

Permalink
fix: Fix thread-safety issues when accessing dartMessageHandlerManager_
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Sep 24, 2024
1 parent faa774a commit 9723857
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/iris_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint>

#include <memory>
#include <mutex>
#include <map>
#include <vector>

Expand Down Expand Up @@ -196,30 +197,30 @@ class DartMessageHandlerManager : public DartMessageHandlerBase
std::map<Dart_Port, std::unique_ptr<DartMessageHandler>> dartMessageHandlerMap_;
};

DartMessageHandlerManager *dartMessageHandlerManager_ = nullptr;
std::mutex message_handler_mutex_;
std::unique_ptr<DartMessageHandlerManager> dartMessageHandlerManager_ = nullptr;

// Initialize `dart_api_dl.h`
EXPORT intptr_t InitDartApiDL(void *data)
{
std::lock_guard<std::mutex> lock(message_handler_mutex_);
if (!dartMessageHandlerManager_)
{
dartMessageHandlerManager_ = new DartMessageHandlerManager();
dartMessageHandlerManager_ = std::make_unique<DartMessageHandlerManager>();
}

return dartMessageHandlerManager_->InitDartApiDL(data);
}

EXPORT void Dispose()
{
if (dartMessageHandlerManager_)
{
delete dartMessageHandlerManager_;
dartMessageHandlerManager_ = nullptr;
}
std::lock_guard<std::mutex> lock(message_handler_mutex_);
dartMessageHandlerManager_.reset();
}

EXPORT void OnEvent(EventParam *param)
{
std::lock_guard<std::mutex> lock(message_handler_mutex_);
if (dartMessageHandlerManager_)
{
dartMessageHandlerManager_->Post(param);
Expand All @@ -228,6 +229,7 @@ EXPORT void OnEvent(EventParam *param)

EXPORT void RegisterDartPort(Dart_Port send_port)
{
std::lock_guard<std::mutex> lock(message_handler_mutex_);
if (dartMessageHandlerManager_)
{
dartMessageHandlerManager_->RegisterDartPort(send_port);
Expand All @@ -236,6 +238,7 @@ EXPORT void RegisterDartPort(Dart_Port send_port)

EXPORT void UnregisterDartPort(Dart_Port send_port)
{
std::lock_guard<std::mutex> lock(message_handler_mutex_);
if (dartMessageHandlerManager_)
{
dartMessageHandlerManager_->UnregisterDartPort(send_port);
Expand Down

0 comments on commit 9723857

Please sign in to comment.