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> |
11 | 11 |
12 #ifdef __GNUC__ | |
binji
2013/09/04 20:21:48
not sure I like adding UNUSED to this header. Does
Sam Clegg
2013/09/04 20:54:43
I prefer the attribute to the #pragma.
(void) doe
| |
13 #define UNUSED __attribute__ ((unused)) | |
14 #else | |
15 #define UNUSED | |
16 #endif | |
17 | |
12 // This file provides a queue that uses a mutex and condition variable so that | 18 // This file provides a queue that uses a mutex and condition variable so that |
13 // one thread can put pointers into the queue and another thread can pull items | 19 // one thread can put pointers into the queue and another thread can pull items |
14 // out of the queue. | 20 // out of the queue. |
15 | 21 |
16 // Specifies whether we want to wait for the queue. | 22 // Specifies whether we want to wait for the queue. |
17 enum QueueWaitingFlag { | 23 enum QueueWaitingFlag { |
18 kWait = 0, | 24 kWait = 0, |
19 kDontWait | 25 kDontWait |
20 }; | 26 }; |
21 | 27 |
(...skipping 23 matching lines...) Expand all Loading... | |
45 } | 51 } |
46 | 52 |
47 private: | 53 private: |
48 pthread_mutex_t* mutex_; // Weak reference, passed in to constructor. | 54 pthread_mutex_t* mutex_; // Weak reference, passed in to constructor. |
49 | 55 |
50 // Disable copy and assign. | 56 // Disable copy and assign. |
51 ScopedLock& operator=(const ScopedLock&); | 57 ScopedLock& operator=(const ScopedLock&); |
52 ScopedLock(const ScopedLock&); | 58 ScopedLock(const ScopedLock&); |
53 }; | 59 }; |
54 | 60 |
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 |