提交 7d4009b6 编写于 作者: C cheng_jinsong

use thread to check

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 836ccc39
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
#include "device_info_stub.h" #include "device_info_stub.h"
#include <chrono> #include <chrono>
#include <csignal> #include <thread>
#include <unistd.h>
#include "beget_ext.h" #include "beget_ext.h"
#include "idevice_info.h" #include "idevice_info.h"
...@@ -48,23 +47,23 @@ static const int DEVICE_INFO_EXIT_TIMEOUT_S = 15; ...@@ -48,23 +47,23 @@ static const int DEVICE_INFO_EXIT_TIMEOUT_S = 15;
static const int DEVICE_INFO_EXIT_TIMEOUT_S = 3; static const int DEVICE_INFO_EXIT_TIMEOUT_S = 3;
#endif #endif
static void UnloadDeviceInfoSa(int signo) static int UnloadDeviceInfoSa(void)
{ {
{ {
std::unique_lock<std::mutex> lock(g_lock); std::unique_lock<std::mutex> lock(g_lock);
struct timespec currTimer = {0}; struct timespec currTimer = {0};
(void)clock_gettime(CLOCK_MONOTONIC, &currTimer); (void)clock_gettime(CLOCK_MONOTONIC, &currTimer);
if (IntervalTime(&g_lastTime, &currTimer) < DEVICE_INFO_EXIT_TIMEOUT_S) { if (IntervalTime(&g_lastTime, &currTimer) < DEVICE_INFO_EXIT_TIMEOUT_S) {
alarm(DEVICE_INFO_EXIT_TIMEOUT_S / 3); // 3 half return 0;
return;
} }
} }
DINFO_LOGI("DeviceInfoService::UnloadDeviceInfoSa"); DINFO_LOGI("DeviceInfoService::UnloadDeviceInfoSa");
auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
DINFO_CHECK(sam != nullptr, return, "GetSystemAbilityManager return null"); DINFO_CHECK(sam != nullptr, return 0, "GetSystemAbilityManager return null");
int32_t ret = sam->UnloadSystemAbility(SYSPARAM_DEVICE_SERVICE_ID); int32_t ret = sam->UnloadSystemAbility(SYSPARAM_DEVICE_SERVICE_ID);
DINFO_CHECK(ret == ERR_OK, return, "UnLoadSystemAbility deviceinfo sa failed"); DINFO_CHECK(ret == ERR_OK, return 0, "UnLoadSystemAbility deviceinfo sa failed");
return 1;
} }
int32_t DeviceInfoStub::OnRemoteRequest(uint32_t code, int32_t DeviceInfoStub::OnRemoteRequest(uint32_t code,
...@@ -144,14 +143,14 @@ void DeviceInfoService::OnStart(void) ...@@ -144,14 +143,14 @@ void DeviceInfoService::OnStart(void)
if (!res) { if (!res) {
DINFO_LOGE("DeviceInfoService Publish failed"); DINFO_LOGE("DeviceInfoService Publish failed");
} }
signal(SIGALRM, UnloadDeviceInfoSa); threadStarted_ = true;
alarm(DEVICE_INFO_EXIT_TIMEOUT_S / 2); // 2 half std::thread(&DeviceInfoService::ThreadForUnloadSa, this).detach();
return; return;
} }
void DeviceInfoService::OnStop(void) void DeviceInfoService::OnStop(void)
{ {
signal(SIGALRM, nullptr); threadStarted_ = false;
DINFO_LOGI("DeviceInfoService OnStop"); DINFO_LOGI("DeviceInfoService OnStop");
} }
...@@ -162,5 +161,18 @@ int DeviceInfoService::Dump(int fd, const std::vector<std::u16string>& args) ...@@ -162,5 +161,18 @@ int DeviceInfoService::Dump(int fd, const std::vector<std::u16string>& args)
DINFO_CHECK(fd >= 0, return -1, "Invalid fd for dump %d", fd); DINFO_CHECK(fd >= 0, return -1, "Invalid fd for dump %d", fd);
return dprintf(fd, "%s\n", "No information to dump for this service"); return dprintf(fd, "%s\n", "No information to dump for this service");
} }
void DeviceInfoService::ThreadForUnloadSa(void)
{
while (1) {
std::this_thread::sleep_for(std::chrono::seconds(DEVICE_INFO_EXIT_TIMEOUT_S / 3)); // 3 count
if (!threadStarted_) {
break;
}
if (UnloadDeviceInfoSa() == 1) {
break;
}
}
}
} // namespace device_info } // namespace device_info
} // namespace OHOS } // namespace OHOS
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#ifndef OHOS_SYSTEM_DEVICEIDSTUB_H #ifndef OHOS_SYSTEM_DEVICEIDSTUB_H
#define OHOS_SYSTEM_DEVICEIDSTUB_H #define OHOS_SYSTEM_DEVICEIDSTUB_H
#include <atomic>
#include <mutex>
#include "iremote_stub.h" #include "iremote_stub.h"
#include "idevice_info.h" #include "idevice_info.h"
#include "system_ability.h" #include "system_ability.h"
...@@ -45,6 +47,8 @@ protected: ...@@ -45,6 +47,8 @@ protected:
void OnStart(void) override; void OnStart(void) override;
void OnStop(void) override; void OnStop(void) override;
int Dump(int fd, const std::vector<std::u16string>& args) override; int Dump(int fd, const std::vector<std::u16string>& args) override;
void ThreadForUnloadSa(void);
std::atomic_bool threadStarted_ { false };
}; };
} // namespace device_info } // namespace device_info
} // namespace OHOS } // namespace OHOS
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册