@@ -204,7 +204,14 @@ void T2ER_Push(char* eventName, char* eventValue)
204204 event -> value = strdup (eventValue );
205205 T2Debug ("Adding eventName : %s eventValue : %s to t2event queue\n" , event -> name , event -> value );
206206 t2_queue_push (eQueue , (void * ) event );
207- if (!stopDispatchThread )
207+
208+ bool shouldSignal = false;
209+ if (pthread_mutex_lock (& sTDMutex ) == 0 )
210+ {
211+ shouldSignal = !stopDispatchThread ;
212+ pthread_mutex_unlock (& sTDMutex );
213+ }
214+ if (shouldSignal )
208215 {
209216 ret = pthread_cond_signal (& erCond );
210217 if (ret != 0 ) // pthread_cond _signal failed so after unlocking the mutex it will return
@@ -235,14 +242,26 @@ void* T2ER_EventDispatchThread(void *arg)
235242 (void ) arg ; // To avoid compiler warning
236243 T2Debug ("%s ++in\n" , __FUNCTION__ );
237244 Vector * profileList = NULL ;
238- while (!stopDispatchThread )
245+ bool shouldContinue = true;
246+ while (shouldContinue )
239247 {
240248 if (pthread_mutex_lock (& erMutex ) != 0 ) // mutex lock failed, without locking eQueue shouldn't be accessed
241249 {
242250 T2Error ("%s pthread_mutex_lock for erMutex failed\n" , __FUNCTION__ );
243251 return NULL ;
244252 }
245253 T2Debug ("Checking for events in event queue , event count = %d\n" , t2_queue_count (eQueue ));
254+ while (t2_queue_count (eQueue ) == 0 && shouldContinue )
255+ {
256+ T2Debug ("Event Queue size is 0, Waiting events from T2ER_Push\n" );
257+ int ret = pthread_cond_wait (& erCond , & erMutex );
258+ if (ret != 0 ) // pthread cond wait failed return after unlock
259+ {
260+ T2Error ("%s pthread_cond_wait failed with error code: %d\n" , __FUNCTION__ , ret );
261+ }
262+ T2Debug ("Received signal from T2ER_Push\n" );
263+ }
264+
246265 if (t2_queue_count (eQueue ) > 0 )
247266 {
248267 T2Event * event = (T2Event * )t2_queue_pop (eQueue );
@@ -280,18 +299,21 @@ void* T2ER_EventDispatchThread(void *arg)
280299 }
281300 else
282301 {
283- T2Debug ("Event Queue size is 0, Waiting events from T2ER_Push\n" );
284- int ret = pthread_cond_wait (& erCond , & erMutex );
285- if (ret != 0 ) // pthread cond wait failed return after unlock
286- {
287- T2Error ("%s pthread_cond_wait failed with error code: %d\n" , __FUNCTION__ , ret );
288- }
289302 if (pthread_mutex_unlock (& erMutex ) != 0 )
290303 {
291304 T2Error ("%s pthread_mutex_unlock for erMutex failed\n" , __FUNCTION__ );
292305 return NULL ;
293306 }
294- T2Debug ("Received signal from T2ER_Push\n" );
307+ }
308+ // Check if thread should continue
309+ if (pthread_mutex_lock (& sTDMutex ) == 0 )
310+ {
311+ shouldContinue = !stopDispatchThread ;
312+ pthread_mutex_unlock (& sTDMutex );
313+ }
314+ else
315+ {
316+ shouldContinue = false; // Exit on lock failure
295317 }
296318 }
297319 T2Debug ("%s --out\n" , __FUNCTION__ );
@@ -345,7 +367,10 @@ T2ERROR T2ER_Init()
345367 registerForTelemetryEvents (T2ER_PushDataWithDelim );
346368 }
347369
348- system ("touch /tmp/.t2ReadyToReceiveEvents" );
370+ if (system ("touch /tmp/.t2ReadyToReceiveEvents" ) != 0 )
371+ {
372+ T2Error ("Failed to create /tmp/.t2ReadyToReceiveEvents flag file \n" );
373+ }
349374 setT2EventReceiveState (T2_STATE_COMPONENT_READY );
350375 T2Info ("T2 is now Ready to Recieve Events\n" );
351376
@@ -500,14 +525,16 @@ void T2ER_Uninit()
500525 }
501526 EREnabled = false;
502527
528+ pthread_t threadToJoin ;
529+ if (pthread_mutex_lock (& sTDMutex ) != 0 ) // mutex lock failed so return from T2ER_Uninit
530+ {
531+ T2Error ("%s pthread_mutex_lock for sTDMutex failed\n" , __FUNCTION__ );
532+ return ;
533+ }
503534 if (!stopDispatchThread )
504535 {
505- if (pthread_mutex_lock (& sTDMutex ) != 0 ) // mutex lock failed so return from T2ER_Uninit
506- {
507- T2Error ("%s pthread_mutex_lock for sTDMutex failed\n" , __FUNCTION__ );
508- return ;
509- }
510536 stopDispatchThread = true;
537+ threadToJoin = erThread ; // Save thread handle while holding lock
511538 if (pthread_mutex_unlock (& sTDMutex ) != 0 ) //mutex unlock failed so return from T2ER_Uninit
512539 {
513540 T2Error ("%s pthread_mutex_unlock for sTDMutex failed\n" , __FUNCTION__ );
@@ -530,7 +557,7 @@ void T2ER_Uninit()
530557 return ;
531558 }
532559
533- if (pthread_join (erThread , NULL ) != 0 )
560+ if (pthread_join (threadToJoin , NULL ) != 0 )
534561 {
535562 T2Error ("%s erThread join failed\n" , __FUNCTION__ );
536563 }
@@ -551,6 +578,14 @@ void T2ER_Uninit()
551578 return ;
552579 }
553580 }
581+ else
582+ {
583+ if (pthread_mutex_unlock (& sTDMutex ) != 0 )
584+ {
585+ T2Error ("%s pthread_mutex_unlock for sTDMutex failed\n" , __FUNCTION__ );
586+ return ;
587+ }
588+ }
554589 T2Debug ("T2ER Event Dispatch Thread successfully terminated\n" );
555590 t2_queue_destroy (eQueue , freeT2Event );
556591 eQueue = NULL ;
0 commit comments