1 /* 2 * Copyright (C) 2016 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 CHRE_EVENT_REF_QUEUE_H 18 #define CHRE_EVENT_REF_QUEUE_H 19 20 #include "chre/core/event.h" 21 #include "chre/util/array_queue.h" 22 23 namespace chre { 24 25 /** 26 * A non-thread-safe, non-blocking wrapper around ArrayQueue that stores Event* 27 * and manages the Event reference counter. 28 * TODO: make this a template specialization? Or rework the ref count design? 29 */ 30 class EventRefQueue { 31 public: 32 ~EventRefQueue(); 33 34 /** 35 * @return true if there are no events in the queue 36 */ empty()37 bool empty() const { 38 return mQueue.empty(); 39 } 40 41 /** 42 * Adds an event to the queue, and increments its reference counter 43 * 44 * @param event The event to add 45 * @return true on success 46 */ 47 bool push(Event *event); 48 49 /** 50 * Removes the oldest event from the queue, and decrements its reference 51 * counter. Does not trigger freeing of the event if the reference count 52 * reaches 0 as a result of this function call. The queue must be non-empty as 53 * a precondition to calling this function, or undefined behavior will result. 54 * 55 * @return Pointer to the next event in the queue 56 */ 57 Event *pop(); 58 59 private: 60 //! The maximum number of events that can be outstanding for an app. 61 static constexpr size_t kMaxPendingEvents = 16; 62 63 //! The queue of incoming events. 64 ArrayQueue<Event *, kMaxPendingEvents> mQueue; 65 }; 66 67 } // namespace chre 68 69 #endif // CHRE_EVENT_REF_QUEUE_H 70