• Stars
    star
    424
  • Rank 98,496 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 1 year ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Synchronization Primitive for Swift Concurrency

Semaphore

A Synchronization Primitive for Swift Concurrency

Requirements: iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ • Swift 5.7+ / Xcode 14+

📖 Documentation


This package provides AsyncSemaphore, a traditional counting semaphore.

Unlike DispatchSemaphore, it does not block any thread. Instead, Swift concurrency tasks are suspended "awaiting" for the semaphore.

Usage

You can use a semaphore to suspend a task and resume it later:

let semaphore = AsyncSemaphore(value: 0)

Task {
  // Suspends the task until a signal occurs.
  await semaphore.wait()
  await doSomething()
}

// Resumes the suspended task.
semaphore.signal()

An actor can use a semaphore so that its methods can't run concurrently, avoiding the "actor reentrancy problem":

actor MyActor {
  private let semaphore = AsyncSemaphore(value: 1)
  
  func serializedMethod() async {
    // Makes sure no two tasks can execute
    // serializedMethod() concurrently. 
    await semaphore.wait()
    defer { semaphore.signal() }
    
    await doSomething()
    await doSomethingElse()
  }
}

A semaphore can generally limit the number of concurrent accesses to a resource:

class Downloader {
  private let semaphore: AsyncSemaphore

  /// Creates a Downloader that can run at most
  /// `maxDownloadCount` concurrent downloads. 
  init(maxDownloadCount: Int) {
    semaphore = AsyncSemaphore(value: maxDownloadCount) 
  }

  func download(...) async throws -> Data {
    try await semaphore.waitUnlessCancelled()
    defer { semaphore.signal() }
    return try await ...
  }
}

You can see in the latest example that the wait() method has a waitUnlessCancelled variant that throws CancellationError if the task is cancelled before a signal occurs.

For a nice introduction to semaphores, see The Beauty of Semaphores in Swift 🚦. The article discusses DispatchSemaphore, but it can easily be ported to Swift concurrency: get inspiration from the above examples.

More Repositories

1

GRDB.swift

A toolkit for SQLite databases, with a focus on application development
Swift
6,503
star
2

GRMustache

Flexible and production-ready Mustache templates for MacOS Cocoa and iOS
Objective-C
1,447
star
3

GRMustache.swift

Flexible Mustache templates for Swift
Swift
590
star
4

CombineExpectations

Utilities for tests that wait for Combine publishers
Swift
247
star
5

GRDBCombine

GRDB ❤️ Combine
223
star
6

GRDBQuery

The SwiftUI companion for GRDB
Swift
187
star
7

SortedDifference

A general sequence diffing algorithm, with a very specific purpose
Swift
52
star
8

GRDBSnapshotTesting

The snapshot testing library for GRDB
Swift
31
star
9

CombineTraits

Combine Publishers with Guarantees
Swift
24
star
10

GRDBObjc

FMDB-compatible bindings to GRDB.swift
Objective-C
20
star
11

WWDCCompanion

A demo app for full text search with GRDB.swift
Swift
18
star
12

GRDBCollections

Support for large result sets with GRDB
Swift
9
star
13

GRDBDemo

An iOS app that demonstrates the SQLite library GRDB and its FetchedRecordsController
9
star
14

GRValidation

Validation toolkit for Swift 2
Swift
8
star
15

GRDBDiff

Diff algorithms for SQLite, based on GRDB
Swift
5
star
16

CocoaHeadsDemo

Swift
4
star
17

GRMustacheSPM

A Swift package that uses GRMustache.swift
Swift
2
star
18

CSQLite

C
1
star
19

ARTemplating

Very simplistic performance comparison of mustache.js in a UIWebView and GRMustache
Objective-C
1
star
20

GRDBIssue636

Reproducible crash reported in https://github.com/groue/GRDB.swift/issues/636
Swift
1
star
21

SE-0235

A playground which explores SE-0235: https://forums.swift.org/t/se-0235-add-result-to-the-standard-library/17752
Swift
1
star
22

GRMustacheBenchmark

GRMustache Benchmarks
Objective-C
1
star