| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_ | 5 #ifndef LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_ |
| 6 #define LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_ | 6 #define LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_ |
| 7 | 7 |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 | 11 |
| 12 #include "sdk_util/auto_lock.h" | 12 #include "sdk_util/auto_lock.h" |
| 13 #include "sdk_util/macros.h" | 13 #include "sdk_util/macros.h" |
| 14 | 14 |
| 15 | 15 |
| 16 // ThreadSafeQueue | 16 // ThreadSafeQueue |
| 17 // | 17 // |
| 18 // A simple template to support multithreaded and optionally blocking access | 18 // A simple template to support multithreaded and optionally blocking access |
| 19 // to a Queue of object pointers. | 19 // to a Queue of object pointers. |
| 20 // | 20 // |
| 21 template<class T> class ThreadSafeQueue { | 21 template<class T> class ThreadSafeQueue { |
| 22 public: | 22 public: |
| 23 ThreadSafeQueue() { | 23 ThreadSafeQueue() { |
| 24 pthread_mutex_init(&mutex_, NULL); | |
| 25 pthread_cond_init(&cond_, NULL); | 24 pthread_cond_init(&cond_, NULL); |
| 26 } | 25 } |
| 27 | 26 |
| 28 ~ThreadSafeQueue() { | 27 ~ThreadSafeQueue() { |
| 29 pthread_mutex_destroy(&mutex_); | |
| 30 pthread_cond_destroy(&cond_); | 28 pthread_cond_destroy(&cond_); |
| 31 } | 29 } |
| 32 | 30 |
| 33 void Enqueue(T* item) { | 31 void Enqueue(T* item) { |
| 34 AutoLock lock(&mutex_); | 32 AUTO_LOCK(lock_); |
| 35 list_.push_back(item); | 33 list_.push_back(item); |
| 36 | 34 |
| 37 pthread_cond_signal(&cond_); | 35 pthread_cond_signal(&cond_); |
| 38 } | 36 } |
| 39 | 37 |
| 40 T* Dequeue(bool block) { | 38 T* Dequeue(bool block) { |
| 41 AutoLock lock(&mutex_); | 39 AUTO_LOCK(lock_); |
| 42 | 40 |
| 43 // If blocking enabled, wait until we queue is non-empty | 41 // If blocking enabled, wait until we queue is non-empty |
| 44 if (block) { | 42 if (block) { |
| 45 while (list_.empty()) pthread_cond_wait(&cond_, &mutex_); | 43 while (list_.empty()) pthread_cond_wait(&cond_, lock_.mutex()); |
| 46 } | 44 } |
| 47 | 45 |
| 48 if (list_.empty()) return NULL; | 46 if (list_.empty()) return NULL; |
| 49 | 47 |
| 50 T* item = list_.front(); | 48 T* item = list_.front(); |
| 51 list_.pop_front(); | 49 list_.pop_front(); |
| 52 return item; | 50 return item; |
| 53 } | 51 } |
| 54 | 52 |
| 55 private: | 53 private: |
| 56 std::list<T*> list_; | 54 std::list<T*> list_; |
| 57 pthread_cond_t cond_; | 55 pthread_cond_t cond_; |
| 58 pthread_mutex_t mutex_; | 56 SimpleLock lock_; |
| 59 DISALLOW_COPY_AND_ASSIGN(ThreadSafeQueue); | 57 DISALLOW_COPY_AND_ASSIGN(ThreadSafeQueue); |
| 60 }; | 58 }; |
| 61 | 59 |
| 62 #endif // LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_ | 60 #endif // LIBRARIES_SDK_UTIL_THREAD_SAFE_QUEUE_H_ |
| 63 | 61 |
| OLD | NEW |