From 0a3b124a51c29318859acf825a96b5ca87e4ba33 Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Fri, 25 Jan 2019 13:22:32 -0500 Subject: [PATCH] Add safe queue sync. --- .../MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h | 3 +++ .../MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h index 3cdffb3..982ed5d 100644 --- a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h +++ b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h @@ -18,4 +18,7 @@ + (void)performBlockInBackground:(nonnull void (^)(void))block; + (void)performSyncBlockInBackground:(nonnull void (^)(void))block; +/// Ensures the block is peformed on the same *labeled* dispatch queue. The queue *must* be previously initialized with a unique label. ++ (void)performSyncBlock:(nonnull void (^)(void))block onQueue:(dispatch_queue_t)queue; + @end diff --git a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m index 8ff96fb..9791ace 100644 --- a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m +++ b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m @@ -42,4 +42,14 @@ } } ++ (void)performSyncBlock:(nonnull void (^)(void))block onQueue:(dispatch_queue_t)queue { + assert(dispatch_queue_get_label(queue) != NULL); // The queue being queried MUST have a label. + const char *currQueueLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL); + if (!currQueueLabel || strcmp(dispatch_queue_get_label(queue), currQueueLabel) != 0) { + dispatch_sync(queue, block); + } else { + block(); + } +} + @end