diff options
-rw-r--r-- | src/actors/scala/actors/MessageQueue.scala | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/actors/scala/actors/MessageQueue.scala b/src/actors/scala/actors/MessageQueue.scala new file mode 100644 index 0000000000..4503065d8a --- /dev/null +++ b/src/actors/scala/actors/MessageQueue.scala @@ -0,0 +1,86 @@ + +package scala.actors + +/** + * This class is used by our efficient message queue + * implementation. + * + * @version 0.9.2 + * @author Philipp Haller + */ +class MessageQueueElement { + var msg: Any = _ + var session: Channel[Any] = null + var next: MessageQueueElement = null +} + +/** + * The class <code>MessageQueue</code> provides an efficient + * implementation of a message queue specialized for this actor + * library. Classes in this package are supposed to be the only + * clients of this class. + * + * @version 0.9.2 + * @author Philipp Haller + */ +class MessageQueue { + var first: MessageQueueElement = null + // last == null iff list empty + var last: MessageQueueElement = null + + def append(msg: Any, session: Channel[Any]) = { + if (null eq last) { // list empty + val el = new MessageQueueElement + el.msg = msg + el.session = session + first = el + last = el + } + else { + val el = new MessageQueueElement + el.msg = msg + el.session = session + last.next = el + last = el + } + } + + def extractFirst(p: Any => boolean): MessageQueueElement = { + if (null eq last) null + else { + // test first element + if (p(first.msg)) { + val tmp = first + // remove first element + first = first.next + + // might have to update last + if (tmp eq last) { + last = null + } + + tmp + } + else { + var curr = first + var prev = curr + while(curr.next != null) { + prev = curr + curr = curr.next + if (p(curr.msg)) { + // remove curr + prev.next = curr.next + + // might have to update last + if (curr eq last) { + last = prev + } + + return curr + } + } + null + } + } + } +} |