-
Notifications
You must be signed in to change notification settings - Fork 14
/
tstack.lisp
94 lines (69 loc) · 2.46 KB
/
tstack.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;; -*- lisp -*-
;; This file is part of STMX.
;; Copyright (c) 2013-2016 Massimiliano Ghilardi
;;
;; This library is free software: you can redistribute it and/or
;; modify it under the terms of the Lisp Lesser General Public License
;; (http://opensource.franz.com/preamble.html), known as the LLGPL.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty
;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;; See the Lisp Lesser General Public License for more details.
(in-package :stmx.util)
;;;; ** Transactional stack (first-in-last-out) buffer
(transactional
(defclass tstack ()
((top :type list :initarg top :initform nil :accessor top-of))))
(declaim (ftype (function () (values tstack &optional)) tstack))
(defun tstack ()
"Create and return a new TSTACK."
(new 'tstack))
(defmethod empty? ((s tstack))
(null (_ s top)))
(transaction
(defmethod empty! ((s tstack))
(setf (_ s top) nil)
s))
(defmethod full? ((s tstack))
"A tstack is never full, so this method always returns nil."
nil)
(defmethod peek ((s tstack) &optional default)
"Return the first value in tstack S without removing it, and t as multiple values.
Return (values DEFAULT nil) if S contains no values."
(with-ro-slots (top) s
(if (null top)
(values default nil)
(values (first top) t))))
(transaction
(defmethod take ((s tstack))
"Wait until tstack S contains at least one value, then remove
and return the first value."
(with-rw-slots (top) s
(if (null top)
(retry)
(pop top)))))
(transaction
(defmethod put ((s tstack) value)
"Insert VALUE as first element in tstack S and return VALUE.
Since tstack can contain unlimited values, this method never blocks."
(push value (_ s top))
value))
(transaction
(defmethod try-take ((s tstack))
"If tstack S contains at least one value, remove the first value
and return t and the first value as multiple values.
Otherwise return (values nil nil)"
(with-rw-slots (top) s
(if (null top)
(values nil nil)
(values t (pop top))))))
(defmethod try-put ((s tstack) value)
"Append VALUE to tstack S and return (values t VALUE).
Since fifo can contain unlimited values, this method never fails."
(values t (put s value)))
(defprint-object (obj tstack :identity nil)
(with-ro-slots (top) obj
(if top
(format t "~S" (reverse top))
(write-string "()"))))