about summary refs log tree commit diff
path: root/src/libcore/rt/message_queue.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-05-20 21:40:27 -0700
committerbors <bors@rust-lang.org>2013-05-20 21:40:27 -0700
commit7abcc142e5da1b87c59a1510fa87aefc4122bd6d (patch)
treee069fcc7e3da94e50e678fcc723eb59b87123b3c /src/libcore/rt/message_queue.rs
parentadaae45c3e15f95b052648f3511a1097155296b9 (diff)
parenta246e8faf362a1615b5bb4938455dd70642e0f4b (diff)
downloadrust-7abcc142e5da1b87c59a1510fa87aefc4122bd6d.tar.gz
rust-7abcc142e5da1b87c59a1510fa87aefc4122bd6d.zip
auto merge of #6626 : brson/rust/io-upstream, r=graydon
r?

Mostly refactoring, and adding some of the remaining types described in #4419.

The [`Local`](https://github.com/brson/rust/blob/3b4ff41511cfaa5e311b03d16b47bf40c117fa2f/src/libcore/rt/local.rs#L17) trait collects some common, often unsafe patterns around task-local and thread-local values. Making all these types safe is largely the aim of #6210.



Diffstat (limited to 'src/libcore/rt/message_queue.rs')
-rw-r--r--src/libcore/rt/message_queue.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libcore/rt/message_queue.rs b/src/libcore/rt/message_queue.rs
new file mode 100644
index 00000000000..eaab9288ac8
--- /dev/null
+++ b/src/libcore/rt/message_queue.rs
@@ -0,0 +1,53 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use container::Container;
+use kinds::Owned;
+use vec::OwnedVector;
+use cell::Cell;
+use option::*;
+use unstable::sync::{Exclusive, exclusive};
+use clone::Clone;
+
+pub struct MessageQueue<T> {
+    // XXX: Another mystery bug fixed by boxing this lock
+    priv queue: ~Exclusive<~[T]>
+}
+
+impl<T: Owned> MessageQueue<T> {
+    pub fn new() -> MessageQueue<T> {
+        MessageQueue {
+            queue: ~exclusive(~[])
+        }
+    }
+
+    pub fn push(&mut self, value: T) {
+        let value = Cell(value);
+        self.queue.with(|q| q.push(value.take()) );
+    }
+
+    pub fn pop(&mut self) -> Option<T> {
+        do self.queue.with |q| {
+            if !q.is_empty() {
+                Some(q.shift())
+            } else {
+                None
+            }
+        }
+    }
+}
+
+impl<T> Clone for MessageQueue<T> {
+    fn clone(&self) -> MessageQueue<T> {
+        MessageQueue {
+            queue: self.queue.clone()
+        }
+    }
+}