Skip to content

Commit

Permalink
chore: rename Semaphore to Lock
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Aug 25, 2022
1 parent 2e9e493 commit 90c6d7e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 34 deletions.
6 changes: 3 additions & 3 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DBOp, DBSaveLookups, DBSetBlockOrHeader, DBSetHashToNumber, DBSetTD } f
import { DBManager } from './db/manager'
import { DBTarget } from './db/operation'
import { genesisStateRoot } from './genesisStates'
import { Semaphore } from './semaphore'
import { Lock } from './lock'

import type { Consensus } from './consensus'
import type { GenesisState } from './genesisStates'
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Blockchain implements BlockchainInterface {
private _heads: { [key: string]: Buffer }

protected _isInitialized = false
private _lock: Semaphore
private _lock: Lock

_common: Common
private _hardforkByHeadBlockNumber: boolean
Expand Down Expand Up @@ -152,7 +152,7 @@ export class Blockchain implements BlockchainInterface {

this._heads = {}

this._lock = new Semaphore(1)
this._lock = new Lock()

if (opts.genesisBlock && !opts.genesisBlock.isGenesis()) {
throw 'supplied block is not a genesis block'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
// Based on https://github.com/jsoendermann/semaphore-async-await/blob/master/src/Semaphore.ts
export class Semaphore {
private permits: number
export class Lock {
private permits: number = 1
private promiseResolverQueue: Array<(v: boolean) => void> = []

/**
* Creates a semaphore.
* @param permits The number of permits, i.e. strands of execution being allowed
* to run in parallel.
* This number can be initialized with a negative integer.
*/
constructor(permits: number) {
this.permits = permits
}

/**
* Returns a promise used to wait for a permit to become available. This method should be awaited on.
* @returns A promise that gets resolved when execution is allowed to proceed.
Expand All @@ -37,7 +27,7 @@ export class Semaphore {

if (this.permits > 1 && this.promiseResolverQueue.length > 0) {
// eslint-disable-next-line no-console
console.warn('Semaphore.permits should never be > 0 when there is someone waiting.')
console.warn('Lock.permits should never be > 0 when there is someone waiting.')
} else if (this.permits === 1 && this.promiseResolverQueue.length > 0) {
// If there is someone else waiting, immediately consume the permit that was released
// at the beginning of this function and let the waiting function resume.
Expand Down
4 changes: 2 additions & 2 deletions packages/trie/src/trie/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { keccak256 } from 'ethereum-cryptography/keccak'
import { CheckpointDB, MapDB } from '../db'
import { verifyRangeProof } from '../proof/range'
import { ROOT_DB_KEY } from '../types'
import { Lock } from '../util/lock'
import { bufferToNibbles, doKeysMatch, matchingNibbleLength } from '../util/nibbles'
import { TrieReadStream as ReadStream } from '../util/readStream'
import { Semaphore } from '../util/semaphore'
import { WalkController } from '../util/walkController'

import { BranchNode, ExtensionNode, LeafNode, decodeNode, decodeRawNode, isRawNode } from './node'
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Trie {
/** The backend DB */
protected _db: CheckpointDB
protected _hashLen: number
protected _lock: Semaphore = new Semaphore(1)
protected _lock = new Lock()
protected _root: Buffer

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
// Based on https://github.com/jsoendermann/semaphore-async-await/blob/master/src/Semaphore.ts
export class Semaphore {
private permits: number
export class Lock {
private permits: number = 1
private promiseResolverQueue: Array<(v: boolean) => void> = []

/**
* Creates a semaphore.
* @param permits The number of permits, i.e. strands of execution being allowed
* to run in parallel.
* This number can be initialized with a negative integer.
*/
constructor(permits: number) {
this.permits = permits
}

/**
* Returns a promise used to wait for a permit to become available. This method should be awaited on.
* @returns A promise that gets resolved when execution is allowed to proceed.
Expand All @@ -37,7 +27,7 @@ export class Semaphore {

if (this.permits > 1 && this.promiseResolverQueue.length > 0) {
// eslint-disable-next-line no-console
console.warn('Semaphore.permits should never be > 0 when there is someone waiting.')
console.warn('Lock.permits should never be > 0 when there is someone waiting.')
} else if (this.permits === 1 && this.promiseResolverQueue.length > 0) {
// If there is someone else waiting, immediately consume the permit that was released
// at the beginning of this function and let the waiting function resume.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Based on https://github.com/jsoendermann/semaphore-async-await/blob/master/__tests__/Semaphore.spec.ts
import * as tape from 'tape'

import { Semaphore } from '../../src/util/semaphore'
import { Lock } from '../../src/util/lock'

const wait = (ms: number) => new Promise((r) => setTimeout(r, ms))

tape('Semaphore', (t) => {
tape('Lock', (t) => {
t.test('should lock', async (st) => {
let global = 0
const lock = new Semaphore(1)
const lock = new Lock()

const f = async () => {
await lock.acquire()
Expand Down

0 comments on commit 90c6d7e

Please sign in to comment.