I have a script in Greasemonkey that checks for updates and prompts the user to download if necessary. The issue arises when multiple tabs are opened simultaneously, causing the script to notify the user in each tab at the same time, which can be frustrating.
The only way I can communicate between instances of the script is through GM_setValue/GM_getValue, giving access to a key/value store.
I am looking to implement a locking system (let's call it GM_setLock/GM_releaseLock) to manage this situation:
GM_setLock();
const tried_update = GM_getValue(available_version);
GM_setValue(available_version, true);
GM_releaseLock();
if (!tried_update) { prompt_user() }
Without locking, multiple instances could read the value before any of them actually set it, resulting in the user being notified more than once.
However, figuring out how to implement locking with just atomic read and write operations - without return previous value capability - is challenging. Any suggestions?