Skip to content

Commit

Permalink
addon: lock before tsfn call
Browse files Browse the repository at this point in the history
  • Loading branch information
eilvelia committed May 15, 2024
1 parent c6d24f9 commit 04a2b94
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/tdl/addon/td.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class ReceiveWorker {
using Tsfn = Napi::TypedThreadSafeFunction<TsfnCtx, char, CallJs>;

void loop() {
std::unique_lock<std::mutex> lock(mutex);
while (true) {
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this] { return ready; });
if (stop) break;
ready = false;
Expand All @@ -125,6 +125,7 @@ class ReceiveWorker {
// TDLib stores the response in thread-local storage that is deallocated
// on execute() and receive(). Since we never call execute() in this
// thread, it should be safe not to copy the response here.
lock.lock();
tsfn.NonBlockingCall(const_cast<char *>(response));
}
tsfn.Release();
Expand Down

0 comments on commit 04a2b94

Please sign in to comment.