Giter VIP home page Giter VIP logo

sync's Introduction

Sync

Useful synchronization primitives in Swift.

Mutex<T>

A fair mutual exclusion primitive useful for protecting shared data

This mutex will block threads waiting for the lock to become available. The mutex can also be statically initialized or created via a new constructor. Each mutex has a type parameter which represents the data that it is protecting. The data can only be accessed through the access handle passed to the callback of lock and tryLock, which guarantees that the data is only ever accessed when the mutex is locked.

Note: The implementation is based on pthread_mutex_t (64 bytes).

Minimal Example

let mutex = try Mutex(0)

try! mutex.read { value in 
    print(value)
}

try! mutex.write { access in
    access { value in
        value += 42
    }
}
Real-world Example

Real-world Example

let mutex = try Mutex(0)

let count: Int = 1000

let queue = DispatchQueue(
    label: #function,
    attributes: .concurrent
)

let group = DispatchGroup()

for _ in 0..<count {
    group.enter()

    queue.async {
        defer {
            group.leave()
        }
        try! mutex.write { value in
            value += 2
        }
    }
}

group.wait()

let value = try! mutex.unwrap()

XCTAssertEqual(value, 2 * count)

UnfairMutex<T>

An unfair mutual exclusion primitive useful for protecting shared data

This mutex will block threads waiting for the lock to become available. The mutex can also be statically initialized or created via a new constructor. Each mutex has a type parameter which represents the data that it is protecting. The data can only be accessed through the access handle passed to the callback of lock and tryLock, which guarantees that the data is only ever accessed when the mutex is locked.

Note: The implementation is based on os_unfair_lock_s (4 bytes).

Minimal Example

let mutex = try Mutex(0)

try! mutex.read { value in 
    print(value)
}

try! mutex.write { access in
    access { value in
        value += 42
    }
}
Real-world Example

Real-world Example

let unfairMutex = try UnfairMutex(0)

let count: Int = 1000

let queue = DispatchQueue(
    label: #function,
    attributes: .concurrent
)

let group = DispatchGroup()

for _ in 0..<count {
    group.enter()

    queue.async {
        defer {
            group.leave()
        }
        try! unfairMutex.write { value in
            value += 2
        }
    }
}

group.wait()

let value = try! unfairMutex.unwrap()

XCTAssertEqual(value, 2 * count)

RWLock<T>

A reader-writer lock

This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).

In comparison, a Mutex does not distinguish between readers or writers that acquire the lock, therefore blocking any threads waiting for the lock to become available. An RWLock will allow any number of readers to acquire the lock as long as a writer is not holding the lock.

Note: The implementation is based on pthread_rwlock_t (200 bytes).

Important: RWLock does not support priority inversion avoidance.

Minimal Example

let rwlock = try RWLock(0)

try! rwlock.read { value in 
    print(value)
}

try! rwlock.write { access in
    access { value in
        value += 42
    }
}
Real-world Example

Real-world Example

let rwlock = try RWLock(0)

let count: Int = 1000

let queue = DispatchQueue(
    label: #function,
    attributes: .concurrent
)

let group = DispatchGroup()

for _ in 0..<count {
    group.enter()

    queue.async {
        defer {
            group.leave()
        }
        try! rwlock.write { value in
            value += 2
        }
    }
}

group.wait()

let value = try! rwlock.unwrap()

XCTAssertEqual(value, 2 * count)

License

This project is licensed under the MPL-2.0 โ€“ see the LICENSE.md file for details.

sync's People

Contributors

regexident avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.