@@ -50,17 +50,28 @@ static void *process_rp_thread(void *data)
5050{
5151 (void ) data ;//To fix compiler warning
5252 cJSON * reportProfiles = NULL ;
53+ bool shouldContinue = true;
5354
5455 T2Debug ("%s ++in\n" , __FUNCTION__ );
5556
56- while (! stopProcessing )
57+ while (shouldContinue )
5758 {
5859 pthread_mutex_lock (& rpMutex );
60+ shouldContinue = !stopProcessing ;
61+ if (!shouldContinue )
62+ {
63+ pthread_mutex_unlock (& rpMutex );
64+ break ;
65+ }
5966 T2Info ("%s: Waiting for event from tr-181 \n" , __FUNCTION__ );
60- pthread_cond_wait (& rpCond , & rpMutex );
67+ while (t2_queue_count (rpQueue ) == 0 && shouldContinue )
68+ {
69+ pthread_cond_wait (& rpCond , & rpMutex );
70+ shouldContinue = !stopProcessing ;
71+ }
6172
6273 T2Debug ("%s: Received wake up signal \n" , __FUNCTION__ );
63- if (t2_queue_count (rpQueue ) > 0 )
74+ if (t2_queue_count (rpQueue ) > 0 && shouldContinue )
6475 {
6576 reportProfiles = (cJSON * )t2_queue_pop (rpQueue );
6677 if (reportProfiles )
@@ -80,17 +91,32 @@ static void *process_tmprp_thread(void *data)
8091{
8192 (void ) data ;//To fix compiler warning
8293 cJSON * tmpReportProfiles = NULL ;
94+ bool shouldContinue = true;
8395
8496 T2Debug ("%s ++in\n" , __FUNCTION__ );
8597
86- while (! stopProcessing )
98+ while (shouldContinue )
8799 {
88100 pthread_mutex_lock (& tmpRpMutex );
101+ pthread_mutex_lock (& rpMutex );
102+ shouldContinue = !stopProcessing ;
103+ pthread_mutex_unlock (& rpMutex );
104+ if (!shouldContinue )
105+ {
106+ pthread_mutex_unlock (& tmpRpMutex );
107+ break ;
108+ }
89109 T2Info ("%s: Waiting for event from tr-181 \n" , __FUNCTION__ );
90- pthread_cond_wait (& tmpRpCond , & tmpRpMutex );
110+ while (t2_queue_count (tmpRpQueue ) == 0 && shouldContinue )
111+ {
112+ pthread_cond_wait (& tmpRpCond , & tmpRpMutex );
113+ pthread_mutex_lock (& rpMutex );
114+ shouldContinue = !stopProcessing ;
115+ pthread_mutex_unlock (& rpMutex );
116+ }
91117
92118 T2Debug ("%s: Received wake up signal \n" , __FUNCTION__ );
93- if (t2_queue_count (tmpRpQueue ) > 0 )
119+ if (t2_queue_count (tmpRpQueue ) > 0 && shouldContinue )
94120 {
95121 tmpReportProfiles = (cJSON * )t2_queue_pop (tmpRpQueue );
96122 if (tmpReportProfiles )
@@ -110,11 +136,31 @@ static void *process_msg_thread(void *data)
110136{
111137 (void ) data ;//To fix compiler warning
112138 struct __msgpack__ * msgpack ;
113- while (!stopProcessing )
139+ bool shouldContinue = true;
140+
141+ while (shouldContinue )
114142 {
143+ // Check stopProcessing first without holding rpMsgMutex to avoid
144+ // nested mutex acquisition which could lead to deadlock if another
145+ // thread acquires these mutexes in opposite order (e.g., rpMutex then rpMsgMutex)
146+ pthread_mutex_lock (& rpMutex );
147+ shouldContinue = !stopProcessing ;
148+ pthread_mutex_unlock (& rpMutex );
149+
150+ if (!shouldContinue )
151+ {
152+ break ;
153+ }
154+
115155 pthread_mutex_lock (& rpMsgMutex );
116- pthread_cond_wait (& msg_Cond , & rpMsgMutex );
117- if (t2_queue_count (rpMsgPkgQueue ) > 0 )
156+ while (t2_queue_count (rpMsgPkgQueue ) == 0 && shouldContinue )
157+ {
158+ pthread_cond_wait (& msg_Cond , & rpMsgMutex );
159+ pthread_mutex_lock (& rpMutex );
160+ shouldContinue = !stopProcessing ;
161+ pthread_mutex_unlock (& rpMutex );
162+ }
163+ if (t2_queue_count (rpMsgPkgQueue ) > 0 && shouldContinue )
118164 {
119165 msgpack = (struct __msgpack__ * )t2_queue_pop (rpMsgPkgQueue );
120166 if (msgpack )
@@ -274,18 +320,24 @@ T2ERROR datamodel_MsgpackProcessProfile(char *str, int strSize)
274320
275321 msgpack -> msgpack_blob = str ;
276322 msgpack -> msgpack_blob_size = strSize ;
277- pthread_mutex_lock (& rpMsgMutex );
278- if (!stopProcessing )
279- {
280- t2_queue_push (rpMsgPkgQueue , (void * )msgpack );
281- pthread_cond_signal (& msg_Cond );
282- }
283- else
323+
324+ // Check stopProcessing without holding rpMsgMutex to avoid nested mutex
325+ // acquisition which could lead to deadlock
326+ pthread_mutex_lock (& rpMutex );
327+ bool isProcessing = !stopProcessing ;
328+ pthread_mutex_unlock (& rpMutex );
329+
330+ if (!isProcessing )
284331 {
285332 free (msgpack -> msgpack_blob );
286333 free (msgpack );
287334 T2Error ("Datamodel not initialized, dropping request \n" );
335+ return T2ERROR_SUCCESS ;
288336 }
337+
338+ pthread_mutex_lock (& rpMsgMutex );
339+ t2_queue_push (rpMsgPkgQueue , (void * )msgpack );
340+ pthread_cond_signal (& msg_Cond );
289341 pthread_mutex_unlock (& rpMsgMutex );
290342 return T2ERROR_SUCCESS ;
291343}
0 commit comments