#endif\r
static ThreadId g_rtcEventThreadId;\r
static ThreadId g_mainThreadId;\r
+static bool g_graphicServiceInitialized = false;\r
+static OHOS::GraphicMutex g_initMutex;\r
GraphicService::GraphicService() {}\r
\r
GraphicService::~GraphicService() {}\r
\r
bool GraphicService::InitGraphicService()\r
{\r
+ /* Use mutex to prevent race condition during initialization */\r
+ g_initMutex.Lock();\r
+ \r
+ /* Check if already initialized to prevent duplicate thread creation */\r
+ //在创建线程之前设置 g_graphicServiceInitialized = true,防止并发初始化\r
+ if (g_graphicServiceInitialized || g_mainThreadId != nullptr || g_rtcEventThreadId != nullptr) {\r
+ g_initMutex.Unlock();\r
+ GRAPHIC_LOGW("GraphicService already initialized, skip duplicate initialization\n");\r
+ return true;\r
+ }\r
+ \r
+ /* Set flag early to prevent concurrent initialization */\r
+ g_graphicServiceInitialized = true;\r
+\r
#if !defined(_WIN32)\r
if (!uapi_is_lcd_connect()) {\r
+ g_graphicServiceInitialized = false; /* Reset flag on failure */\r
+ g_initMutex.Unlock();\r
return false;\r
}\r
#endif\r
g_rtcEventThreadId = (ThreadId)ThreadCreate((Runnable)GraphicService::RtcEventThread, nullptr, &rtcAttr);\r
if (g_rtcEventThreadId == nullptr) {\r
GRAPHIC_LOGE("%s: create thread fail\n", __func__);\r
+ g_graphicServiceInitialized = false; /* Reset flag on failure */\r
+ g_initMutex.Unlock();\r
return false;\r
}\r
\r
g_mainThreadId = (ThreadId)ThreadCreate((Runnable)GraphicService::NativeMainThread, nullptr, &mainAttr);\r
if (g_mainThreadId == nullptr) {\r
GRAPHIC_LOGE("create thread fail\n");\r
+ g_graphicServiceInitialized = false; /* Reset flag on failure */\r
+ g_initMutex.Unlock();\r
return false;\r
}\r
+ \r
+ g_initMutex.Unlock();\r
return true;\r
}\r