about summary refs log tree commit diff
path: root/src/rt/rust_builtin.cpp
diff options
context:
space:
mode:
authorEric Holk <eric.holk@gmail.com>2012-06-06 15:06:24 -0700
committerEric Holk <eric.holk@gmail.com>2012-06-19 10:31:12 -0700
commitdc718d97a64e8177690cd21a49eee9c28220df2d (patch)
treeb0ecdb9460a45534251629290d3e907df3783f9c /src/rt/rust_builtin.cpp
parent9ee1480fd19a4a98e3c2ce6076f4dc6eff63a620 (diff)
downloadrust-dc718d97a64e8177690cd21a49eee9c28220df2d.tar.gz
rust-dc718d97a64e8177690cd21a49eee9c28220df2d.zip
Adding a lock/condition variable to libcore.
Diffstat (limited to 'src/rt/rust_builtin.cpp')
-rw-r--r--src/rt/rust_builtin.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index 4f2510b1bd3..ece6d97bb49 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -7,6 +7,7 @@
 #include "sync/timer.h"
 #include "rust_abi.h"
 #include "rust_port.h"
+#include "rust_cond_lock.h"
 
 #include <time.h>
 
@@ -861,6 +862,61 @@ rust_task_allow_kill() {
     task->allow_kill();
 }
 
+extern "C" rust_cond_lock*
+rust_create_cond_lock() {
+    return new rust_cond_lock();
+}
+
+extern "C" void
+rust_destroy_cond_lock(rust_cond_lock *lock) {
+    delete lock;
+}
+
+extern "C" void
+rust_lock_cond_lock(rust_cond_lock *lock) {
+    lock->lock.lock();
+}
+
+extern "C" void
+rust_unlock_cond_lock(rust_cond_lock *lock) {
+    lock->lock.unlock();
+}
+
+// The next two functions do not use the built in condition variable features
+// because the Rust schedule is not aware of them, and they can block the
+// scheduler thread.
+
+extern "C" void
+rust_wait_cond_lock(rust_cond_lock *lock) {
+    rust_task *task = rust_get_current_task();
+#ifdef DEBUG_LOCKS
+    assert(lock->lock.lock_held_by_current_thread());
+#endif
+    assert(NULL == lock->waiting);
+    lock->waiting = task;
+    task->block(lock, "waiting for signal");
+    lock->lock.unlock();
+    task->yield(false);
+    lock->lock.lock();
+}
+
+extern "C" bool
+rust_signal_cond_lock(rust_cond_lock *lock) {
+#ifdef DEBUG_LOCKS
+    assert(lock->lock.lock_held_by_current_thread());
+#endif
+    if(NULL == lock->waiting) {
+        return false;
+    }
+    else {
+        lock->waiting->wakeup(lock);
+        lock->waiting = NULL;
+        return true;
+    }
+}
+
+
+
 //
 // Local Variables:
 // mode: C++