21
21
namespace WPEFramework {
22
22
namespace Plugin {
23
23
24
+ static int threadCount = 0 ;
24
25
bool EndpointCache::isEndpointCashFileExist ()
25
26
{
26
27
std::ifstream fileStream (CachefilePath);
@@ -457,25 +458,18 @@ namespace WPEFramework {
457
458
458
459
timeout.store (timeoutInSeconds >= MONITOR_TIMEOUT_INTERVAL_MIN ? timeoutInSeconds:defaultTimeoutInSec);
459
460
460
- if (isMonitorThreadRunning () && stopFlag == false )
461
+ if (isMonitorThreadRunning ())
461
462
{
462
463
isContinuesMonitoringNeeded = true ;
463
464
resetTimeout = true ;
464
465
cv_.notify_all ();
465
466
}
466
467
else
467
468
{
468
- if (thread_.joinable ())
469
- {
470
- LOGWARN (" Connectivity Monitor joinable Thread is active" );
471
- stopFlag = true ;
472
- cv_.notify_all ();
473
- thread_.join ();
474
- }
475
-
476
469
isContinuesMonitoringNeeded = true ;
477
470
stopFlag = false ;
478
471
thread_ = std::thread (&ConnectivityMonitor::connectivityMonitorFunction, this );
472
+ thread_.detach ();
479
473
LOGINFO (" Connectivity monitor started with %d" , timeout.load ());
480
474
}
481
475
@@ -490,34 +484,27 @@ namespace WPEFramework {
490
484
return false ;
491
485
}
492
486
493
- if (isMonitorThreadRunning () && stopFlag == false )
487
+ if (isMonitorThreadRunning ())
494
488
{
495
- LOGINFO (" Connectivity Monitor Thread is active so notify" );
489
+ LOGINFO (" Connectivity monitor thread is active so notify" );
496
490
g_internetState = nsm_internetState::UNKNOWN;
497
491
cv_.notify_all ();
498
492
}
499
493
else
500
494
{
501
- if (thread_.joinable ())
502
- {
503
- LOGWARN (" Connectivity Monitor joinable Thread is active" );
504
- stopFlag = true ;
505
- cv_.notify_all ();
506
- thread_.join ();
507
- }
508
-
509
495
stopFlag = false ;
510
496
timeout.store (timeoutInSeconds >= MONITOR_TIMEOUT_INTERVAL_MIN ? timeoutInSeconds:defaultTimeoutInSec);
511
497
thread_ = std::thread (&ConnectivityMonitor::connectivityMonitorFunction, this );
512
- LOGINFO (" Initial Connectivity Monitoring started with %d" , timeout.load ());
498
+ thread_.detach ();
499
+ LOGINFO (" Initial connectivity monitoring started with %d" , timeout.load ());
513
500
}
514
501
515
502
return true ;
516
503
}
517
504
518
505
bool ConnectivityMonitor::isMonitorThreadRunning ()
519
506
{
520
- return thread_. joinable ();
507
+ return isRunning. load ();
521
508
}
522
509
523
510
bool ConnectivityMonitor::stopInitialConnectivityMonitoring ()
@@ -528,42 +515,55 @@ namespace WPEFramework {
528
515
return true ;
529
516
}
530
517
531
- stopFlag = true ;
532
- cv_.notify_all ();
533
-
534
- if (thread_.joinable ())
535
- thread_.join ();
518
+ int tryCount = 5 ; // max 4 sec
519
+ do {
520
+ stopFlag = true ;
521
+ cv_.notify_all ();
522
+ sleep (1 );
523
+ } while (isMonitorThreadRunning () && (--tryCount > 0 ));
536
524
537
- LOGINFO (" Initial Connectivity Monitor stopped" );
525
+ if (isMonitorThreadRunning () && isContinuesMonitoringNeeded == false )
526
+ {
527
+ LOGERR (" Initial connectivity monitor not stopped" );
528
+ return false ;
529
+ }
530
+ else
531
+ LOGINFO (" Initial connectivity monitor stopped" );
538
532
539
533
return true ;
540
534
}
541
535
542
536
bool ConnectivityMonitor::stopContinuousConnectivityMonitoring ()
543
537
{
544
- stopFlag = true ;
545
- cv_.notify_all ();
538
+ int tryCount = 5 ; // max 5 sec
539
+ do {
540
+ stopFlag = true ;
541
+ cv_.notify_all ();
542
+ sleep (1 );
543
+ } while (isMonitorThreadRunning () && (--tryCount > 0 ));
546
544
547
- if (thread_.joinable ())
548
- thread_.join ();
545
+ if (isMonitorThreadRunning ())
546
+ {
547
+ LOGERR (" Continuous connectivity monitor not stopped" );
548
+ return false ;
549
+ }
550
+ else
551
+ LOGINFO (" Continuous connectivity monitor stopped" );
549
552
550
- isContinuesMonitoringNeeded = false ;
551
- LOGINFO (" Continuous Connectivity monitor stopped" );
552
553
return true ;
553
554
}
554
555
555
556
void ConnectivityMonitor::signalConnectivityMonitor ()
556
557
{
557
- if (isMonitorThreadRunning ())
558
- {
559
- /* Reset the global value to UNKNOWN state so the cache is reset */
560
- g_internetState = nsm_internetState::UNKNOWN;
561
- cv_.notify_all ();
562
- }
558
+ /* Reset the global value to UNKNOWN state so the cache is reset */
559
+ g_internetState = nsm_internetState::UNKNOWN;
560
+ cv_.notify_all ();
563
561
}
564
562
565
563
void ConnectivityMonitor::connectivityMonitorFunction ()
566
564
{
565
+ isRunning = true ;
566
+ threadCount++;
567
567
nsm_internetState InternetConnectionState = nsm_internetState::UNKNOWN;
568
568
int notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT;
569
569
int tempTimeout = defaultTimeoutInSec;
@@ -641,9 +641,18 @@ namespace WPEFramework {
641
641
}
642
642
643
643
} while (!stopFlag);
644
-
645
644
g_internetState = nsm_internetState::UNKNOWN;
646
645
LOGWARN (" Connectivity monitor exiting" );
646
+ threadCount--;
647
+ isRunning = false ;
648
+ }
649
+
650
+ ConnectivityMonitor::~ConnectivityMonitor () {
651
+ LOGINFO (" ~ConnectivityMonitor" );
652
+ stopContinuousConnectivityMonitoring ();
653
+ sleep (1 );
654
+ if (threadCount > 0 )
655
+ LOGWARN (" thread still active %d" , threadCount);
647
656
}
648
657
649
658
} // namespace Plugin
0 commit comments