Giter Site home page Giter Site logo

concurrentkvo's Introduction

ConcurrentKVO

Classes that implement:

  • A Reader/Writer Lock built using a GCD DispatchQueue.
  • A KVO-compliant property.
  • A concurrent, KVO-compliant property, which uses neither of the above.

Most implementations of KVO-compliant properties that use R/W locks for thread safety have a race condition.

  • You can’t call willChangeValue and didChangeValue inside the barrier/write operation.
    • NSObject calls your property getter from within these calls, which causes a deadlock.
    • GCD detects this and throws an exception to terminate your app.
  • But if you call willChangeValue and didChangeValue outside the barrier/write operation, you permit a race condition.
    • Other writers may interleave their KVO calls with yours, even though the write itself is protected.
    • This creates an “audit trail” of KVO notifications that doesn’t accurately reflect the true sequence of property value changes.

The ConcurrentKVO class prevents this race by sending the KVO notification from inside the barrier. It avoids deadlock by using thread-local storage to record the property value before sending the notification. The property getter checks for this, so it doesn’t need to re-enter the lock when it gets called on the same thread as a running barrier operation.

Although recursive locks are considered an anti-pattern, the NSObject implementation of KVO requires that the public getter be available for re-entrancy from within the setter, so this is unavoidable.

concurrentkvo's People

Contributors

r72-engineering avatar

Watchers

James Luke avatar

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.