From 9044a7694a182cd029cb3bb658ae10753d070f61 Mon Sep 17 00:00:00 2001 From: havelessbemore Date: Mon, 13 May 2024 10:19:53 -0400 Subject: [PATCH] Add constants MAX_INT32_VALUE and MIN_INT32_VALUE --- src/mutexes/recursiveMutex.ts | 10 +++++----- src/semaphores/countingSemaphore.ts | 14 +++++++------- src/utils/constants.ts | 3 +++ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 src/utils/constants.ts diff --git a/src/mutexes/recursiveMutex.ts b/src/mutexes/recursiveMutex.ts index 3fe2f1d..9b82843 100644 --- a/src/mutexes/recursiveMutex.ts +++ b/src/mutexes/recursiveMutex.ts @@ -35,6 +35,11 @@ export const LOCK_BIT = 1; * 1. {@link https://en.cppreference.com/w/cpp/thread/recursive_mutex | C++ std::recursive_mutex} */ export class RecursiveMutex implements Lockable, SharedResource { + /** + * The maximum levels of recursive ownership. + */ + static readonly MAX = Number.MAX_SAFE_INTEGER; + /** * The number of locks acquired by the agent. */ @@ -45,11 +50,6 @@ export class RecursiveMutex implements Lockable, SharedResource { */ protected _mem: Int32Array; - /** - * The maximum levels of recursive ownership. - */ - static readonly MAX = Number.MAX_SAFE_INTEGER; - constructor(); /** * @param sharedBuffer The {@link SharedArrayBuffer} that backs the mutex. diff --git a/src/semaphores/countingSemaphore.ts b/src/semaphores/countingSemaphore.ts index a1c0cdc..5763175 100644 --- a/src/semaphores/countingSemaphore.ts +++ b/src/semaphores/countingSemaphore.ts @@ -2,11 +2,11 @@ import { CV_TIMED_OUT } from "../types/cvStatus"; import type { SharedResource } from "../types/sharedResource"; import type { TimedLockable } from "../types/timedLockable"; +import { ConditionVariable } from "../condVars/conditionVariable"; import { ERR_SEM_NEG_COUNT, ERR_SEM_OVERFLOW } from "../errors/constants"; import { lockGuard } from "../locks/lockGuard"; - -import { ConditionVariable } from "../condVars/conditionVariable"; import { TimedMutex } from "../mutexes/timedMutex"; +import { MAX_INT32_VALUE } from "../utils/constants"; /** * A counting semaphore based on shared memory and atomics, allowing for @@ -16,14 +16,14 @@ import { TimedMutex } from "../mutexes/timedMutex"; * 1. {@link https://en.cppreference.com/w/cpp/thread/counting_semaphore | C++ std::counting_semaphore} */ export class CountingSemaphore implements SharedResource { - private _gate: ConditionVariable; - private _mem: Int32Array; - private _mutex: TimedLockable; - /** * The maximum possible value of the internal counter */ - static readonly MAX = ~(1 << 31); + static readonly MAX = MAX_INT32_VALUE; + + private _gate: ConditionVariable; + private _mem: Int32Array; + private _mutex: TimedLockable; constructor(); /** diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..ed0b395 --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1,3 @@ +export const MAX_INT32_VALUE = 2147483647; // (2**31) - 1 + +export const MIN_INT32_VALUE = -2147483648; // -(2**31)