]> git.corax.cc Git - toolbox/commit
include/{mutex,wmutex,sem,queue}: Add timeout argument to blocking functions
authorMatthias Kruk <matthias.kruk@miraclelinux.com>
Sat, 29 May 2021 04:17:48 +0000 (13:17 +0900)
committerMatthias Kruk <matthias.kruk@miraclelinux.com>
Sat, 29 May 2021 04:32:03 +0000 (13:32 +0900)
commitdf97069dce689b550183704d00cee8a9e51e4054
treef3ea7b22a50ddd634372ae3e2654a19a43937e28
parentb3cf44567d9a01f8bdba85ddcf5df2f3d070d55d
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.
include/mutex.sh
include/queue.sh
include/sem.sh
include/wmutex.sh