From ec19b928a8b4cee7e12798e2ada629611a3b4a5f Mon Sep 17 00:00:00 2001 From: Huy Nguyen Date: Tue, 6 Oct 2020 11:42:37 -0700 Subject: [PATCH] Ship ASExperimentalDispatchApply (#1924) Closes #1850. --- Source/Private/ASDispatch.h | 2 +- Source/Private/ASDispatch.mm | 40 +++++++++++------------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/Source/Private/ASDispatch.h b/Source/Private/ASDispatch.h index caccf74de..6ef5a8c96 100644 --- a/Source/Private/ASDispatch.h +++ b/Source/Private/ASDispatch.h @@ -11,7 +11,7 @@ #import /** - * Like dispatch_apply, but you can set the thread count. 0 means 2*active CPUs. + * Like dispatch_apply, but you can set the thread count. 0 means letting dispatch_apply determine it. * * Note: The actual number of threads may be lower than threadCount, if libdispatch * decides the system can't handle it. In reality this rarely happens. diff --git a/Source/Private/ASDispatch.mm b/Source/Private/ASDispatch.mm index 769a9185d..b82032da7 100644 --- a/Source/Private/ASDispatch.mm +++ b/Source/Private/ASDispatch.mm @@ -9,43 +9,27 @@ #import #import - // Prefer C atomics in this file because ObjC blocks can't capture C++ atomics well. #import -/** - * Like dispatch_apply, but you can set the thread count. 0 means 2*active CPUs. - * - * Note: The actual number of threads may be lower than threadCount, if libdispatch - * decides the system can't handle it. In reality this rarely happens. - */ void ASDispatchApply(size_t iterationCount, dispatch_queue_t queue, NSUInteger threadCount, NS_NOESCAPE void(^work)(size_t i)) { if (threadCount == 0) { - if (ASActivateExperimentalFeature(ASExperimentalDispatchApply)) { - dispatch_apply(iterationCount, queue, work); - return; + dispatch_apply(iterationCount, queue, work); + } else { + dispatch_group_t group = dispatch_group_create(); + __block atomic_size_t counter = ATOMIC_VAR_INIT(0); + for (NSUInteger t = 0; t < threadCount; t++) { + dispatch_group_async(group, queue, ^{ + size_t i; + while ((i = atomic_fetch_add(&counter, 1)) < iterationCount) { + work(i); + } + }); } - threadCount = NSProcessInfo.processInfo.activeProcessorCount * 2; - } - dispatch_group_t group = dispatch_group_create(); - __block atomic_size_t counter = ATOMIC_VAR_INIT(0); - for (NSUInteger t = 0; t < threadCount; t++) { - dispatch_group_async(group, queue, ^{ - size_t i; - while ((i = atomic_fetch_add(&counter, 1)) < iterationCount) { - work(i); - } - }); + dispatch_group_wait(group, DISPATCH_TIME_FOREVER); } - dispatch_group_wait(group, DISPATCH_TIME_FOREVER); }; -/** - * Like dispatch_async, but you can set the thread count. 0 means 2*active CPUs. - * - * Note: The actual number of threads may be lower than threadCount, if libdispatch - * decides the system can't handle it. In reality this rarely happens. - */ void ASDispatchAsync(size_t iterationCount, dispatch_queue_t queue, NSUInteger threadCount, NS_NOESCAPE void(^work)(size_t i)) { if (threadCount == 0) { threadCount = NSProcessInfo.processInfo.activeProcessorCount * 2;