Skip to content

Commit

Permalink
HostQueue: Don't use dispatch_sync to run code synchronously
Browse files Browse the repository at this point in the history
This method may choose to run on the current thread as an optimization.
Since we may try to call DOLHostQueueRunSync on the main thread, we don't want this.
  • Loading branch information
OatmealDome committed Jun 24, 2023
1 parent df51158 commit 8fcc264
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Source/iOS/Library/HostQueue.mm
Expand Up @@ -4,6 +4,10 @@
#include "HostQueue.h"

#include <dispatch/dispatch.h>
#include <Foundation/Foundation.h>

#include "Common/Assert.h"
#include "Common/Event.h"

#include "Core/Core.h"

Expand All @@ -21,6 +25,8 @@ dispatch_queue_t DOLHostQueueGetUnderlyingQueue()

void DOLHostQueueExecuteBlock(void (^block)(void))
{
ASSERT(![NSThread isMainThread]);

Core::DeclareAsHostThread();

block();
Expand All @@ -30,9 +36,16 @@ void DOLHostQueueExecuteBlock(void (^block)(void))

void DOLHostQueueRunSync(void (^block)(void))
{
dispatch_sync(DOLHostQueueGetUnderlyingQueue(), ^{
Common::Event sync_event;
Common::Event* sync_event_ptr = &sync_event;

dispatch_async(DOLHostQueueGetUnderlyingQueue(), ^{
DOLHostQueueExecuteBlock(block);

sync_event_ptr->Set();
});

sync_event.Wait();
}

void DOLHostQueueRunAsync(void (^block)(void))
Expand Down

0 comments on commit 8fcc264

Please sign in to comment.