Home | History | Annotate | Download | only in webm
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LINKEDBLOCKINGQUEUE_H_
     18 #define LINKEDBLOCKINGQUEUE_H_
     19 
     20 #include <utils/List.h>
     21 #include <utils/Mutex.h>
     22 #include <utils/Condition.h>
     23 
     24 namespace android {
     25 
     26 template<typename T>
     27 class LinkedBlockingQueue {
     28     List<T> mList;
     29     Mutex mLock;
     30     Condition mContentAvailableCondition;
     31 
     32     T front(bool remove) {
     33         Mutex::Autolock autolock(mLock);
     34         while (mList.empty()) {
     35             mContentAvailableCondition.wait(mLock);
     36         }
     37         T e = *(mList.begin());
     38         if (remove) {
     39             mList.erase(mList.begin());
     40         }
     41         return e;
     42     }
     43 
     44     DISALLOW_EVIL_CONSTRUCTORS(LinkedBlockingQueue);
     45 
     46 public:
     47     LinkedBlockingQueue() {
     48     }
     49 
     50     ~LinkedBlockingQueue() {
     51     }
     52 
     53     bool empty() {
     54         Mutex::Autolock autolock(mLock);
     55         return mList.empty();
     56     }
     57 
     58     void clear() {
     59         Mutex::Autolock autolock(mLock);
     60         mList.clear();
     61     }
     62 
     63     T peek() {
     64         return front(false);
     65     }
     66 
     67     T take() {
     68         return front(true);
     69     }
     70 
     71     void push(T e) {
     72         Mutex::Autolock autolock(mLock);
     73         mList.push_back(e);
     74         mContentAvailableCondition.signal();
     75     }
     76 };
     77 
     78 } /* namespace android */
     79 #endif /* LINKEDBLOCKINGQUEUE_H_ */
     80