Browse the docs

Lock or unlock

Takes or releases a named lock so only one run at a time works on a shared thing.

action the lock name

Keyboard Maestro: Semaphore

Details #

A semaphore is a named lock. Before touching something that two runs must not share at once, lock it. When you're done, unlock it. If another run holds the lock, yours waits for up to timeout seconds, then the step fails.

The name is free text. Any macros that use the same name compete for the same lock.

Locking a name this run already holds does nothing and doesn't wait. Unlocking a name this run doesn't hold does nothing either, and the Trace says so.

Locks are released for you when the run ends, however it ends, so a failed macro can't leave one stuck. A lock also expires when the holder's time limit would have.

Take turns using a shared resource
Do
  1. Wait for the lock “printer”
  2. Show “Using the printer”
  3. Release the lock “printer”
Open in Switchboard to try it

Parameters #

ParameterTypeRequiredValuesWhat it does
nametextYesThe lock's name.
actionone oflock unlock resetlock (default), unlock or reset. reset frees the lock whoever holds it.
timeoutnumberSeconds to wait for a busy lock. Default 10. This value also sets the step's time limit unless the step has its own timeout, and a wait stops 0.3 seconds before that limit. So a long wait needs no second setting, but a short step timeout cuts the wait short.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Skip the work if someone else has it
Do
  1. Wait for the lock “sync”
  2. Show “Syncing”
  3. Release the lock “sync”
Open in Switchboard to try it

Gotchas #

  • A dry run simulates this step. Nothing is locked.

  • Locks live in memory. They don't survive a restart of the app, and they don't protect against other software.

  • For one macro that must never overlap itself, the macro's own concurrent setting (queue, cancelPrevious, parallel, ignore) is simpler than a lock.

See also #

Run macro, Set variable