Skip to content

Commit

Permalink
fix potential crash in multi-threading
Browse files Browse the repository at this point in the history
  • Loading branch information
jingpeng committed Nov 10, 2023
1 parent 3aa9c24 commit 01e0605
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/dangle/DangleContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace dangle {
using Op = std::function<void(void)>;

bool destroy = false;
std::mutex destroyMutex;

// Ops are combined into batches:
// 1. A batch is always executed entirely in one go on the GL thread
Expand Down Expand Up @@ -97,6 +98,8 @@ namespace dangle {
// [JS thread] Add a blocking operation to the 'next' batch -- waits for the
// queued function to run before returning
void addBlockingToNextBatch(Op &&op) noexcept {
std::unique_lock<std::mutex> lock(destroyMutex);

if (destroy) {
DangleSysLog("addBlockingToNextBatch after DangleContext destroyed");
return;
Expand Down Expand Up @@ -260,6 +263,11 @@ namespace dangle {
});
}

~DangleContext() {
std::unique_lock<std::mutex> lock(destroyMutex);
DangleSysLog("~DangleContext()");
}

static DangleContext *ContextGet(UDangleContextId dangleCtxId);

static UDangleContextId ContextCreate(jsi::Runtime &runtime);
Expand Down

0 comments on commit 01e0605

Please sign in to comment.