Skip to content

Commit

Permalink
feat: MacOS support for ExpiringActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Feb 1, 2024
1 parent 5ded5cd commit 2f1d979
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 7 additions & 7 deletions Sources/InfomaniakCore/Background/ClosureExpiringActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ protocol ClosureExpiringActivable {

/// Something to wrap arbitrary code that should be performed on the background.
public struct ClosureExpiringActivity: ClosureExpiringActivable {
private let processInfo = ProcessInfo.processInfo

private let qos: DispatchQoS

/// Init method of `BackgroundExecutor`
Expand All @@ -46,21 +48,19 @@ public struct ClosureExpiringActivity: ClosureExpiringActivable {
public func executeWithBackgroundTask(_ block: @escaping (@escaping TaskCompletion) -> Void,
onExpired: @escaping () -> Void) {
let taskName = "executeWithBackgroundTask \(UUID().uuidString)"
let processInfos = ProcessInfo()
let group = TolerantDispatchGroup(qos: qos)
group.enter()

#if os(macOS)
DDLogDebug("Starting task \(taskName) (No expiration handler as we are running on macOS)")
processInfos.performActivity(options: .suddenTerminationDisabled, reason: taskName) {
processInfo.performActivity(options: .suddenTerminationDisabled, reason: taskName) {
block {
DDLogDebug("Ending task \(taskName)")
group.leave()
}
group.wait()
}
#else
DDLogDebug("Starting task \(taskName)")
processInfos.performExpiringActivity(withReason: taskName) { expired in
let group = TolerantDispatchGroup(qos: qos)
group.enter()
processInfo.performExpiringActivity(withReason: taskName) { expired in
if expired {
onExpired()
DDLogDebug("Expired task \(taskName)")
Expand Down
15 changes: 14 additions & 1 deletion Sources/InfomaniakCore/Background/ExpiringActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public final class ExpiringActivity: ExpiringActivityable {

private let queue: DispatchQueue

private let processInfo = ProcessInfo.processInfo

var locks = [TolerantDispatchGroup]()

let id: String
Expand Down Expand Up @@ -83,8 +85,18 @@ public final class ExpiringActivity: ExpiringActivityable {
self.locks.append(group)
}

#if os(macOS)
// We block a non cooperative queue that matches current QoS
DispatchQueue.global(qos: qos.qosClass).async {
self.processInfo.performActivity(options: .suddenTerminationDisabled, reason: self.id) {
// No expiration handler as we are running on macOS
group.enter()
group.wait()
}
}
#else
// Make sure to not lock an unexpected thread that would deinit()
ProcessInfo.processInfo.performExpiringActivity(withReason: id) { [weak self] shouldTerminate in
processInfo.performExpiringActivity(withReason: id) { [weak self] shouldTerminate in
guard let self else {
return
}
Expand All @@ -96,6 +108,7 @@ public final class ExpiringActivity: ExpiringActivityable {
group.enter()
group.wait()
}
#endif
}

public func endAll() {
Expand Down

0 comments on commit 2f1d979

Please sign in to comment.