Remove now unused locks

This commit is contained in:
Oliver Blanthorn 2020-05-09 12:36:03 +01:00
parent 8ada387819
commit dd482087ed
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
4 changed files with 0 additions and 99 deletions

View file

@ -25,7 +25,6 @@ import { AutoContain } from "@src/lib/autocontainers"
import * as extension_info from "@src/lib/extension_info" import * as extension_info from "@src/lib/extension_info"
import * as omnibox from "@src/background/omnibox" import * as omnibox from "@src/background/omnibox"
import * as R from "ramda" import * as R from "ramda"
import * as locks from "@src/lib/locks"
// Add various useful modules to the window for debugging // Add various useful modules to the window for debugging
; (window as any).tri = Object.assign(Object.create(null), { ; (window as any).tri = Object.assign(Object.create(null), {
@ -46,7 +45,6 @@ import * as locks from "@src/lib/locks"
contentLocation: window.location, contentLocation: window.location,
R, R,
perf, perf,
locks,
}) })
import { HintingCmds } from "@src/background/hinting" import { HintingCmds } from "@src/background/hinting"

View file

@ -143,7 +143,6 @@ import * as updates from "@src/lib/updates"
import * as urlutils from "@src/lib/url_util" import * as urlutils from "@src/lib/url_util"
import * as scrolling from "@src/content/scrolling" import * as scrolling from "@src/content/scrolling"
import * as R from "ramda" import * as R from "ramda"
import * as locks from "@src/lib/locks"
import * as visual from "@src/lib/visual" import * as visual from "@src/lib/visual"
/* tslint:disable:import-spacing */ /* tslint:disable:import-spacing */
; (window as any).tri = Object.assign(Object.create(null), { ; (window as any).tri = Object.assign(Object.create(null), {
@ -175,7 +174,6 @@ import * as visual from "@src/lib/visual"
R, R,
updates, updates,
urlutils, urlutils,
locks,
}) })
logger.info("Loaded commandline content?", commandline_content) logger.info("Loaded commandline content?", commandline_content)

View file

@ -803,7 +803,6 @@ export class default_config {
performance: "warning", performance: "warning",
state: "warning", state: "warning",
styling: "warning", styling: "warning",
locks: "warning",
} }
/** /**

View file

@ -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?