Add API for check core thread + shutting down 96/299296/4
authorEunki Hong <eunkiki.hong@samsung.com>
Mon, 25 Sep 2023 09:56:35 +0000 (18:56 +0900)
committerEunki Hong <eunkiki.hong@samsung.com>
Tue, 10 Oct 2023 03:15:27 +0000 (12:15 +0900)
Add some API to check whether the core is shutting down or not,
So out of dali-core side can also know it.

Also, Make combined helper API to check whether core installed before or not.
It will be useful when we check worker thread access into UI classes of dali.

Change-Id: I2d8d3120b4fc959056f4644846d3d14a3b89bcc6
Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
automated-tests/src/dali/utc-Dali-Stage.cpp
dali/devel-api/common/stage.cpp
dali/devel-api/common/stage.h
dali/internal/event/common/stage-impl.cpp
dali/internal/event/common/stage-impl.h

index b9b805007f15d9b15641108fb3b13613a6a05778..d3866df3f2745522207ddd75d58b48a87c79b5fb 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <dali-test-suite-utils.h>
 #include <dali/devel-api/common/stage-devel.h>
+#include <dali/devel-api/threading/thread.h>
 #include <dali/integration-api/context-notifier.h>
 #include <dali/integration-api/events/key-event-integ.h>
 #include <dali/integration-api/events/touch-event-integ.h>
@@ -24,6 +25,7 @@
 #include <dali/public-api/dali-core.h>
 #include <dali/public-api/events/key-event.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <iostream>
 
@@ -317,6 +319,44 @@ void GenerateTouch(TestApplication& application, PointState::Type state, const V
   application.ProcessEvent(touchEvent);
 }
 
+volatile bool gRunThreadEntryFunc = false;
+
+class TestThread : public Dali::Thread
+{
+public:
+  TestThread()
+  : mCallback(nullptr)
+  {
+  }
+  TestThread(Dali::CallbackBase* callback)
+  : mCallback(callback)
+  {
+  }
+
+  ~TestThread()
+  {
+    delete mCallback;
+  }
+
+  virtual void Run()
+  {
+    if(mCallback)
+    {
+      Dali::CallbackBase::Execute(*mCallback);
+    }
+
+    gRunThreadEntryFunc = true;
+  }
+
+private:
+  Dali::CallbackBase* mCallback{nullptr};
+};
+
+void CoreThreadCheck()
+{
+  DALI_TEST_CHECK(!Stage::IsCoreThread());
+}
+
 } // unnamed namespace
 
 int UtcDaliStageDefaultConstructorP(void)
@@ -387,6 +427,59 @@ int UtcDaliStageIsInstalledN(void)
   END_TEST;
 }
 
+int UtcDaliStageIsShuttingDown(void)
+{
+  DALI_TEST_CHECK(!Stage::IsShuttingDown());
+
+  {
+    TestApplication application;
+
+    DALI_TEST_CHECK(!Stage::IsShuttingDown());
+
+    Stage::GetCurrent();
+
+    DALI_TEST_CHECK(!Stage::IsShuttingDown());
+  }
+
+  // Core destroyed
+  DALI_TEST_CHECK(Stage::IsShuttingDown());
+  END_TEST;
+}
+
+int UtcDaliStageIsCoreThread(void)
+{
+  DALI_TEST_CHECK(!Stage::IsCoreThread());
+
+  {
+    TestApplication application;
+
+    DALI_TEST_CHECK(Stage::IsCoreThread());
+
+    Stage::GetCurrent();
+
+    DALI_TEST_CHECK(Stage::IsCoreThread());
+
+    TestThread thread(Dali::MakeCallback(&CoreThreadCheck));
+
+    gRunThreadEntryFunc = false;
+
+    thread.Start();
+    // wait till the thread is terminated
+    while(!gRunThreadEntryFunc)
+    {
+      usleep(1); // 1 microsecond
+    }
+    DALI_TEST_EQUALS(true, gRunThreadEntryFunc, TEST_LOCATION);
+
+    thread.Join();
+  }
+
+  // Core destroyed
+  DALI_TEST_CHECK(Stage::IsCoreThread());
+  END_TEST;
+}
+
+
 int UtcDaliStageCopyConstructorP(void)
 {
   TestApplication application;
index 02f361fa05f67db4e821ae3910748efe07920833..7a594d26e22c4ffc79bb319a5a37b3b5408f092e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -59,6 +59,17 @@ bool Stage::IsInstalled()
   return Internal::Stage::IsInstalled();
 }
 
+bool Stage::IsShuttingDown()
+{
+  return Internal::Stage::IsShuttingDown();
+}
+
+bool Stage::IsCoreThread()
+{
+  return IsInstalled() ||  ///< Check if Core is installed now,
+         IsShuttingDown(); ///< or Core is shutting down now.
+}
+
 void Stage::Add(Actor& actor)
 {
   GetImplementation(*this).Add(GetImplementation(actor));
index cf271cfbaab3e4580f6baba09f719b02200c9fdf..6597f0b07206993fd99587c1f29171ca3476b099 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_STAGE_H
 
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -113,6 +113,21 @@ public:
    */
   static bool IsInstalled();
 
+  /**
+   * @brief Queries whether the Stage shutting down now; this should only return false during or before destruction of Dali core.
+   *
+   * @return True when Dali core destructor called.
+   */
+  static bool IsShuttingDown();
+
+  /**
+   * @brief Queries whether we installed Dali core before, or not.
+   * It will be useful whether you want to check we are on valid core ui thread or not, after Core initalized ensured.
+   *
+   * @return True when this API called at core ui thread. False otherwise.
+   */
+  static bool IsCoreThread();
+
   /**
    * @brief Destructor.
    *
index d2309d6519c37c66a056aea8fa882ce99619c27d..ae0b7d2fe8b5b5a2e1644950148c72f374da0176 100644 (file)
@@ -112,6 +112,11 @@ bool Stage::IsInstalled()
   return ThreadLocalStorage::Created();
 }
 
+bool Stage::IsShuttingDown()
+{
+  return ThreadLocalStorage::IsShuttingDown();
+}
+
 ObjectRegistry& Stage::GetObjectRegistry()
 {
   return ThreadLocalStorage::Get().GetObjectRegistry();
index 8711fcf7107f464981e11101cc2e7aa16072e8a8..8b4abed11abf2e63e8588b7b00d26c42c4f72645 100644 (file)
@@ -89,6 +89,11 @@ public:
    */
   static bool IsInstalled();
 
+  /**
+   * @copydoc Dali::Stage::IsShuttingDown().
+   */
+  static bool IsShuttingDown();
+
   /**
    * @copydoc Dali::Stage::GetObjectRegistry()
    */