Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1322)

Unified Diff: native_client_sdk/src/libraries/utils/thread_safe_queue.h

Issue 15011003: ppapi_simple (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Copyright Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « native_client_sdk/src/libraries/ppapi_simple/ps_main.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: native_client_sdk/src/libraries/utils/thread_safe_queue.h
diff --git a/native_client_sdk/src/libraries/utils/thread_safe_queue.h b/native_client_sdk/src/libraries/utils/thread_safe_queue.h
new file mode 100644
index 0000000000000000000000000000000000000000..d35d504350fc11cb5b1c5eab6a2f339cca6ed285
--- /dev/null
+++ b/native_client_sdk/src/libraries/utils/thread_safe_queue.h
@@ -0,0 +1,62 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef LIBRARIES_UTILS_THREAD_SAFE_QUEUE_H_
+#define LIBRARIES_UTILS_THREAD_SAFE_QUEUE_H_
+
+#include <pthread.h>
+
+#include <list>
+
+#include "utils/auto_lock.h"
+#include "utils/macros.h"
+
+
+// ThreadSafeQueue
+//
+// A simple template to support multithreaded and optionally blocking access
+// to a Queue of object pointers.
+//
+template<class T> class ThreadSafeQueue {
+ public:
+ ThreadSafeQueue() {
+ pthread_mutex_init(&mutex_, NULL);
+ pthread_cond_init(&cond_, NULL);
+ }
+
+ ~ThreadSafeQueue() {
+ pthread_mutex_destroy(&mutex_);
+ pthread_cond_destroy(&cond_);
+ }
+
+ void Enqueue(T* item) {
+ AutoLock lock(&mutex_);
+ list_.push_back(item);
+
+ pthread_cond_signal(&cond_);
+ }
+
+ T* Dequeue(bool block) {
+ AutoLock lock(&mutex_);
+
+ // If blocking enabled, wait until we queue is non-empty
+ if (block) {
+ while (list_.empty()) pthread_cond_wait(&cond_, &mutex_);
+ }
+
+ if (list_.empty()) return NULL;
+
+ T* item = list_.front();
+ list_.pop_front();
+ return item;
+ }
+
+ private:
+ std::list<T*> list_;
+ pthread_cond_t cond_;
+ pthread_mutex_t mutex_;
+ DISALLOW_COPY_AND_ASSIGN(ThreadSafeQueue);
+};
+
+#endif // LIBRARIES_UTILS_THREAD_SAFE_QUEUE_H_
« no previous file with comments | « native_client_sdk/src/libraries/ppapi_simple/ps_main.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698