1- import Foundation
1+ @ preconcurrency import Foundation
22import OrderedCollections
33
44/// An event object that controls access to a resource between high and low priority tasks
@@ -41,13 +41,15 @@ public actor AsyncCountdownEvent: AsyncObject {
4141 /// Queued tasks are resumed from suspension when event is set and until current count exceeds limit.
4242 public var isSet : Bool { currentCount >= 0 && currentCount <= limit }
4343
44+ // MARK: Internal
45+
4446 /// Add continuation with the provided key in `continuations` map.
4547 ///
4648 /// - Parameters:
4749 /// - continuation: The `continuation` to add.
4850 /// - key: The key in the map.
4951 @inlinable
50- func addContinuation (
52+ func _addContinuation (
5153 _ continuation: Continuation ,
5254 withKey key: UUID
5355 ) {
@@ -59,7 +61,7 @@ public actor AsyncCountdownEvent: AsyncObject {
5961 ///
6062 /// - Parameter key: The key in the map.
6163 @inlinable
62- func removeContinuation ( withKey key: UUID ) {
64+ func _removeContinuation ( withKey key: UUID ) {
6365 let continuation = continuations. removeValue ( forKey: key)
6466 continuation? . cancel ( )
6567 }
@@ -68,14 +70,14 @@ public actor AsyncCountdownEvent: AsyncObject {
6870 ///
6971 /// - Parameter number: The number to decrement count by.
7072 @inlinable
71- func decrementCount ( by number: UInt = 1 ) {
73+ func _decrementCount ( by number: UInt = 1 ) {
7274 guard currentCount > 0 else { return }
7375 currentCount -= number
7476 }
7577
7678 /// Resume previously waiting continuations for countdown event.
7779 @inlinable
78- func resumeContinuations ( ) {
80+ func _resumeContinuations ( ) {
7981 while !continuations. isEmpty && isSet {
8082 let ( _, continuation) = continuations. removeFirst ( )
8183 continuation. resume ( )
@@ -84,27 +86,29 @@ public actor AsyncCountdownEvent: AsyncObject {
8486 }
8587
8688 /// Suspends the current task, then calls the given closure with a throwing continuation for the current task.
87- /// Continuation can be cancelled with error if current task is cancelled, by invoking `removeContinuation `.
89+ /// Continuation can be cancelled with error if current task is cancelled, by invoking `_removeContinuation `.
8890 ///
89- /// Spins up a new continuation and requests to track it with key by invoking `addContinuation `.
90- /// This operation cooperatively checks for cancellation and reacting to it by invoking `removeContinuation `.
91+ /// Spins up a new continuation and requests to track it with key by invoking `_addContinuation `.
92+ /// This operation cooperatively checks for cancellation and reacting to it by invoking `_removeContinuation `.
9193 /// Continuation can be resumed with error and some cleanup code can be run here.
9294 ///
9395 /// - Throws: If `resume(throwing:)` is called on the continuation, this function throws that error.
9496 @inlinable
95- func withPromisedContinuation ( ) async throws {
97+ func _withPromisedContinuation ( ) async throws {
9698 let key = UUID ( )
9799 try await withTaskCancellationHandler { [ weak self] in
98100 Task { [ weak self] in
99- await self ? . removeContinuation ( withKey: key)
101+ await self ? . _removeContinuation ( withKey: key)
100102 }
101103 } operation: { ( ) -> Continuation . Success in
102104 try await Continuation . with { continuation in
103- self . addContinuation ( continuation, withKey: key)
105+ self . _addContinuation ( continuation, withKey: key)
104106 }
105107 }
106108 }
107109
110+ // MARK: Public
111+
108112 /// Creates new countdown event with the limit count down up to and an initial count.
109113 /// By default, both limit and initial count are zero.
110114 ///
@@ -141,7 +145,7 @@ public actor AsyncCountdownEvent: AsyncObject {
141145 /// are resumed from suspension until current count exceeds limit.
142146 public func reset( ) {
143147 self . currentCount = initialCount
144- resumeContinuations ( )
148+ _resumeContinuations ( )
145149 }
146150
147151 /// Resets initial count and current count to specified value.
@@ -153,7 +157,7 @@ public actor AsyncCountdownEvent: AsyncObject {
153157 public func reset( to count: UInt ) {
154158 initialCount = count
155159 self . currentCount = count
156- resumeContinuations ( )
160+ _resumeContinuations ( )
157161 }
158162
159163 /// Registers a signal (decrements) with the countdown event.
@@ -171,8 +175,8 @@ public actor AsyncCountdownEvent: AsyncObject {
171175 ///
172176 /// - Parameter count: The number of signals to register.
173177 public func signal( repeat count: UInt ) {
174- decrementCount ( by: count)
175- resumeContinuations ( )
178+ _decrementCount ( by: count)
179+ _resumeContinuations ( )
176180 }
177181
178182 /// Waits for, or increments, a countdown event.
@@ -184,6 +188,6 @@ public actor AsyncCountdownEvent: AsyncObject {
184188 @Sendable
185189 public func wait( ) async {
186190 if isSet { currentCount += 1 ; return }
187- try ? await withPromisedContinuation ( )
191+ try ? await _withPromisedContinuation ( )
188192 }
189193}
0 commit comments