Skip to content

swhitty/swift-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Codecov Platforms Swift 5.8 License Twitter

Introduction

AllocatedLock is a lightweight cross platform lock with an API compatible with OSAllocatedUnfairLock available in macOS Ventura, iOS 16 and later. The lock wraps os_unfair_lock_t on Darwin platforms and pthread_mutex_t on Linux.

Installation

AllocatedLock can be installed by using Swift Package Manager.

Note: AllocatedLock requires Swift 5.5 on Xcode 14+. It runs on iOS 13+, tvOS 13+, macOS 10.15+ and Linux.

To install using Swift Package Manager, add this to the dependencies: section in your Package.swift file:

.package(url: "https://github.com/swhitty/AllocatedLock.git", .upToNextMajor(from: "0.0.1"))

Usage

Usage is similar with OSAllocatedUnfairLock.

The recommended usage to create a lock that protects some state:

let state = AllocatedLock<Int>(initialState: 0)

Use .withLock to acquire the lock to read the state:

let val = state.withLock { $0 }

Or mutate the state

let val = state.withLock { $0 += 1 }

Credits

AllocatedLock is primarily the work of Simon Whitty.

(Full list of contributors)

About

Cross platform lock backporting the Swift 6 Mutex API to earlier platforms

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •