Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/InContextCore/Utilities/ThreadExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ import Foundation
class ThreadExecutor {

private let workQueue = DispatchQueue(label: "RenderExecutor.workQueue")
private let operationQueue = OperationQueue()

init() {
operationQueue.maxConcurrentOperationCount = 8
}

// Guarantee that a throwing synchronous block is executed in a classical thread context and not an async one,
// wrapping it as a throwing async call in the process. This works by using `withCheckedThrowingContinuation`
// to cross the boundary, and then dispatching async onto a single internal serial queue, serializing all dispatched
// blocks.
// to cross the boundary, and then enqueueing the work on a concurrent operation queue.
func perform<T>(_ block: @escaping () throws -> T) async throws -> T {
return try await withCheckedThrowingContinuation { continuation in
workQueue.async {
operationQueue.addOperation {
do {
let result = try block()
continuation.resume(returning: result)
Expand Down