1 /*
2 Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7 		* Redistributions of source code must retain the above copyright
8 			notice, this list of conditions and the following disclaimer.
9 		* Redistributions in binary form must reproduce the above
10 			copyright notice, this list of conditions and the following
11 			disclaimer in the documentation and/or other materials provided
12 			with the distribution.
13 		* Neither the name of The Linux Foundation nor the names of its
14 			contributors may be used to endorse or promote products derived
15 			from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*!
30 	@file
31 	IPACM_CmdQueue.cpp
32 
33 	@brief
34 	This file implements the IPAM Comment Queue functionality
35 
36 	@Author
37    Sunil
38 
39 */
40 #include <string.h>
41 #include "IPACM_CmdQueue.h"
42 #include "IPACM_Log.h"
43 #include "IPACM_Iface.h"
44 
45 pthread_mutex_t mutex    = PTHREAD_MUTEX_INITIALIZER;
46 pthread_cond_t  cond_var = PTHREAD_COND_INITIALIZER;
47 
48 MessageQueue* MessageQueue::inst_internal = NULL;
49 MessageQueue* MessageQueue::inst_external = NULL;
50 
getInstanceInternal()51 MessageQueue* MessageQueue::getInstanceInternal()
52 {
53 	if(inst_internal == NULL)
54 	{
55 		inst_internal = new MessageQueue();
56 		if(inst_internal == NULL)
57 		{
58 			IPACMERR("unable to create internal Message Queue instance\n");
59 			return NULL;
60 		}
61 	}
62 
63 	return inst_internal;
64 }
65 
getInstanceExternal()66 MessageQueue* MessageQueue::getInstanceExternal()
67 {
68 	if(inst_external == NULL)
69 	{
70 		inst_external = new MessageQueue();
71 		if(inst_external == NULL)
72 		{
73 			IPACMERR("unable to create external Message Queue instance\n");
74 			return NULL;
75 		}
76 	}
77 
78 	return inst_external;
79 }
80 
enqueue(Message * item)81 void MessageQueue::enqueue(Message *item)
82 {
83 	if(!Head)
84 	{
85 		Tail = item;
86 		Head = item;
87 	}
88 	else
89 	{
90 		if(Tail == NULL)
91 		{
92 			IPACMDBG("Tail is null\n");
93 			Head->setnext(item);
94 		}
95 		else
96 		{
97 			Tail->setnext(item);
98 		}
99 		Tail = item;
100 	}
101 }
102 
103 
dequeue(void)104 Message* MessageQueue::dequeue(void)
105 {
106 	if(Head == NULL)
107 	{
108 		return NULL;
109 	}
110 	else
111 	{
112 		Message *tmp = Head;
113 		Head = Head->getnext();
114 
115 		return tmp;
116 	}
117 }
118 
119 
Process(void * param)120 void* MessageQueue::Process(void *param)
121 {
122 	MessageQueue *MsgQueueInternal = NULL;
123 	MessageQueue *MsgQueueExternal = NULL;
124 	Message *item = NULL;
125 	param = NULL;
126 	const char *eventName = NULL;
127 
128 	IPACMDBG("MessageQueue::Process()\n");
129 
130 	MsgQueueInternal = MessageQueue::getInstanceInternal();
131 	if(MsgQueueInternal == NULL)
132 	{
133 		IPACMERR("unable to start internal cmd queue process\n");
134 		return NULL;
135 	}
136 
137 	MsgQueueExternal = MessageQueue::getInstanceExternal();
138 	if(MsgQueueExternal == NULL)
139 	{
140 		IPACMERR("unable to start external cmd queue process\n");
141 		return NULL;
142 	}
143 
144 	while(1)
145 	{
146 		if(pthread_mutex_lock(&mutex) != 0)
147 		{
148 			IPACMERR("unable to lock the mutex\n");
149 			return NULL;
150 		}
151 
152 		item = MsgQueueInternal->dequeue();
153 		if(item == NULL)
154 		{
155 			item = MsgQueueExternal->dequeue();
156 			if(item)
157 			{
158 				eventName = IPACM_Iface::ipacmcfg->getEventName(item->evt.data.event);
159 				if (eventName != NULL)
160 				{
161 					IPACMDBG("Get event %s from external queue.\n",
162 							eventName);
163 				}
164 			}
165 		}
166 		else
167 		{
168 			eventName = IPACM_Iface::ipacmcfg->getEventName(item->evt.data.event);
169 			if (eventName != NULL)
170 			{
171 				IPACMDBG("Get event %s from internal queue.\n",
172 					eventName);
173 			}
174 		}
175 
176 		if(item == NULL)
177 		{
178 			IPACMDBG("Waiting for Message\n");
179 
180 			if(pthread_cond_wait(&cond_var, &mutex) != 0)
181 			{
182 				IPACMERR("unable to lock the mutex\n");
183 
184 				if(pthread_mutex_unlock(&mutex) != 0)
185 				{
186 					IPACMERR("unable to unlock the mutex\n");
187 					return NULL;
188 				}
189 
190 				return NULL;
191 			}
192 
193 			if(pthread_mutex_unlock(&mutex) != 0)
194 			{
195 				IPACMERR("unable to unlock the mutex\n");
196 				return NULL;
197 			}
198 
199 		}
200 		else
201 		{
202 			if(pthread_mutex_unlock(&mutex) != 0)
203 			{
204 				IPACMERR("unable to unlock the mutex\n");
205 				return NULL;
206 			}
207 
208 			IPACMDBG("Processing item %pK event ID: %d\n",item,item->evt.data.event);
209 			item->evt.callback_ptr(&item->evt.data);
210 			delete item;
211 			item = NULL;
212 		}
213 
214 	} /* Go forever until a termination indication is received */
215 
216 }
217