Skip to content

Instantly share code, notes, and snippets.

@deadtrickster
Created October 8, 2016 12:29
Show Gist options
  • Save deadtrickster/df1780d5312e157db6839f081b9344c1 to your computer and use it in GitHub Desktop.
Save deadtrickster/df1780d5312e157db6839f081b9344c1 to your computer and use it in GitHub Desktop.
(defun receive-message (mailbox &key timeout)
"Removes the oldest message from MAILBOX and returns it as the primary
value, and a secondary value of T. If MAILBOX is empty waits until a message
arrives.
If TIMEOUT is provided, and no message arrives within the specified interval,
returns primary and secondary value of NIL."
(tagbody
;; Disable interrupts for keeping semaphore count in sync with
;; #msgs in the mailbox.
(sb-sys:without-interrupts
(sb-sys:allow-with-interrupts
(or (wait-on-semaphore (mailbox-semaphore mailbox) :timeout timeout)
(return-from receive-message (values nil nil))))
(multiple-value-bind (value ok) (dequeue (mailbox-queue mailbox))
(if ok
(return-from receive-message (values value t))
(go :error))))
:error
(sb-int:bug "Mailbox ~S empty after WAIT-ON-SEMAPHORE."
mailbox)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment