include/{mutex,wmutex,sem,queue}: Add timeout argument to blocking functions
The following functions potentially block the executing script indefinitely,
without a way to recover from the situation.
- queue_get()
- queue_get_file()
- sem_wait()
- mutex_lock()
- wmutex_lock()
It is a common pattern to implement deamons dispatching messages from a queue
in the following way using the "inst" module.
while inst_running; do
local msg
if msg=$(queue_get "$queue"); then
handle_message "$msg"
fi
done
The problem with this code is that queue_get() potentially waits indefinitely,
and the daemon won't terminate even if it was instructed to. To mitigate this
situation, this commit adds a timeout argument to all of the above functions,
allowing the caller to specify the number of seconds the functions should wait
at most. If the timeout parameter is 0 or omitted, the functions will wait
forever (as is the current behavior), so existing code that uses any of the
above functions and that wishes to retain the current behavior does not need to
be modified.