Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / native_client_sdk / src / libraries / sdk_util / thread_safe_queue.h
blob37842dba86315914cdf2292b9a59ea738080c9fa
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_
6 #define LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_
8 #include <pthread.h>
10 #include <list>
12 #include "sdk_util/auto_lock.h"
13 #include "sdk_util/macros.h"
15 namespace sdk_util {
17 // ThreadSafeQueue
19 // A simple template to support multithreaded and optionally blocking access
20 // to a Queue of object pointers.
22 template<class T> class ThreadSafeQueue {
23 public:
24 ThreadSafeQueue() {
25 pthread_cond_init(&cond_, NULL);
28 ~ThreadSafeQueue() {
29 pthread_cond_destroy(&cond_);
32 void Enqueue(T* item) {
33 AUTO_LOCK(lock_);
34 list_.push_back(item);
36 pthread_cond_signal(&cond_);
39 T* Dequeue(bool block) {
40 AUTO_LOCK(lock_);
42 // If blocking enabled, wait until we queue is non-empty
43 if (block) {
44 while (list_.empty()) pthread_cond_wait(&cond_, lock_.mutex());
47 if (list_.empty()) return NULL;
49 T* item = list_.front();
50 list_.pop_front();
51 return item;
54 private:
55 std::list<T*> list_;
56 pthread_cond_t cond_;
57 SimpleLock lock_;
58 DISALLOW_COPY_AND_ASSIGN(ThreadSafeQueue);
61 } // namespace sdk_util
63 #endif // LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_