1 /*
2 Copyright (c) 2013-2016, 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
127 IPACMDBG("MessageQueue::Process()\n");
128
129 MsgQueueInternal = MessageQueue::getInstanceInternal();
130 if(MsgQueueInternal == NULL)
131 {
132 IPACMERR("unable to start internal cmd queue process\n");
133 return NULL;
134 }
135
136 MsgQueueExternal = MessageQueue::getInstanceExternal();
137 if(MsgQueueExternal == NULL)
138 {
139 IPACMERR("unable to start external cmd queue process\n");
140 return NULL;
141 }
142
143 while(1)
144 {
145 if(pthread_mutex_lock(&mutex) != 0)
146 {
147 IPACMERR("unable to lock the mutex\n");
148 return NULL;
149 }
150
151 item = MsgQueueInternal->dequeue();
152 if(item == NULL)
153 {
154 item = MsgQueueExternal->dequeue();
155 if(item)
156 {
157 IPACMDBG("Get event %s from external queue.\n",
158 IPACM_Iface::ipacmcfg->getEventName(item->evt.data.event));
159 }
160 }
161 else
162 {
163 IPACMDBG("Get event %s from internal queue.\n",
164 IPACM_Iface::ipacmcfg->getEventName(item->evt.data.event));
165 }
166
167 if(item == NULL)
168 {
169 IPACMDBG("Waiting for Message\n");
170
171 if(pthread_cond_wait(&cond_var, &mutex) != 0)
172 {
173 IPACMERR("unable to lock the mutex\n");
174
175 if(pthread_mutex_unlock(&mutex) != 0)
176 {
177 IPACMERR("unable to unlock the mutex\n");
178 return NULL;
179 }
180
181 return NULL;
182 }
183
184 if(pthread_mutex_unlock(&mutex) != 0)
185 {
186 IPACMERR("unable to unlock the mutex\n");
187 return NULL;
188 }
189
190 }
191 else
192 {
193 if(pthread_mutex_unlock(&mutex) != 0)
194 {
195 IPACMERR("unable to unlock the mutex\n");
196 return NULL;
197 }
198
199 IPACMDBG("Processing item %p event ID: %d\n",item,item->evt.data.event);
200 item->evt.callback_ptr(&item->evt.data);
201 delete item;
202 item = NULL;
203 }
204
205 } /* Go forever until a termination indication is received */
206
207 }
208