@@ -87,7 +87,7 @@ public suspend inline fun <T> Semaphore.withPermit(action: () -> T): T {
87
87
}
88
88
89
89
@Suppress(" UNCHECKED_CAST" )
90
- internal open class SemaphoreImpl (private val permits : Int , acquiredPermits : Int ) : Semaphore {
90
+ internal open class SemaphoreAndMutexImpl (private val permits : Int , acquiredPermits : Int ) {
91
91
/*
92
92
The queue of waiting acquirers is essentially an infinite array based on the list of segments
93
93
(see `SemaphoreSegment`); each segment contains a fixed number of slots. To determine a slot for each enqueue
@@ -144,11 +144,11 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
144
144
* cannot be greater than 2^31 in any real application.
145
145
*/
146
146
private val _availablePermits = atomic(permits - acquiredPermits)
147
- override val availablePermits: Int get() = max(_availablePermits .value, 0 )
147
+ val availablePermits: Int get() = max(_availablePermits .value, 0 )
148
148
149
149
private val onCancellationRelease = { _: Throwable , _: Unit , _: CoroutineContext -> release() }
150
150
151
- override fun tryAcquire (): Boolean {
151
+ fun tryAcquire (): Boolean {
152
152
while (true ) {
153
153
// Get the current number of available permits.
154
154
val p = _availablePermits .value
@@ -167,7 +167,7 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
167
167
}
168
168
}
169
169
170
- override suspend fun acquire () {
170
+ suspend fun acquire () {
171
171
// Decrement the number of available permits.
172
172
val p = decPermits()
173
173
// Is the permit acquired?
@@ -239,7 +239,7 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
239
239
}
240
240
}
241
241
242
- override fun release () {
242
+ fun release () {
243
243
while (true ) {
244
244
// Increment the number of available permits.
245
245
val p = _availablePermits .getAndIncrement()
@@ -346,12 +346,16 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
346
346
} else false
347
347
}
348
348
is SelectInstance <* > -> {
349
- trySelect(this @SemaphoreImpl , Unit )
349
+ trySelect(this @SemaphoreAndMutexImpl , Unit )
350
350
}
351
351
else -> error(" unexpected: $this " )
352
352
}
353
353
}
354
354
355
+ private class SemaphoreImpl (
356
+ permits : Int , acquiredPermits : Int
357
+ ): SemaphoreAndMutexImpl(permits, acquiredPermits), Semaphore
358
+
355
359
private fun createSegment (id : Long , prev : SemaphoreSegment ? ) = SemaphoreSegment (id, prev, 0 )
356
360
357
361
private class SemaphoreSegment (id : Long , prev : SemaphoreSegment ? , pointers : Int ) : Segment<SemaphoreSegment>(id, prev, pointers) {
0 commit comments