From dd482087edbda396899eb32fa2255b37dd755cfc Mon Sep 17 00:00:00 2001 From: Oliver Blanthorn Date: Sat, 9 May 2020 12:36:03 +0100 Subject: [PATCH] Remove now unused locks --- src/background.ts | 2 - src/content.ts | 2 - src/lib/config.ts | 1 - src/lib/locks.ts | 94 ----------------------------------------------- 4 files changed, 99 deletions(-) delete mode 100644 src/lib/locks.ts diff --git a/src/background.ts b/src/background.ts index e415635d..5aebb33f 100644 --- a/src/background.ts +++ b/src/background.ts @@ -25,7 +25,6 @@ import { AutoContain } from "@src/lib/autocontainers" import * as extension_info from "@src/lib/extension_info" import * as omnibox from "@src/background/omnibox" import * as R from "ramda" -import * as locks from "@src/lib/locks" // Add various useful modules to the window for debugging ; (window as any).tri = Object.assign(Object.create(null), { @@ -46,7 +45,6 @@ import * as locks from "@src/lib/locks" contentLocation: window.location, R, perf, - locks, }) import { HintingCmds } from "@src/background/hinting" diff --git a/src/content.ts b/src/content.ts index 222950f3..39bfed17 100644 --- a/src/content.ts +++ b/src/content.ts @@ -143,7 +143,6 @@ import * as updates from "@src/lib/updates" import * as urlutils from "@src/lib/url_util" import * as scrolling from "@src/content/scrolling" import * as R from "ramda" -import * as locks from "@src/lib/locks" import * as visual from "@src/lib/visual" /* tslint:disable:import-spacing */ ; (window as any).tri = Object.assign(Object.create(null), { @@ -175,7 +174,6 @@ import * as visual from "@src/lib/visual" R, updates, urlutils, - locks, }) logger.info("Loaded commandline content?", commandline_content) diff --git a/src/lib/config.ts b/src/lib/config.ts index 9d192ef9..ab83b72d 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -803,7 +803,6 @@ export class default_config { performance: "warning", state: "warning", styling: "warning", - locks: "warning", } /** diff --git a/src/lib/locks.ts b/src/lib/locks.ts deleted file mode 100644 index b4d4a508..00000000 --- a/src/lib/locks.ts +++ /dev/null @@ -1,94 +0,0 @@ -// This attempts to implement the Ricart-Agrawala algorithm: https://www.wikipedia.org/wiki/Ricart%E2%80%93Agrawala_algorithm -import {messageAllTabs} from "@src/lib/messaging" - -import {v1 as uuid} from "uuid" -import * as Logging from "@src/lib/logging" - -const logger = new Logging.Logger("locks") - -export const OWNED_LOCKS = new Set() - -export const DESIRED_LOCKS = {} - -export const ID = uuid() - -const now = () => (new Date()).getTime() + Math.random() // getTime is accurate only to ms, so fake microseconds with random - -export async function acquire(lockname: string, timeout = 2000) { - if (OWNED_LOCKS.has(lockname) || DESIRED_LOCKS.hasOwnProperty(lockname)) return; - const time = now() - - DESIRED_LOCKS[lockname] = time - - const a = await Promise.race([ - Promise.all([ - browser.runtime.sendMessage({type: "lock", command: "acquire", args: [lockname, time, ID]}), - messageAllTabs("lock", "acquire", [lockname, time, ID]), - ]), - new Promise(resolve => setTimeout( - () => { - resolve("ERROR: LOST THE RACE") - }, - timeout)), // Take the lock anyway after timeout - ]) - if (a === "ERROR: LOST THE RACE") {logger.warning("lock " + lockname + " was taken without confirmation")} - - delete DESIRED_LOCKS[lockname] - OWNED_LOCKS.add(lockname) -} - -/** - * Execute func after acquiring a named lock. If func takes longer than timeout, release the lock early. - * Returns the value of func. - */ -export async function withlock(lockname: string, func, timeout = 500) { - await acquire(lockname) - const p = (async () => func())() // Ensure function is promisified - const a = await Promise.race([ - p, - new Promise(resolve => setTimeout( - () => { - resolve("ERROR: LOST THE RACE") - }, - timeout)), // Release the lock anyway after timeout - ]) - if (a === "ERROR: LOST THE RACE") {logger.warning("lock " + lockname + " was released early")} - release(lockname) - return p -} - -export async function release(lockname: string) { - OWNED_LOCKS.delete(lockname) -} - -function lockhandler(msg, sender, sendResponse) { - if (msg.type !== "lock") return false - if (msg.command == "acquire") { - const lockname = msg.args[0] - const their_niceness = msg.args[1] - if (ID == msg.args[2]) {return sendResponse("Lock reply: I am you")} - (async () => { - while (true) { - // If we don't have the lock - if (!OWNED_LOCKS.has(lockname)) { - // and don't want it - if (!DESIRED_LOCKS.hasOwnProperty(lockname)) { - // Let them know they can have it - return sendResponse([lockname, now()]) - } - // or they wanted it before we asked for it - if (DESIRED_LOCKS[lockname] > their_niceness) { - // Let them know they can have it - return sendResponse([lockname, now()]) - } - } - - // Otherwise wait a bit and then look again - await new Promise(resolve => setTimeout(resolve, 100)) // Sleep 100ms - } - })() - } - return true // Tell the browser to wait for sendResponse even though it's async -} - -browser.runtime.onMessage.addListener(lockhandler) // Messaging.addListener doesn't allow us to send async responses - is this on purpose?