about summary refs log tree commit diff
path: root/src/rt/sync/condition_variable.cpp
diff options
context:
space:
mode:
authorMichael Bebenita <mbebenita@mozilla.com>2010-07-19 14:05:18 -0700
committerMichael Bebenita <mbebenita@mozilla.com>2010-07-19 14:05:18 -0700
commit00d1465d13980fc3acf650f182ee0723fbda0e06 (patch)
treea73cf5f0f20c0bee6722b33d975eb930919fefdf /src/rt/sync/condition_variable.cpp
parent1f0656d9084970fcc02ba9c27277265b8b3b7217 (diff)
Added a message passing system based on lock free queues for inter-thread communication. Channels now buffer on the sending side, and no longer require blocking when sending. Lots of other refactoring and bug fixes.
Diffstat (limited to 'src/rt/sync/condition_variable.cpp')
-rw-r--r--src/rt/sync/condition_variable.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/rt/sync/condition_variable.cpp b/src/rt/sync/condition_variable.cpp
new file mode 100644
index 00000000000..d403257243d
--- /dev/null
+++ b/src/rt/sync/condition_variable.cpp
@@ -0,0 +1,66 @@
+#include "../globals.h"
+
+/*
+ * Conditional variable. Implemented using pthreads condition variables, and
+ * using events on windows.
+ */
+
+#include "condition_variable.h"
+
+// #define TRACE
+
+condition_variable::condition_variable() {
+#if defined(__WIN32__)
+    _event = CreateEvent(NULL, FALSE, FALSE, NULL);
+#else
+    pthread_cond_init(&_cond, NULL);
+    pthread_mutex_init(&_mutex, NULL);
+#endif
+}
+
+condition_variable::~condition_variable() {
+#if defined(__WIN32__)
+    CloseHandle(_event);
+#else
+    pthread_cond_destroy(&_cond);
+    pthread_mutex_destroy(&_mutex);
+#endif
+}
+
+/**
+ * Wait indefinitely until condition is signaled.
+ */
+void condition_variable::wait() {
+#ifdef TRACE
+    printf("waiting on condition_variable: 0x%" PRIxPTR "\n",
+           (uintptr_t)this);
+#endif
+#if defined(__WIN32__)
+    WaitForSingleObject(_event, INFINITE);
+#else
+    pthread_mutex_lock(&_mutex);
+    pthread_cond_wait(&_cond, &_mutex);
+    pthread_mutex_unlock(&_mutex);
+#endif
+#ifdef TRACE
+    printf("resumed on condition_variable: 0x%" PRIxPTR "\n",
+           (uintptr_t)this);
+#endif
+}
+
+/**
+ * Signal condition, and resume the waiting thread.
+ */
+void condition_variable::signal() {
+#if defined(__WIN32__)
+    SetEvent(_event);
+#else
+    pthread_mutex_lock(&_mutex);
+    pthread_cond_signal(&_cond);
+    pthread_mutex_unlock(&_mutex);
+#endif
+#ifdef TRACE
+    printf("signal  condition_variable: 0x%" PRIxPTR "\n",
+           (uintptr_t)this);
+#endif
+}