1343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih/* 2343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Copyright (C) 2014 The Android Open Source Project 3343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * 4343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Licensed under the Apache License, Version 2.0 (the "License"); 5343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * you may not use this file except in compliance with the License. 6343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * You may obtain a copy of the License at 7343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * 8343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * http://www.apache.org/licenses/LICENSE-2.0 9343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * 10343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Unless required by applicable law or agreed to in writing, software 11343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * distributed under the License is distributed on an "AS IS" BASIS, 12343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * See the License for the specific language governing permissions and 14343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * limitations under the License. 15343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih */ 16343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 17343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#ifndef LINKEDBLOCKINGQUEUE_H_ 18343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#define LINKEDBLOCKINGQUEUE_H_ 19343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 20343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/List.h> 21343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/Mutex.h> 22343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <utils/Condition.h> 23343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 24343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihnamespace android { 25343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 26343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihtemplate<typename T> 27343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihclass LinkedBlockingQueue { 28343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih List<T> mList; 29343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih Mutex mLock; 30343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih Condition mContentAvailableCondition; 31343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 32343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih T front(bool remove) { 33343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih Mutex::Autolock autolock(mLock); 34343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih while (mList.empty()) { 35343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mContentAvailableCondition.wait(mLock); 36343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 37343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih T e = *(mList.begin()); 38343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih if (remove) { 39343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mList.erase(mList.begin()); 40343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 41343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return e; 42343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 43343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 44343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih DISALLOW_EVIL_CONSTRUCTORS(LinkedBlockingQueue); 45343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 46343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihpublic: 47343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih LinkedBlockingQueue() { 48343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 49343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 50343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih ~LinkedBlockingQueue() { 51343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 52343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 53343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih bool empty() { 54343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih Mutex::Autolock autolock(mLock); 55343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return mList.empty(); 56343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 57343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 58343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih void clear() { 59343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih Mutex::Autolock autolock(mLock); 60343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mList.clear(); 61343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 62343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 63343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih T peek() { 64343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return front(false); 65343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 66343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 67343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih T take() { 68343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih return front(true); 69343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 70343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 71343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih void push(T e) { 72343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih Mutex::Autolock autolock(mLock); 73343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mList.push_back(e); 74343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih mContentAvailableCondition.signal(); 75343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih } 76343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}; 77343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih 78343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} /* namespace android */ 79343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#endif /* LINKEDBLOCKINGQUEUE_H_ */ 80