borchero / Squid

Declarative and Reactive Networking for Swift.

Home Page:https://squid.borchero.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thread Sanitizer warnings

jessegrosjean opened this issue · comments

I get Swift access race in Squid.Locked.lock() -> () at 0x7b080002f4e0 when running Squid under Xcode thread sanitizer. My understanding is there's not currently an actual issue in the threading code due to the way that Swift is generating code, but the thread analyzer can't be sure, so generates the warning.

I think to fix warning you can replace:

internal class Locked<Value> {
    private var _lock = os_unfair_lock()
    ...

with:

internal class Locked<Value> {
    private var _lock: UnsafeMutablePointer<os_unfair_lock>

    init(_ value: Value) {
        _lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
        _lock.initialize(to: os_unfair_lock())
        self._value = value
    }

I found this solution and an explanation here:

http://www.russbishop.net/the-law