-
Notifications
You must be signed in to change notification settings - Fork 5
/
multiple-reader-mailbox.lisp
48 lines (40 loc) · 1.79 KB
/
multiple-reader-mailbox.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
;; multiple-reader-mailbox.lisp
;; --------------------------------------------------------------------------------------
;; Multiple-reader-mailboxes = queues with a lock
;; These mailboxes are intended for use by multiple reader threads. (DM/SD 02/09)
;;
;; Copyright (C) 2008 by SpectroDynamics, LLC. All rights reserved.
;;
;; DM/SD 08/08
;; --------------------------------------------------------------------------------------
;; --------------------------------------------------------------------------------------
(in-package :multiple-reader-mailbox)
;; --------------------------------------------------------------------------------------
(defstruct (mailbox
(:include queue:queue))
(lock (mpcompat:make-lock) :read-only t)
(condv (mpcompat:make-condition-variable) :read-only t))
(defun create ()
(make-mailbox))
(defun send (msg mbox)
(with-accessors ((lock mailbox-lock)
(condv mailbox-condv)) mbox
(mpcompat:with-spinlock (lock)
(queue:add msg mbox)
(mpcompat:condition-variable-signal condv)) ))
(defun receive (mbox &optional timeout (timeout-error-p t) timeout-value)
(with-accessors ((lock mailbox-lock)
(condv mailbox-condv)) mbox
(mpcompat:with-spinlock (lock)
(if (or (not-empty mbox)
(and (or (null timeout)
(plusp timeout))
(mpcompat:condition-variable-wait condv lock
:wait-reason "Waiting for mail"
:timeout timeout)))
(queue:pop mbox)
;; else
(if timeout-error-p
(error "Timed out waiting for mail")
;; else
(values timeout-value nil)) )) ))