From 0315bcfb4c38e65a9b45aef504eb6e41982de0dd Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Tue, 5 Dec 2023 22:22:45 +0800 Subject: [PATCH] bugfix watcher Signed-off-by: 18721213663 --- .../mod_fs/class_watcher/watcher_entity.cpp | 65 ++++++++++++------- .../src/mod_fs/class_watcher/watcher_entity.h | 2 +- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index 1ede032ef..a067b5f1f 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -14,11 +14,12 @@ */ #include "watcher_entity.h" +#include #include #include -#include +#include +#include #include -#include #include "filemgmt_libhilog.h" #include "uv.h" @@ -28,7 +29,6 @@ using namespace OHOS::FileManagement::LibN; using namespace std; mutex FileWatcher::watchMutex_; -mutex FileWatcher::selectMutex_; FileWatcher::FileWatcher() {} @@ -46,6 +46,11 @@ bool FileWatcher::InitNotify() HILOGE("Failed to init notify errCode:%{public}d", errno); return false; } + eventFd_ = eventfd(0, EFD_CLOEXEC); + if (eventFd_ < 0) { + HILOGE("Failed to init eventfd errCode:%{public}d", errno); + return false; + } return true; } @@ -118,6 +123,11 @@ int FileWatcher::CloseNotifyFd() HILOGE("Failed to stop notify close fd errCode:%{public}d", closeRet); } notifyFd_ = -1; + closeRet = close(eventFd_); + if (closeRet != 0) { + HILOGE("Failed to close eventfd errCode:%{public}d", closeRet); + } + eventFd_ = -1; run_ = false; } @@ -150,35 +160,42 @@ int FileWatcher::StopNotify(shared_ptr arg) void FileWatcher::GetNotifyEvent(WatcherCallback callback) { - unique_lock lock(selectMutex_, std::try_to_lock); - if (!lock.try_lock()) { - return; - } if (run_) { return; } run_ = true; char buf[BUF_SIZE] = {0}; struct inotify_event *event = nullptr; - fd_set fds; - FD_ZERO(&fds); - FD_SET(notifyFd_, &fds); - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 1000; + nfds_t nfds = 2; + struct pollfd fds[2]; + fds[0].fd = eventFd_; + fds[0].events = 0; + fds[1].fd = notifyFd_; + fds[1].events = POLLIN; + int ret = 0; while (run_) { - if (notifyFd_ < 0) { - HILOGE("Failed to run Listener Thread because notifyFd_:%{public}d", notifyFd_); - break; - } - if (select(notifyFd_ + 1, &fds, nullptr, nullptr, &tv) > 0) { - int len, index = 0; - while (((len = read(notifyFd_, &buf, sizeof(buf))) < 0) && (errno == EINTR)) {}; - while (index < len) { - event = reinterpret_cast(buf + index); - NotifyEvent(event, callback); - index += sizeof(struct inotify_event) + event->len; + ret = poll(fds, nfds, -1); + if (ret > 0) { + if (fds[0].revents & POLLNVAL) { + run_ = false; + return; } + if (fds[1].revents & POLLIN) { + int len, index = 0; + while (((len = read(notifyFd_, &buf, sizeof(buf)))) > 0) { + while (index < len) { + event = reinterpret_cast(buf + index); + NotifyEvent(event, callback); + index += sizeof(struct inotify_event) + event->len; + } + } + break; + } + } else if (ret < 0 && errno == EINTR) { + continue; + } else { + HILOGE("Failed to poll NotifyFd, errno=%{public}d", errno); + return; } } } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 102430604..94791ecce 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -67,9 +67,9 @@ private: private: static std::mutex watchMutex_; - static std::mutex selectMutex_; bool run_ = false; int32_t notifyFd_ = -1; + int32_t eventFd_ = -1; std::unordered_set> watcherInfoSet_; std::unordered_map> wdFileNameMap_; }; -- Gitee