Skip to content

Commit

Permalink
[Fastlane.Swift] Swift fastlane does not run on Apple Silicon fastlan…
Browse files Browse the repository at this point in the history
…e#18502

* [Swift] Replace DispatchQueue with os_unfair_lock as suggested in review.
  • Loading branch information
kikeenrique committed Dec 2, 2021
1 parent 57f33e8 commit 4d1fbf8
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions fastlane/swift/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,42 @@ func desc(_: String) {
// no-op, this is handled in fastlane/lane_list.rb
}

class AtomicDictionary<Key: Hashable, Value> {
private let lockQueue = DispatchQueue(label: "tools.fastlane.serial.queue")
class UnfairDictionary<Key: Hashable, Value> {
private var unfairLock: UnsafeMutablePointer<os_unfair_lock>
private var storage: [Key: Value]

init() {
unfairLock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
unfairLock.initialize(to: os_unfair_lock())
storage = [:]
}

deinit {
unfairLock.deallocate()
}

func get(_ key: Key) -> Value? {
lockQueue.sync {
storage[key]
}
os_unfair_lock_lock(unfairLock)
defer { os_unfair_lock_unlock(unfairLock) }
return storage[key]
}

func set(_ key: Key, value: Value) {
lockQueue.sync {
storage[key] = value
}
os_unfair_lock_lock(unfairLock)
defer { os_unfair_lock_unlock(unfairLock) }
storage[key] = value
}

func removeValue(forKey key: Key) {
lockQueue.sync {
_ = storage.removeValue(forKey: key)
}
os_unfair_lock_lock(unfairLock)
defer { os_unfair_lock_unlock(unfairLock) }
_ = storage.removeValue(forKey: key)
}

var allValues: [Key: Value] {
lockQueue.sync {
storage
}
os_unfair_lock_lock(unfairLock)
defer { os_unfair_lock_unlock(unfairLock) }
return storage
}
}

Expand All @@ -62,7 +68,7 @@ class Runner {
private var returnValue: String? // lol, so safe
private var currentlyExecutingCommand: RubyCommandable?
private var shouldLeaveDispatchGroupDuringDisconnect = false
private var executeNext: AtomicDictionary<String, Bool> = AtomicDictionary()
private var executeNext: UnfairDictionary<String, Bool> = UnfairDictionary()

func executeCommand(_ command: RubyCommandable) -> String {
dispatchGroup.enter()
Expand Down

0 comments on commit 4d1fbf8

Please sign in to comment.