2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "pb_decode.h"
19 #include <hardware/ril/librilutils/proto/sap-api.pb.h>
20 #include <utils/Log.h>
25 * Template queue class to handling requests for a rild socket.
27 * This class performs the following functions :
31 * <li>Check and dequeue.
39 * Mutex attribute used in queue mutex initialization.
41 pthread_mutexattr_t attr;
44 * Queue mutex variable for synchronized queue access.
46 pthread_mutex_t mutex_instance;
49 * Condition to be waited on for dequeuing.
61 * Remove the first element of the queue.
63 * @return first element of the queue.
68 * Add a request to the front of the queue.
70 * @param Request to be added.
72 void enqueue(T* request);
75 * Check if the queue is empty.
80 * Check and remove an element with a particular message id and token.
82 * @param Request message id.
83 * @param Request token.
85 int checkAndDequeue( MsgId id, int token);
94 Ril_queue<T>::Ril_queue(void) {
95 pthread_mutexattr_init(&attr);
96 pthread_mutex_init(&mutex_instance, &attr);
97 cond = PTHREAD_COND_INITIALIZER;
101 template <typename T>
102 T* Ril_queue<T>::dequeue(void) {
105 pthread_mutex_lock(&mutex_instance);
107 pthread_cond_wait(&cond, &mutex_instance);
110 if(NULL != this->front->p_next) {
111 this->front = this->front->p_next;
115 pthread_mutex_unlock(&mutex_instance);
120 template <typename T>
121 void Ril_queue<T>::enqueue(T* request) {
123 pthread_mutex_lock(&mutex_instance);
125 if(NULL == this->front) {
126 this->front = request;
127 request->p_next = NULL;
129 request->p_next = this->front;
130 this->front = request;
132 pthread_cond_broadcast(&cond);
133 pthread_mutex_unlock(&mutex_instance);
136 template <typename T>
137 int Ril_queue<T>::checkAndDequeue(MsgId id, int token) {
141 pthread_mutex_lock(&mutex_instance);
143 for(T **ppCur = &(this->front); *ppCur != NULL; ppCur = &((*ppCur)->p_next)) {
144 if (token == (*ppCur)->token && id == (*ppCur)->curr->id) {
147 *ppCur = (*ppCur)->p_next;
153 pthread_mutex_unlock(&mutex_instance);
159 template <typename T>
160 int Ril_queue<T>::empty(void) {
162 if(this->front == NULL) {