Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remaining issues in async/sync mode switching. #270

Merged
merged 10 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 5 additions & 10 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -437,16 +437,11 @@ extension OperatingMode {
task()
}
case .synchronous:
// if for some reason, we're told to do all this stuff on
// main, ignore it, and use the default queue. this prevents
// a possible deadlock.
if queue === DispatchQueue.main {
OperatingMode.defaultQueue.asyncAndWait {
task()
}
} else {
queue.asyncAndWait { task() }
}
// in synchronous mode, always use our own queue to
// prevent deadlocks.
let workItem = DispatchWorkItem(block: task)
OperatingMode.defaultQueue.asyncAndWait(execute: workItem)
}
}
}

11 changes: 11 additions & 0 deletions Sources/Segment/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@

import Foundation

#if os(Linux)
extension DispatchQueue {
func asyncAndWait(execute workItem: DispatchWorkItem) {
async {
workItem.perform()
}
workItem.wait()
}
}
#endif

/// Inquire as to whether we are within a Unit Testing environment.
#if DEBUG
internal var isUnitTesting: Bool = {
Expand Down
Loading