Chromium Code Reviews| Index: native_client_sdk/src/libraries/utils/pointer_queue.h |
| diff --git a/native_client_sdk/src/libraries/utils/pointer_queue.h b/native_client_sdk/src/libraries/utils/pointer_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d59f4d2c5c8e3388d04b8ac9913198be1e3855ea |
| --- /dev/null |
| +++ b/native_client_sdk/src/libraries/utils/pointer_queue.h |
| @@ -0,0 +1,63 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef LIBRARIES_UTILS_POINTER_QUEUE_H_ |
| +#define LIBRARIES_UTILS_POINTER_QUEUE_H_ |
| + |
| +#include <pthread.h> |
| + |
| +#include <list> |
| + |
| +#include "utils/auto_lock.h" |
| +#include "utils/macros.h" |
| + |
| + |
| +// PointerQueue |
| +// |
| +// A simple template to support multithreaded and optionally blocking access |
| +// to a Queue of object pointers. We use object pointer to keep the library |
| +// simple by avoiding issues of template traits. |
| +// |
| +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.
|
| + public: |
| + PointerQueue() { |
| + pthread_mutex_init(&mutex_, NULL); |
| + pthread_cond_init(&cond_, NULL); |
| + } |
| + |
| + ~PointerQueue() { |
| + pthread_mutex_destroy(&mutex_); |
| + pthread_cond_destroy(&cond_); |
| + } |
| + |
| + 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.
|
| + AutoLock lock(&mutex_); |
| + list_.push_back(item); |
| + |
| + pthread_cond_signal(&cond_); |
| + } |
| + |
| + T* Get(bool block) { |
|
binji
2013/05/23 18:06:49
same here: PopFront or Dequeue
noelallen1
2013/05/23 22:01:24
Done.
|
| + AutoLock lock(&mutex_); |
| + |
| + // If blocking enabled, wait until we get the queue is non-empty |
| + if (block) { |
| + while(list_.empty()) pthread_cond_wait(&cond_, &mutex_); |
| + } |
| + |
| + if (list_.empty()) return NULL; |
| + |
| + T* item = list_.front(); |
| + list_.pop_front(); |
| + return item; |
| + } |
| + |
| + private: |
| + std::list<T*> list_; |
| + pthread_cond_t cond_; |
| + pthread_mutex_t mutex_; |
| + DISALLOW_COPY_AND_ASSIGN(PointerQueue); |
| +}; |
| + |
| +#endif // LIBRARIES_UTILS_POINTER_QUEUE_H_ |