You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Within the same context, semaphores with the same ID must be created with the same initial value. Semaphores created with a different value are unusable. Their methods will reject with a `SemaphoreCreationError`.
95
+
96
+
Semaphores have two methods: `acquire()` and `acquireNow()`. Use `acquire()` to decrement the semaphore's value. If the semaphore's value would become negative, instead `acquire()` waits until the semaphore's value is high enough.
`acquire()` returns a function, `release()`, which increments the semaphore's value by the same amount as was acquired.
104
+
105
+
The semaphore is _managed_: if you don't call `release()`, it'll be run automatically when the test worker exits. Any pending `acquire()` calls will also be removed from the queue at this time.
106
+
107
+
`acquireNow()` works like `acquire()`, except that if the semaphore can't be decremented immediately, `acquireNow()` rejects with a `SemaphoreDownError` rather than wait.
108
+
109
+
Semaphores are _weighted_. `acquire()` and `acquireNow()` accept a non-negative integer amount, defaulting to `1`, by which to decrement or increment the value:
110
+
111
+
```js
112
+
awaitsemaphore.acquire(0);
113
+
awaitsemaphore.acquireNow(2);
114
+
```
115
+
116
+
You can also pass an amount to `release()` to release just part of the acquisition at a time:
117
+
118
+
```js
119
+
constrelease=awaitsemaphore.acquire(3); // Decrements the semaphore by 3
120
+
release(1); // Increments the semaphore by 1
121
+
release(); // Increments the semaphore by the remaining 2
122
+
```
123
+
124
+
`acquire()` calls resolve in FIFO order. If the current value is `1`, and a call tries to acquire `2`, subsequent `acquire()` calls have to wait, even if they want to acquire just `1`.
125
+
126
+
`acquireNow()` skips the queue and decrements immediately if possible.
127
+
128
+
#### Lower-level, unmanaged semaphores
129
+
130
+
You can create a lower-level, _unmanaged_ semaphore which doesn't have any auto-release behavior. Instead you need to increment the semaphore in code.
131
+
132
+
```js
133
+
constinitialValue=3; // Must be a non-negative integer.
Unmanaged semaphores mustn't use the same ID as a managed semaphore, within the same context. Semaphores with the same ID must be created with the same initial value. Mismatched managed and unmanaged semaphores, or those created with different values are unusable. Their methods will reject with a `SemaphoreCreationError`.
138
+
139
+
Unmanaged semaphores have three methods. `down()` and `downNow()` decrement the value and `up()` increments:
140
+
141
+
```js
142
+
awaitsemaphore.down(0);
143
+
awaitsemaphore.downNow(2);
144
+
awaitsemaphore.up(); // `amount` defaults to 1
145
+
```
146
+
147
+
Like the `acquire()` and `acquireNow()` methods of managed semaphores, `down()` waits for the semaphore's value to be at least the requested amount, while `downNow()` rejects with `SemaphoreDownError` if the value cannot be decremented immediately.
148
+
149
+
These unmanaged semaphores do not release the "acquired" amount when a test worker exits.
0 commit comments