|
|
@ -2,13 +2,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
(in-package :cl-channel)
|
|
|
|
(in-package :cl-channel)
|
|
|
|
|
|
|
|
|
|
|
|
(flan-utils:enable-all-readers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defstruct %queue
|
|
|
|
(defstruct %queue
|
|
|
|
internal
|
|
|
|
internal
|
|
|
|
mutex)
|
|
|
|
mutex)
|
|
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
|
|
|
|
|
|
(mapc 'export (list
|
|
|
|
(defun make-queue (&optional (from nil))
|
|
|
|
(defun make-queue (&optional (from nil))
|
|
|
|
(let ((q (make-%queue)))
|
|
|
|
(let ((q (make-%queue)))
|
|
|
|
(setf (%queue-internal q) from)
|
|
|
|
(setf (%queue-internal q) from)
|
|
|
@ -31,7 +31,7 @@
|
|
|
|
(defun queue-clear (q)
|
|
|
|
(defun queue-clear (q)
|
|
|
|
(bt:with-lock-held ((%queue-mutex q))
|
|
|
|
(bt:with-lock-held ((%queue-mutex q))
|
|
|
|
(setf (%queue-internal q) nil)))
|
|
|
|
(setf (%queue-internal q) nil)))
|
|
|
|
]
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
(defstruct %channel
|
|
|
|
(defstruct %channel
|
|
|
|
internal
|
|
|
|
internal
|
|
|
@ -49,7 +49,8 @@
|
|
|
|
(defun sigall (sem)
|
|
|
|
(defun sigall (sem)
|
|
|
|
(bt-sem:signal-semaphore sem (bt-sem:semaphore-waiters sem)))
|
|
|
|
(bt-sem:signal-semaphore sem (bt-sem:semaphore-waiters sem)))
|
|
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
(mapc 'export (list
|
|
|
|
|
|
|
|
|
|
|
|
(defun make-channel (&optional (max 0))
|
|
|
|
(defun make-channel (&optional (max 0))
|
|
|
|
(let ((c (make-%channel)))
|
|
|
|
(let ((c (make-%channel)))
|
|
|
|
(setf (%channel-internal c) (make-queue))
|
|
|
|
(setf (%channel-internal c) (make-queue))
|
|
|
@ -71,7 +72,7 @@
|
|
|
|
(defun <- (chan)
|
|
|
|
(defun <- (chan)
|
|
|
|
(let ((out nil)
|
|
|
|
(let ((out nil)
|
|
|
|
(rout nil))
|
|
|
|
(rout nil))
|
|
|
|
(loop while (and (null out) ¬(closed chan)) do
|
|
|
|
(loop while (and (null out) (not (closed chan))) do
|
|
|
|
(progn
|
|
|
|
(progn
|
|
|
|
(if (> (poll chan) 0)
|
|
|
|
(if (> (poll chan) 0)
|
|
|
|
(%atomic chan
|
|
|
|
(%atomic chan
|
|
|
@ -83,15 +84,15 @@
|
|
|
|
(if (closed chan) (values nil nil) (values rout t))))
|
|
|
|
(if (closed chan) (values nil nil) (values rout t))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun -> (chan item)
|
|
|
|
(defun -> (chan item)
|
|
|
|
(loop while (and ¬(closed chan) (> (%channel-max chan) 0) (%atomic chan (>= (queue-poll (%channel-internal chan)) (%channel-max chan))))
|
|
|
|
(loop while (and (not (closed chan)) (> (%channel-max chan) 0) (%atomic chan (>= (queue-poll (%channel-internal chan)) (%channel-max chan))))
|
|
|
|
do (bt-sem:wait-on-semaphore (%channel-rel-send)))
|
|
|
|
do (bt-sem:wait-on-semaphore (%channel-rel-send)))
|
|
|
|
(let ((lv (%atomic chan
|
|
|
|
(let ((lv (%atomic chan
|
|
|
|
(if ¬(if (or (%channel-closed chan) (and (> (%channel-max chan) 0) (>= (queue-poll (%channel-internal chan)) (%channel-max chan))))
|
|
|
|
(if (not (if (or (%channel-closed chan) (and (> (%channel-max chan) 0) (>= (queue-poll (%channel-internal chan)) (%channel-max chan))))
|
|
|
|
nil
|
|
|
|
nil
|
|
|
|
(progn
|
|
|
|
(progn
|
|
|
|
(queue-> (%channel-internal chan) item)
|
|
|
|
(queue-> (%channel-internal chan) item)
|
|
|
|
(bt-sem:signal-semaphore (%channel-rel-recv chan) 1)
|
|
|
|
(bt-sem:signal-semaphore (%channel-rel-recv chan) 1)
|
|
|
|
t))
|
|
|
|
t)))
|
|
|
|
(if (%channel-closed chan) nil 'reset)
|
|
|
|
(if (%channel-closed chan) nil 'reset)
|
|
|
|
t))))
|
|
|
|
t))))
|
|
|
|
(if (eq lv 'reset)
|
|
|
|
(if (eq lv 'reset)
|
|
|
@ -110,9 +111,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro make ()
|
|
|
|
(defmacro make ()
|
|
|
|
`(make-channel))
|
|
|
|
`(make-channel))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun test ()
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#|(defun test ()
|
|
|
|
(let ((chan (make-channel)))
|
|
|
|
(let ((chan (make-channel)))
|
|
|
|
$(progn
|
|
|
|
$(progn
|
|
|
|
(loop while ¬(closed chan) do (let ((val (<- chan)))
|
|
|
|
(loop while ¬(closed chan) do (let ((val (<- chan)))
|
|
|
@ -122,5 +124,9 @@
|
|
|
|
(release chan))))
|
|
|
|
(release chan))))
|
|
|
|
(print "Thread end")
|
|
|
|
(print "Thread end")
|
|
|
|
(print "."))
|
|
|
|
(print "."))
|
|
|
|
|
|
|
|
$(loop while ¬(closed chan) do
|
|
|
|
|
|
|
|
(progn
|
|
|
|
|
|
|
|
(sleep 2)
|
|
|
|
|
|
|
|
(-> chan 'teste)))
|
|
|
|
(loop while ¬(closed chan) do (-> chan (write-to-string (read)))))
|
|
|
|
(loop while ¬(closed chan) do (-> chan (write-to-string (read)))))
|
|
|
|
(print "End"))
|
|
|
|
(print "End"))|#
|
|
|
|