1 | /* |
---|
2 | * Mailbox.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004-2007 Peter Graves, Andras Simon |
---|
5 | * $Id: Mailbox.java 11488 2008-12-27 10:50:33Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import java.util.LinkedList; |
---|
37 | import java.util.NoSuchElementException; |
---|
38 | |
---|
39 | public final class Mailbox extends LispObject |
---|
40 | { |
---|
41 | private LinkedList<LispObject> box = new LinkedList<LispObject>(); |
---|
42 | |
---|
43 | @Override |
---|
44 | public LispObject typeOf() |
---|
45 | { |
---|
46 | return Symbol.MAILBOX; |
---|
47 | } |
---|
48 | |
---|
49 | @Override |
---|
50 | public LispObject classOf() |
---|
51 | { |
---|
52 | return BuiltInClass.MAILBOX; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public LispObject typep(LispObject typeSpecifier) throws ConditionThrowable |
---|
57 | { |
---|
58 | if (typeSpecifier == Symbol.MAILBOX) |
---|
59 | return T; |
---|
60 | if (typeSpecifier == BuiltInClass.MAILBOX) |
---|
61 | return T; |
---|
62 | return super.typep(typeSpecifier); |
---|
63 | } |
---|
64 | |
---|
65 | private void send(LispObject o) |
---|
66 | { |
---|
67 | synchronized(this) |
---|
68 | { |
---|
69 | box.add(o); |
---|
70 | notify(); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | private LispObject read() |
---|
75 | { |
---|
76 | synchronized(this) |
---|
77 | { |
---|
78 | while (box.isEmpty()) |
---|
79 | { |
---|
80 | try |
---|
81 | { |
---|
82 | wait(); |
---|
83 | } |
---|
84 | catch(InterruptedException e) |
---|
85 | { |
---|
86 | throw new RuntimeException(e); |
---|
87 | } |
---|
88 | } |
---|
89 | return (LispObject) box.removeFirst(); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | private LispObject peek() |
---|
94 | { |
---|
95 | synchronized(this) |
---|
96 | { |
---|
97 | try |
---|
98 | { |
---|
99 | return (LispObject) box.getFirst(); |
---|
100 | } |
---|
101 | catch(NoSuchElementException e) |
---|
102 | { |
---|
103 | return NIL; |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | private LispObject empty() |
---|
109 | { |
---|
110 | return box.isEmpty() ? T : NIL; |
---|
111 | } |
---|
112 | |
---|
113 | @Override |
---|
114 | public String writeToString() throws ConditionThrowable |
---|
115 | { |
---|
116 | return unreadableString(Symbol.MAILBOX); |
---|
117 | } |
---|
118 | |
---|
119 | // ### make-mailbox |
---|
120 | private static final Primitive MAKE_MAILBOX = |
---|
121 | new Primitive("make-mailbox", PACKAGE_EXT, true, "") |
---|
122 | { |
---|
123 | @Override |
---|
124 | public LispObject execute() throws ConditionThrowable |
---|
125 | { |
---|
126 | return new Mailbox(); |
---|
127 | } |
---|
128 | }; |
---|
129 | |
---|
130 | // ### mailbox-send mailbox object |
---|
131 | private static final Primitive MAILBOX_SEND = |
---|
132 | new Primitive("mailbox-send", PACKAGE_EXT, true, "mailbox object") |
---|
133 | { |
---|
134 | @Override |
---|
135 | public LispObject execute(LispObject first, LispObject second) |
---|
136 | throws ConditionThrowable |
---|
137 | { |
---|
138 | if (first instanceof Mailbox) |
---|
139 | { |
---|
140 | Mailbox mbox = (Mailbox) first; |
---|
141 | mbox.send(second); |
---|
142 | return T; |
---|
143 | } |
---|
144 | else |
---|
145 | return type_error(first, Symbol.MAILBOX); |
---|
146 | } |
---|
147 | }; |
---|
148 | |
---|
149 | // ### mailbox-read mailbox |
---|
150 | private static final Primitive MAILBOX_READ = |
---|
151 | new Primitive("mailbox-read", PACKAGE_EXT, true, "mailbox") |
---|
152 | { |
---|
153 | @Override |
---|
154 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
155 | { |
---|
156 | if (arg instanceof Mailbox) |
---|
157 | { |
---|
158 | Mailbox mbox = (Mailbox) arg; |
---|
159 | return mbox.read(); |
---|
160 | } |
---|
161 | else |
---|
162 | return type_error(arg, Symbol.MAILBOX); |
---|
163 | } |
---|
164 | }; |
---|
165 | |
---|
166 | // ### mailbox-peek mailbox |
---|
167 | private static final Primitive MAILBOX_PEEK = |
---|
168 | new Primitive("mailbox-peek", PACKAGE_EXT, true, "mailbox") |
---|
169 | { |
---|
170 | @Override |
---|
171 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
172 | { |
---|
173 | if (arg instanceof Mailbox) |
---|
174 | { |
---|
175 | Mailbox mbox = (Mailbox) arg; |
---|
176 | return mbox.peek(); |
---|
177 | } |
---|
178 | else |
---|
179 | return type_error(arg, Symbol.MAILBOX); |
---|
180 | } |
---|
181 | }; |
---|
182 | |
---|
183 | // ### mailbox-empty-p mailbox |
---|
184 | private static final Primitive MAILBOX_EMPTY_P = |
---|
185 | new Primitive("mailbox-empty-p", PACKAGE_EXT, true, "mailbox") |
---|
186 | { |
---|
187 | @Override |
---|
188 | public LispObject execute(LispObject arg) throws ConditionThrowable |
---|
189 | { |
---|
190 | if (arg instanceof Mailbox) |
---|
191 | { |
---|
192 | Mailbox mbox = (Mailbox) arg; |
---|
193 | return mbox.empty(); |
---|
194 | } |
---|
195 | else |
---|
196 | return type_error(arg, Symbol.MAILBOX); |
---|
197 | } |
---|
198 | }; |
---|
199 | } |
---|