about summary refs log tree commit diff
path: root/src/rt/sync/sync.cpp
diff options
context:
space:
mode:
authorMichael Bebenita <mbebenita@mozilla.com>2010-09-07 18:05:42 -0700
committerMichael Bebenita <mbebenita@mozilla.com>2010-09-07 18:41:07 -0700
commit9b74129a4f2d3edd4502dd263b54535aa67780a0 (patch)
treedaa65eaa3a178bd27e5a97d41be0b81774f7405d /src/rt/sync/sync.cpp
parent9b8d647af3cf743e22468de44c49bebf4270c8a6 (diff)
downloadrust-9b74129a4f2d3edd4502dd263b54535aa67780a0.tar.gz
rust-9b74129a4f2d3edd4502dd263b54535aa67780a0.zip
Added a thread utility class to factor out operations on threads.
Diffstat (limited to 'src/rt/sync/sync.cpp')
-rw-r--r--src/rt/sync/sync.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/rt/sync/sync.cpp b/src/rt/sync/sync.cpp
index 09a6f291c70..70e5077cf98 100644
--- a/src/rt/sync/sync.cpp
+++ b/src/rt/sync/sync.cpp
@@ -10,3 +10,46 @@ void sync::yield() {
     pthread_yield();
 #endif
 }
+
+#if defined(__WIN32__)
+static DWORD WINAPI
+#elif defined(__GNUC__)
+static void *
+#else
+#error "Platform not supported"
+#endif
+rust_thread_start(void *ptr) {
+    rust_thread *thread = (rust_thread *) ptr;
+    thread->run();
+    thread->thread = 0;
+    return 0;
+}
+
+void
+rust_thread::start() {
+#if defined(__WIN32__)
+   thread = CreateThread(NULL, 0, rust_thread_start, this, 0, NULL);
+#else
+   pthread_attr_t attr;
+   pthread_attr_init(&attr);
+   pthread_attr_setstacksize(&attr, 1024 * 1024);
+   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+   pthread_create(&thread, &attr, rust_thread_start, (void *) this);
+#endif
+}
+
+void
+rust_thread::join() {
+#if defined(__WIN32__)
+   WaitForSingleObject(thread, INFINITE);
+#else
+   pthread_join(thread, NULL);
+#endif
+   thread = 0;
+}
+
+bool
+rust_thread::is_running() {
+    // TODO: This may be broken because of possible races.
+    return thread;
+}