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 */ 37 bool empty() const; 38 39 /** 40 * Adds an event to the queue, and increments its reference counter 41 * 42 * @param event The event to add 43 * @return true on success 44 */ 45 bool push(Event *event); 46 47 /** 48 * Removes the oldest event from the queue, and decrements its reference 49 * counter. Does not trigger freeing of the event if the reference count 50 * reaches 0 as a result of this function call. The queue must be non-empty as 51 * a precondition to calling this function, or undefined behavior will result. 52 * 53 * @return Pointer to the next event in the queue 54 */ 55 Event *pop(); 56 57 private: 58 //! The maximum number of events that can be outstanding for an app. 59 static constexpr size_t kMaxPendingEvents = 16; 60 61 //! The queue of incoming events. 62 ArrayQueue<Event *, kMaxPendingEvents> mQueue; 63 }; 64 65 } // namespace chre 66 67 #endif // CHRE_EVENT_REF_QUEUE_H 68