Skip to content

Commit

Permalink
use a Set in Semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Feb 7, 2024
1 parent a054cb0 commit c9c53b3
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions packages/effect/src/internal/effect/circular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import * as supervisor from "../supervisor.js"

/** @internal */
class Semaphore {
public waiters = new Array<() => boolean>()
public waiters = new Set<() => boolean>()
public taken = 0

constructor(readonly permits: number) {}
Expand All @@ -51,20 +51,14 @@ class Semaphore {
if (this.free < n) {
return false
}
const observerIndex = this.waiters.findIndex((cb) => cb === observer)
if (observerIndex !== -1) {
this.waiters.splice(observerIndex, 1)
}
this.waiters.delete(observer)
this.taken += n
resume(core.succeed(n))
return true
}
this.waiters.push(observer)
this.waiters.add(observer)
return Either.left(core.sync(() => {
const observerIndex = this.waiters.findIndex((cb) => cb === observer)
if (observerIndex !== -1) {
this.waiters.splice(observerIndex, 1)
}
this.waiters.delete(observer)
}))
}
this.taken += n
Expand All @@ -75,8 +69,8 @@ class Semaphore {
core.withFiberRuntime<never, never, void>((fiber) => {
this.taken = f(this.taken)
fiber.getFiberRef(currentScheduler).scheduleTask(() => {
while (this.waiters.length > 0) {
if (this.waiters[0]() === false) {
for (const waiter of this.waiters) {
if (waiter() === false) {
break
}
}
Expand Down

0 comments on commit c9c53b3

Please sign in to comment.