Skip to content

Commit

Permalink
fix rw lock... but why without wrapper not work?....
Browse files Browse the repository at this point in the history
  • Loading branch information
ivlevAstef committed Dec 25, 2024
1 parent 1ec9af8 commit 9b267a9
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions Sources/SwiftLazy/BaseLazy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,31 @@ public class BaseThreadSaveLazy<Value>: @unchecked Sendable {

/// `true` if `self` was previously made.
public var wasMade: Bool {
pthread_rwlock_rdlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
lock.readLock()
defer { lock.unlock() }
return cache != nil
}

init() {
pthread_rwlock_init(&lock, &attr)
}

deinit {
pthread_rwlock_destroy(&lock)
}

/// clears the stored value.
public func clear() {
pthread_rwlock_wrlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
lock.writeLock()
defer { lock.unlock() }
cache = nil
}

internal func getValue(_ initializer: () -> Value) -> Value {
pthread_rwlock_rdlock(&lock)
lock.readLock()
if let cache {
pthread_rwlock_unlock(&lock)
lock.unlock()
return cache
}
pthread_rwlock_unlock(&lock)
lock.unlock()

pthread_rwlock_wrlock(&lock)
defer { pthread_rwlock_unlock(&lock) }
lock.writeLock()
defer { lock.unlock() }

if let cache {
return cache
return cache
}

let result = initializer()
Expand All @@ -53,8 +45,8 @@ public class BaseThreadSaveLazy<Value>: @unchecked Sendable {
return result
}

private var lock = pthread_rwlock_t()
private var attr = pthread_rwlockattr_t()
private let lock = RWLock()

private var cache: Value?
}

Expand All @@ -74,3 +66,27 @@ extension BaseThreadSaveLazy: CustomStringConvertible, CustomDebugStringConverti
return "Lazy(\(value): \(Value.self))"
}
}

private final class RWLock {
private var lock = pthread_rwlock_t()

init() {
pthread_rwlock_init(&lock, nil)
}

deinit {
pthread_rwlock_destroy(&lock)
}

func writeLock() {
pthread_rwlock_wrlock(&lock)
}

func readLock() {
pthread_rwlock_rdlock(&lock)
}

func unlock() {
pthread_rwlock_unlock(&lock)
}
}

0 comments on commit 9b267a9

Please sign in to comment.