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
Checks if the critical section for the specified object is currently locked.
175
+
176
+
**Parameters:**
177
+
178
+
-`obj` - The object to check
179
+
180
+
**Returns:**
181
+
182
+
-`boolean` - `true` if the critical section is locked, `false` otherwise
183
+
184
+
**Example:**
185
+
186
+
```typescript
187
+
if (criticalSection.isLocked(myResource)) {
188
+
console.log('Resource is currently locked');
189
+
} else {
190
+
console.log('Resource is available');
191
+
}
192
+
```
193
+
145
194
### `criticalSection.leave(obj: object): void`
146
195
147
196
Leaves the critical section for the given object, allowing queued entries to proceed.
@@ -305,6 +354,39 @@ printDocument('Report B');
305
354
printDocument('Report C');
306
355
```
307
356
357
+
### Monitoring Resource Availability
358
+
359
+
```typescript
360
+
const sharedCache = { name: 'user-cache' };
361
+
362
+
// Wait for cache to be free before doing cleanup
363
+
asyncfunction cleanupCache() {
364
+
console.log('Waiting for cache to be released...');
365
+
366
+
// Wait without locking - other operations can still use the cache
367
+
const released =awaitcriticalSection.waitLeave(sharedCache, 5000);
368
+
369
+
if (released) {
370
+
// Now we can safely check or perform non-critical operations
371
+
if (!criticalSection.isLocked(sharedCache)) {
372
+
console.log('Cache is free, starting cleanup...');
373
+
// Perform cleanup operations
374
+
}
375
+
} else {
376
+
console.log('Cache is still busy, will retry later');
377
+
}
378
+
}
379
+
380
+
// Check status without affecting the lock
381
+
function getCacheStatus() {
382
+
if (criticalSection.isLocked(sharedCache)) {
383
+
return'Cache is currently in use';
384
+
} else {
385
+
return'Cache is available';
386
+
}
387
+
}
388
+
```
389
+
308
390
## How It Works
309
391
310
392
Uses a `WeakMap` to associate objects with their critical section state. Different objects = independent locks. When objects are garbage collected, their critical section state is automatically cleaned up.
0 commit comments