Please add a "mutex" helper for animations / scripts

It would be useful to have a mutex helper which animations / scripts could try to lock so that multiple scripts can avoid interfering with each other.

it is simplest to consider first the condition: mutex.foo.state == ‘on’
in this case, the script would check the internal state of mutex.foo and if it is ‘unlocked’, the condition is true, and internally mutex.foo is atomically set to ‘locked’ and associated with the instance of the script in question. Following this, until unlocked, the state for the locking script is always ‘on’, and for any other script the state is always ‘off’.

(one possible way to implement this could be to give the mutex entity a kind of ‘virtual’ state, plus an attribute ‘owner_id’. If the mutex is unlocked, then the owner_id is 0 or null, and when a script checks if the state is on, set the owner_id to point to the script and return true. If owner_id is set, then the check returns false for the calling script unless the entity_id is equal to the owner_id).

The mutex will be unlocked either when the locking script terminates (either by reaching the end or failing), or when the script holding the mutex lock unlocks it voluntarily using the service call “mutex.unlock”

Any attempts to set the state of the mutex (to ‘on’ or ‘off’) directly should fail (ie. the state is readonly).

A script may also check if the state of the mutex is ‘off’. In this case, true implies that another script holds the mutex, a return value of false means that the mutex is unlocked, but does not actually lock it (exceptionally, for a script that holds the lock the check for ‘off’ should also return false)

One use case for such a mutex is when a script wants to set a temporary condition which might trigger other scripts, leading to undesirable or unknown effects. Such scripts could check for a common mutex thus avoiding such interference.

Using the state as an animation trigger seems not particularly useful, however it could be used as a step trigger (here the timeout and “continue on timeout” features become very useful)

Moved to here: Add a "mutex" automation helper

This can be closed as a duplicate of the other.