Coder Social home page Coder Social logo

Comments (3)

broadwaylamb avatar broadwaylamb commented on May 19, 2024 1

Since the argument passed in this method is inout, the language prevents you from calling it simultaneously from multiple threads by crashing. See https://swift.org/blog/swift-5-exclusivity/.

from opencombine.

maximkrouk avatar maximkrouk commented on May 19, 2024

I think it shouldn't, most of the time you don't need it so synchronization should be implemented only for specific cases as needed...

from opencombine.

maximkrouk avatar maximkrouk commented on May 19, 2024

@bofeizhu you may use something like that

import Foundation

@propertyWrapper
public struct ThreadSafeWithLock<Value> {
  private var value: Value
  private let lock: NSLocking
  public var locksOnRead: Bool = false

  public init(
    wrappedValue: Value,
    _ lock: NSLocking = NSLock(),
    locksOnRead: Bool = false
  ) {
    self.value = wrappedValue
    self.lock = lock
    self.locksOnRead = locksOnRead
  }

  public var wrappedValue: Value {
    get {
      if locksOnRead {
        return lock.execute { value }
      } else {
        return value
      }
    }
    set { lock.store(newValue, in: &value) }
  }
}

@ThreadSafeWithLock
var subscriptions: Set<AnyCancellable> = []

publisher
  .sink { ... }
  .store(in &subscriptions)
import Foundation

extension NSLocking {
  /// Atomically stores new value in object
  @inlinable
  public func store<T>(_ value: T, in object: inout T) {
    mutate(&object, with: { $0 = value })
  }

  /// Atomically mutates object with closure
  @inlinable
  public func mutate<T: AnyObject>(_ object: T, with closure: (T) -> Void) {
    execute { closure(object) }
  }

  /// Atomically mutates object with closure
  @inlinable
  public func mutate<T>(_ object: inout T, with closure: (inout T) -> Void) {
    execute { closure(&object) }
  }

  /// Atomically mutates object with closure
  @inlinable
  public func set<T: AnyObject, Value>(
    _ object: T,
    _ keyPath: ReferenceWritableKeyPath<T, Value>,
    _ value: Value
  ) {
    execute { object[keyPath: keyPath] = value }
  }

  /// Atomically mutates object with closure
  @inlinable
  public func set<T, Value>(_ object: inout T, _ keyPath: WritableKeyPath<T, Value>, _ value: Value)
  {
    execute { object[keyPath: keyPath] = value }
  }

  /// Atomically executes a block of code
  @discardableResult
  @inlinable
  public func execute<T>(code closure: () -> T) -> T {
    lock()
    defer { unlock() }
    return closure()
  }
}

from opencombine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.