Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/src/lib/Instructions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
in this demo page does polling to condition the wait as opposed to blindly waiting:
</p>
<code class="fs-5"><pre>if (!ManualResetEvent.isSignaled(pause)) &#123;
ManualResetEvent.wait(pause);
ManualResetEvent.waitSync(pause);
&#125;</pre></code>
<p>
Still, even with this modification, you'll see a big difference in times between a pausable and a
Expand Down
2 changes: 1 addition & 1 deletion pages/src/workers/exampleWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function isPrimePausable(n: number, pause: Token, cancelToken?: Token) {
for (let i = 2; i <= n / 2; ++i) {
CancellationSource.throwIfSignaled(cancelToken);
if (!ManualResetEvent.isSignaled(pause)) {
ManualResetEvent.wait(pause);
ManualResetEvent.waitSync(pause);
}
if (n % i === 0) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/sync/AutoResetEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AutoResetEvent extends Event {
* @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the
* specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place.
*/
static wait(token: Token, timeout?: number) {
static waitSync(token: Token, timeout?: number) {
checkToken(token, ...autoResetEventIdentityData);
// Performance optimization: Blind attempt.
if (Atomics.compareExchange(token, 0, 1, 0) === 1) {
Expand All @@ -57,7 +57,7 @@ export class AutoResetEvent extends Event {
* @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the
* specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place.
*/
static async waitAsync(token: Token, timeout?: number) {
static async wait(token: Token, timeout?: number) {
checkToken(token, ...autoResetEventIdentityData);
// Performance optimization: Blind attempt.
if (Atomics.compareExchange(token, 0, 1, 0) === 1) {
Expand Down
12 changes: 6 additions & 6 deletions src/sync/ManualResetEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export function isSignaled(identifierData: IdentifierData, token: Token) {
return Atomics.load(token, 0) === 1;
}

export function wait(identifierData: IdentifierData, token: Token, timeout?: number) {
export function waitSync(identifierData: IdentifierData, token: Token, timeout?: number) {
checkToken(token, ...identifierData);
return Atomics.wait(token, 0, 0, timeout);
}

export async function waitAsync(identifierData: IdentifierData, token: Token, timeout?: number) {
export async function wait(identifierData: IdentifierData, token: Token, timeout?: number) {
checkToken(token, ...identifierData);
const result = Atomics.waitAsync(token, 0, 0, timeout);
return result.async ? await result.value : result.value;
Expand Down Expand Up @@ -52,8 +52,8 @@ export class ManualResetEvent extends Event {
* @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the
* specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place.
*/
static wait(token: Token, timeout?: number) {
return wait(manualResetEventIdentityData, token, timeout);
static waitSync(token: Token, timeout?: number) {
return waitSync(manualResetEventIdentityData, token, timeout);
}
/**
* Asynchronously waits on the specified manually-resettable token to be signaled.
Expand All @@ -64,7 +64,7 @@ export class ManualResetEvent extends Event {
* @returns `'ok'` when the waiting is over because the token signaled while waiting on it, `'timed-out'` when the
* specified timeout elapsed and the token did not signal, or `'not-equal'` if no wait took place.
*/
static waitAsync(token: Token, timeout?: number) {
return waitAsync(manualResetEventIdentityData, token, timeout);
static wait(token: Token, timeout?: number) {
return wait(manualResetEventIdentityData, token, timeout);
}
};
4 changes: 2 additions & 2 deletions tests/ut/helpers/test-wait-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ self.onmessage = function(event) {
let result;
self.postMessage('running');
if (waitFunction === 'ManualResetEvent.wait') {
result = ManualResetEvent.wait(sharedBuffer, timeout);
result = ManualResetEvent.waitSync(sharedBuffer, timeout);
} else if (waitFunction === 'AutoResetEvent.wait') {
result = AutoResetEvent.wait(sharedBuffer, timeout);
result = AutoResetEvent.waitSync(sharedBuffer, timeout);
} else {
throw new Error('Unknown wait function: ' + waitFunction);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/ut/sync/AutoResetEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ describe('AutoResetEvent', () => {
});
});

describe('static wait', () => {
describe('static waitSync', () => {
it('Should throw when the given token is not the token of an AutoResetEvent object.', () => {
const foreignEvent = new ManualResetEvent();

expect(() => AutoResetEvent.wait(foreignEvent.token)).toThrow();
expect(() => AutoResetEvent.waitSync(foreignEvent.token)).toThrow();
});

it('Should handle timeout.', () => {
const result = AutoResetEvent.wait(eventObj.token, 10);
const result = AutoResetEvent.waitSync(eventObj.token, 10);

expect(result).toBe('timed-out');
});

it('Should handle immediate success when already signaled.', () => {
eventObj.signal();
const result = AutoResetEvent.wait(eventObj.token);
const result = AutoResetEvent.waitSync(eventObj.token);

expect(result).toBe('not-equal');
// Verify the event auto-reset (signal was consumed)
Expand All @@ -102,19 +102,19 @@ describe('AutoResetEvent', () => {
});
});

describe('static waitAsync', () => {
it('Should throw when the given token is not the token of an AutoResetEvent object.', () => {
describe('static wait', () => {
it('Should throw when the given token is not the token of an AutoResetEvent object.', async () => {
const foreignEvent = new ManualResetEvent();

expect(() => AutoResetEvent.wait(foreignEvent.token)).toThrow();
await expect(AutoResetEvent.wait(foreignEvent.token)).rejects.toThrow();
});
it('Should handle async wait result.', async () => {
// Signal the event after a short delay to test async wait
setTimeout(() => {
eventObj.signal();
}, 0);

const result = await AutoResetEvent.waitAsync(eventObj.token, 1000);
const result = await AutoResetEvent.wait(eventObj.token, 1000);

expect(result).toBe('ok');
expect(Atomics.load(eventObj.token, 0)).toBe(0);
Expand All @@ -124,15 +124,15 @@ describe('AutoResetEvent', () => {
// Pre-signal the event so waitAsync returns immediately
eventObj.signal();

const result = await AutoResetEvent.waitAsync(eventObj.token);
const result = await AutoResetEvent.wait(eventObj.token);

expect(result).toBe('not-equal');
// Verify the event auto-reset (signal was consumed)
expect(Atomics.load(eventObj.token, 0)).toBe(0);
});

it('Should handle timeout in async wait.', async () => {
const result = await AutoResetEvent.waitAsync(eventObj.token, 10);
const result = await AutoResetEvent.wait(eventObj.token, 10);

expect(result).toBe('timed-out');
});
Expand Down
18 changes: 9 additions & 9 deletions tests/ut/sync/ManualResetEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ describe('ManualResetEvent', () => {
tokenTypeTest(AutoResetEvent, ManualResetEvent.isSignaled, ManualResetEvent);
});

describe('static wait', () => {
tokenTypeTest(AutoResetEvent, ManualResetEvent.wait, ManualResetEvent);
describe('static waitSync', () => {
tokenTypeTest(AutoResetEvent, ManualResetEvent.waitSync, ManualResetEvent);

it('Should handle timeout.', () => {
const result = ManualResetEvent.wait(eventObj.token, 10);
const result = ManualResetEvent.waitSync(eventObj.token, 10);

expect(result).toBe('timed-out');
});

it('Should handle immediate success when already signaled.', () => {
eventObj.signal();
const result = ManualResetEvent.wait(eventObj.token);
const result = ManualResetEvent.waitSync(eventObj.token);
expect(result).toBe('not-equal');
});

Expand All @@ -94,15 +94,15 @@ describe('ManualResetEvent', () => {
});
});

describe('static waitAsync', () => {
tokenTypeTest(AutoResetEvent, ManualResetEvent.waitAsync, ManualResetEvent);
describe('static wait', () => {
tokenTypeTest(AutoResetEvent, ManualResetEvent.wait, ManualResetEvent);
it('Should handle async wait result.', async () => {
// Signal the event after a short delay to test async wait
setTimeout(() => {
eventObj.signal();
}, 0);

const result = await ManualResetEvent.waitAsync(eventObj.token, 10);
const result = await ManualResetEvent.wait(eventObj.token, 10);

expect(result).toBe('ok');
});
Expand All @@ -111,13 +111,13 @@ describe('ManualResetEvent', () => {
// Pre-signal the event so waitAsync returns immediately
eventObj.signal();

const result = await ManualResetEvent.waitAsync(eventObj.token);
const result = await ManualResetEvent.wait(eventObj.token);

expect(result).toBe('not-equal');
});

it('Should handle timeout in async wait.', async () => {
const result = await ManualResetEvent.waitAsync(eventObj.token, 10);
const result = await ManualResetEvent.wait(eventObj.token, 10);

expect(result).toBe('timed-out');
});
Expand Down
Loading