Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #ifndef LIBRARIES_UTILS_POINTER_QUEUE_H_ | |
| 6 #define LIBRARIES_UTILS_POINTER_QUEUE_H_ | |
| 7 | |
| 8 #include <pthread.h> | |
| 9 | |
| 10 #include <list> | |
| 11 | |
| 12 #include "utils/auto_lock.h" | |
| 13 #include "utils/macros.h" | |
| 14 | |
| 15 | |
| 16 // PointerQueue | |
| 17 // | |
| 18 // A simple template to support multithreaded and optionally blocking access | |
| 19 // to a Queue of object pointers. We use object pointer to keep the library | |
| 20 // simple by avoiding issues of template traits. | |
| 21 // | |
| 22 template<class T> class PointerQueue { | |
|
binji
2013/05/23 18:06:49
not crazy about the name PointerQueue. Maybe somet
noelallen1
2013/05/23 22:01:24
Done.
| |
| 23 public: | |
| 24 PointerQueue() { | |
| 25 pthread_mutex_init(&mutex_, NULL); | |
| 26 pthread_cond_init(&cond_, NULL); | |
| 27 } | |
| 28 | |
| 29 ~PointerQueue() { | |
| 30 pthread_mutex_destroy(&mutex_); | |
| 31 pthread_cond_destroy(&cond_); | |
| 32 } | |
| 33 | |
| 34 void Add(T* item) { | |
|
binji
2013/05/23 18:06:49
should probably use standard queue terminology:
Pu
noelallen1
2013/05/23 22:01:24
Done.
| |
| 35 AutoLock lock(&mutex_); | |
| 36 list_.push_back(item); | |
| 37 | |
| 38 pthread_cond_signal(&cond_); | |
| 39 } | |
| 40 | |
| 41 T* Get(bool block) { | |
|
binji
2013/05/23 18:06:49
same here: PopFront or Dequeue
noelallen1
2013/05/23 22:01:24
Done.
| |
| 42 AutoLock lock(&mutex_); | |
| 43 | |
| 44 // If blocking enabled, wait until we get the queue is non-empty | |
| 45 if (block) { | |
| 46 while(list_.empty()) pthread_cond_wait(&cond_, &mutex_); | |
| 47 } | |
| 48 | |
| 49 if (list_.empty()) return NULL; | |
| 50 | |
| 51 T* item = list_.front(); | |
| 52 list_.pop_front(); | |
| 53 return item; | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 std::list<T*> list_; | |
| 58 pthread_cond_t cond_; | |
| 59 pthread_mutex_t mutex_; | |
| 60 DISALLOW_COPY_AND_ASSIGN(PointerQueue); | |
| 61 }; | |
| 62 | |
| 63 #endif // LIBRARIES_UTILS_POINTER_QUEUE_H_ | |
| OLD | NEW |