Sleeps for a specified time, but can be woken up.
mp
current-process-pause time &optional function &rest args => result
time⇩ |
A positive number. |
function⇩ |
A function designator. |
args⇩ |
Arguments passed to function. |
result |
A Lisp object. |
The function current-process-pause
sleeps for time seconds, but wakes up if another process did something to wake up the current process (normally this is process-poke, but it can also be process-interrupt, process-stop, process-unstop or process-kill).
current-process-pause
is quite similar to cl:sleep, but it returns if anything causes the process to wake up, even if the time did not pass.
If function is passed just before going to sleep, current-process-pause
applies function to args, and if this returns a true value current-process-pause
returns it immediately. function and args are not used otherwise. If another process calls process-poke on the current process after setting something that causes function to return true, it guarantees that current-process-pause
will return immediately without sleeping.
If another process woke up the current process, current-process-pause
returns the keyword :poked
. If it slept the full time, it returns nil
.
current-process-pause
is applied only once, and within the dynamic scope of current-process-pause
. It therefore does not have any of the restrictions that the wait-function of process-wait has.:poked
and the process being poked in some way.:poked
in a situation when it seems unexpected. For example, if the current process does:
(mailbox-read *mailbox*) ... (current-process-pause)
the call to current-process-pause
may return poked, because a process that sent an event to the mailbox tried to poke the current process, and by the time this poke happened the current process is already inside current-process-pause
. The only guarantees are that current-process-pause
does not wait when a poke occurred, and that it returns nil
only when it paused the full time.
Supposed you want to have a process that each minute does some cleanup, but may also be told by other processes to go and do the cleanup. The process be doing:
(loop (mp:current-process-pause 60 'check-for-need-cleanup) (do-cleanup))
Another process which wants to provoke a cleanup will do:
(setup-cleanup-flag) (mp:process-poke *cleanup-process*)
Note that check-for-need-cleanup
is passed to current-process-pause
, because another process may call process-poke after current-process-pause
was called but before it went to sleep. If check-for-need-cleanup
was not passed, current-process-pause
would unnecessarily sleep the whole 60 seconds in this case. The same thing could be implemented by process-wait-with-timeout, but the implementation above does not require a wait function that can run in another dynamic scope repeatedly at arbitrary times, and it uses much less system resources. It is also easier to debug.
LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:51