Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 SHARED_QUEUE_H | 5 #ifndef SHARED_QUEUE_H |
| 6 #define SHARED_QUEUE_H | 6 #define SHARED_QUEUE_H |
| 7 | 7 |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include <cassert> | 9 #include <cassert> |
| 10 #include <deque> | 10 #include <deque> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 } | 45 } |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 pthread_mutex_t* mutex_; // Weak reference, passed in to constructor. | 48 pthread_mutex_t* mutex_; // Weak reference, passed in to constructor. |
| 49 | 49 |
| 50 // Disable copy and assign. | 50 // Disable copy and assign. |
| 51 ScopedLock& operator=(const ScopedLock&); | 51 ScopedLock& operator=(const ScopedLock&); |
| 52 ScopedLock(const ScopedLock&); | 52 ScopedLock(const ScopedLock&); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 | |
| 56 #ifdef __GNUC__ | |
| 57 #define UNUSED __attribute__ ((unused)) | |
|
binji
2013/09/04 22:22:31
sorry, I meant use __attribute__ without defining
| |
| 58 #else | |
| 59 #define UNUSED | |
| 60 #endif | |
| 61 | |
| 55 // LockingQueue contains a collection of <T>, such as a collection of | 62 // LockingQueue contains a collection of <T>, such as a collection of |
| 56 // objects or pointers. The Push() method is used to add items to the | 63 // objects or pointers. The Push() method is used to add items to the |
| 57 // queue in a thread-safe manner. The GetItem() is used to retrieve | 64 // queue in a thread-safe manner. The GetItem() is used to retrieve |
| 58 // items from the queue in a thread-safe manner. | 65 // items from the queue in a thread-safe manner. |
| 59 template <class T> class LockingQueue { | 66 template <class T> class LockingQueue { |
| 60 public: | 67 public: |
| 61 LockingQueue() : quit_(false) { | 68 LockingQueue() : quit_(false) { |
| 62 int result = pthread_mutex_init(&queue_mutex_, NULL); | 69 int result UNUSED; |
| 70 result = pthread_mutex_init(&queue_mutex_, NULL); | |
| 63 assert(result == 0); | 71 assert(result == 0); |
| 64 result = pthread_cond_init(&queue_condition_var_, NULL); | 72 result = pthread_cond_init(&queue_condition_var_, NULL); |
| 65 assert(result == 0); | 73 assert(result == 0); |
| 66 } | 74 } |
| 67 ~LockingQueue() { pthread_mutex_destroy(&queue_mutex_); } | 75 ~LockingQueue() { pthread_mutex_destroy(&queue_mutex_); } |
| 68 | 76 |
| 69 // The producer (who instantiates the queue) calls this to tell the | 77 // The producer (who instantiates the queue) calls this to tell the |
| 70 // consumer that the queue is no longer being used. | 78 // consumer that the queue is no longer being used. |
| 71 void CancelQueue() { | 79 void CancelQueue() { |
| 72 ScopedLock scoped_mutex(&queue_mutex_); | 80 ScopedLock scoped_mutex(&queue_mutex_); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 std::deque<T> the_queue_; | 146 std::deque<T> the_queue_; |
| 139 bool quit_; | 147 bool quit_; |
| 140 pthread_mutex_t queue_mutex_; | 148 pthread_mutex_t queue_mutex_; |
| 141 pthread_cond_t queue_condition_var_; | 149 pthread_cond_t queue_condition_var_; |
| 142 | 150 |
| 143 // This is used by methods that already have the lock. | 151 // This is used by methods that already have the lock. |
| 144 bool is_empty_no_locking() const { return the_queue_.empty(); } | 152 bool is_empty_no_locking() const { return the_queue_.empty(); } |
| 145 }; | 153 }; |
| 146 | 154 |
| 147 #endif // SHARED_QUEUE_H | 155 #endif // SHARED_QUEUE_H |
| OLD | NEW |