Fix thread safe issue 30/316130/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 14 Aug 2024 03:11:08 +0000 (12:11 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 14 Aug 2024 03:11:08 +0000 (12:11 +0900)
This patch is to fix the following issue:
+------------------------------------------------------------------------------+
| Thread 18 (LWP 16980):                                                       |
| #8  0xf671776e in g_log (log_domain=<optimized out>, log_level=log           |
|                _level@entry=G_LOG_LEVEL_CRITICAL, format=0xf6755a34          |
|                "Source ID %u was not found when attempting to remove it")    |
|                at ../glib/gmessages.c:1455 --> libglib (rpm)                 |
| #9  0xf6711956 in g_source_remove (tag=33111) at ../glib/gmain.c:2543        |
| #10 0xf5d031fa in (anonymous namespace)::WatchdogContext::Stop               |
|                (this=0xf5d41dd0 <_ZN12_GLOBAL__N_17contextE>) at             |
|                /usr/src/debug/aul-0.53.7-1.arm/src/aul_watchdog.cc:122       |
|                                                                              |
| Thread 1 (LWP 16979):                                                        |
| #0  g_source_destroy_internal (source=0xe923a230, context=0xaca04b88,        |
|                               have_lock=0) at ../glib/gmain.c:1331           |
| #1  0xf671193e in g_source_remove (tag=33111) at ../glib/gmain.c:2541        |
| #2  0xf5d031fa in (anonymous namespace)::WatchdogContext::Stop               |
|          (this=0xf5d41dd0 <_ZN12_GLOBAL__N_17contextE>) at             |
|                /usr/src/debug/aul-0.53.7-1.arm/src/aul_watchdog.cc:122       |
+------------------------------------------------------------------------------+

Locking mutex is added for thread safe.

Change-Id: I404186b4d4bf04e03fd27fe56dd188aa0381d1df
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul/aul_watchdog.cc

index fdceabd959163abfee8a19bea9cb7bcf6ede8fb0..a32de6576a18161dd306c42990d9f463dae91015 100644 (file)
@@ -18,6 +18,8 @@
 
 #include <glib.h>
 
+#include <mutex>
+
 #include "aul/app_request.h"
 #include "aul/aul_api.h"
 #include "aul/aul_util.h"
@@ -35,6 +37,7 @@ class WatchdogContext {
   WatchdogContext() = default;
 
   ~WatchdogContext() {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     Stop();
 
     if (enabled_) {
@@ -47,6 +50,7 @@ class WatchdogContext {
   }
 
   void Enable() {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     if (enabled_) {
       _W("Already enabled");
       return;
@@ -62,6 +66,7 @@ class WatchdogContext {
   }
 
   void Disable() {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     if (!enabled_)
       THROW(AUL_R_ERROR);
 
@@ -75,6 +80,7 @@ class WatchdogContext {
   }
 
   void Kick() {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     if (!enabled_)
       THROW(AUL_R_ERROR);
 
@@ -89,6 +95,7 @@ class WatchdogContext {
   }
 
   void Start(unsigned int interval) {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     _D("Interval: %u", interval);
     if (interval == 0) {
       _E("Invalid parameter");
@@ -122,6 +129,7 @@ class WatchdogContext {
   }
 
   void Stop() {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     if (timer_ == 0)
       return;
 
@@ -130,6 +138,7 @@ class WatchdogContext {
   }
 
   bool IsEnabled() const {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
     return enabled_;
   }
 
@@ -143,10 +152,13 @@ class WatchdogContext {
     _D("Ping");
   }
 
+  std::recursive_mutex& GetMutex() const { return mutex_; }
+
  private:
   bool enabled_ = false;
   unsigned int interval_ = 0;
   guint timer_ = 0;
+  mutable std::recursive_mutex mutex_;
 };
 
 WatchdogContext context;