Skip to content

Commit b532c15

Browse files
chkuang-ga-maurice
authored andcommitted
Postpone log callback to Unity to the main thread
Also added AddCallbackWithThreadCheck() to execute the callback immediately if it is called from the callback thread. Mono would attempt to kill the thread which called any C# function before. As a result, if any C++ thread call any log function, Mono would attempt to send SIGINT signal to that thread. Once the signal is sent, any call from C++->C# would cause hang. This postpone the C++->C# call to the main thread to avoid this issue. PiperOrigin-RevId: 267512154
1 parent eb42a24 commit b532c15

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

app/src/callback.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ void* AddCallback(Callback* callback) {
242242
return g_callback_dispatcher->AddCallback(callback);
243243
}
244244

245+
// TODO(chkuang): remove this once we properly implement C++->C# log callback.
246+
void* AddCallbackWithThreadCheck(Callback* callback) {
247+
if (g_callback_thread_id_initialized &&
248+
Thread::IsCurrentThread(g_callback_thread_id)) {
249+
// If we are on the callback thread, we can safely execute the callback
250+
// right away and blocking would be a deadlock, so we run it.
251+
callback->Run();
252+
delete callback;
253+
return nullptr;
254+
} else {
255+
return AddCallback(callback);
256+
}
257+
}
258+
245259
// A blocking callback posts a semaphore after being done.
246260
// This allows the caller to wait for its completion.
247261
class BlockingCallback : public Callback {

app/src/callback.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,16 @@ Callback* NewCallback(CallbackAndArgs... callback_and_args) {
368368
/// callback is executed.
369369
void* AddCallback(Callback* callback);
370370

371+
/// Adds a Callback to be called on the next PollCallbacks call.
372+
/// This function returns a reference in the queue that can be used to remove
373+
/// the callback from the queue. The reference is only valid until the
374+
/// callback is executed.
375+
/// If AddCallbackWithThreadCheck is called from the callback thread,
376+
/// the new callback is executed immediately to avoid deadlocks and return a
377+
/// nullptr.
378+
/// This is a temporary solution for C++->C# log callback.
379+
void* AddCallbackWithThreadCheck(Callback* callback);
380+
371381
/// Adds a Callback to be called on the next PollCallbacks call.
372382
/// This function blocks until the callback has been executed or removed from
373383
/// the queue. If AddBlockingCallback is called from the callback thread,

0 commit comments

Comments
 (0)