!16350 OnShutdown时移除fd监听

Merge pull request !16350 from shegangbin/fd_onshutdown
This commit is contained in:
openharmony_ci 2024-11-02 11:23:35 +00:00 committed by Gitee
commit 8d3cc0f855
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 66 additions and 29 deletions

View File

@ -34,6 +34,7 @@ class VSyncCallBackListener : public OHOS::AppExecFwk::FileDescriptorListener {
public:
using VSyncCallback = std::function<void(int64_t, void*)>;
using VSyncCallbackWithId = std::function<void(int64_t, int64_t, void*)>;
using FdShutDownCallback = std::function<void(int32_t)>;
struct FrameCallback {
void *userData_;
VSyncCallback callback_;
@ -99,12 +100,16 @@ public:
frameCallbacks_.push_back(cb);
}
void CloseFd(int32_t fd);
void RegisterFdShutDownCallback(FdShutDownCallback cb);
void SetFdClosedFlagLocked(bool fdClosed);
std::mutex fdMutex_;
private:
void OnReadable(int32_t fileDescriptor) override;
void OnShutdown(int32_t fileDescriptor) override;
int64_t CalculateExpectedEndLocked(int64_t now);
void HandleVsyncCallbacks(int64_t data[], ssize_t dataCount);
void HandleVsyncCallbacks(int64_t data[], ssize_t dataCount, int32_t fileDescriptor);
VsyncError ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount);
VSyncCallback vsyncCallbacks_;
VSyncCallbackWithId vsyncCallbacksWithId_;
@ -117,8 +122,9 @@ private:
thread_local static inline int64_t periodShared_ = 0;
thread_local static inline int64_t timeStampShared_ = 0;
std::vector<FrameCallback> frameCallbacks_ = {};
std::mutex fdMutex_;
bool fdClosed_ = false;
FdShutDownCallback fdShutDownCallback_ = nullptr;
std::mutex cbMutex_;
};
#ifdef __OHOS__
@ -158,7 +164,8 @@ public:
virtual VsyncError RequestNextVSyncWithMultiCallback(FrameCallback callback);
virtual VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch);
private:
VsyncError Destroy();
VsyncError DestroyLocked();
void RemoveAndCloseFdLocked();
sptr<IVSyncConnection> connection_;
sptr<IRemoteObject> token_;
std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_;

View File

@ -39,6 +39,7 @@ void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
{
HitracePerfScoped perfTrace(ScopedDebugTrace::isEnabled(), HITRACE_TAG_GRAPHIC_AGP, "OnReadablePerfCount");
if (fileDescriptor < 0) {
VLOGE("OnReadable Invalid fileDescriptor:%{public}d", fileDescriptor);
return;
}
// 3 is array size.
@ -47,7 +48,20 @@ void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
if (ReadFdInternal(fileDescriptor, data, dataCount) != VSYNC_ERROR_OK) {
return;
}
HandleVsyncCallbacks(data, dataCount);
HandleVsyncCallbacks(data, dataCount, fileDescriptor);
}
void VSyncCallBackListener::OnShutdown(int32_t fileDescriptor)
{
VLOGI("OnShutdown, fileDescriptor:%{public}d", fileDescriptor);
FdShutDownCallback fdShutDownCallback = nullptr;
{
std::lock_guard<std::mutex> locker(cbMutex_);
fdShutDownCallback = fdShutDownCallback_;
}
if (fdShutDownCallback != nullptr) {
fdShutDownCallback(fileDescriptor);
}
}
VsyncError VSyncCallBackListener::ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount)
@ -79,7 +93,7 @@ VsyncError VSyncCallBackListener::ReadFdInternal(int32_t fd, int64_t (&data)[3],
return VSYNC_ERROR_OK;
}
void VSyncCallBackListener::HandleVsyncCallbacks(int64_t data[], ssize_t dataCount)
void VSyncCallBackListener::HandleVsyncCallbacks(int64_t data[], ssize_t dataCount, int32_t fileDescriptor)
{
VSyncCallback cb = nullptr;
VSyncCallbackWithId cbWithId = nullptr;
@ -105,8 +119,8 @@ void VSyncCallBackListener::HandleVsyncCallbacks(int64_t data[], ssize_t dataCou
VLOGD("dataCount:%{public}d, cb == nullptr:%{public}d", dataCount, (cb == nullptr));
// 1, 2: index of array data.
RS_TRACE_NAME_FMT("ReceiveVsync dataCount: %ldbytes now: %ld expectedEnd: %ld vsyncId: %ld",
dataCount, now, expectedEnd, data[2]); // data[2] is vsyncId
RS_TRACE_NAME_FMT("ReceiveVsync dataCount: %ldbytes now: %ld expectedEnd: %ld vsyncId: %ld, fd:%d",
dataCount, now, expectedEnd, data[2], fileDescriptor); // data[2] is vsyncId
if (callbacks.empty() && dataCount > 0 && (cbWithId != nullptr || cb != nullptr)) {
// data[2] is frameCount
cbWithId != nullptr ? cbWithId(now, data[2], userData) : cb(now, userData);
@ -139,11 +153,15 @@ int64_t VSyncCallBackListener::CalculateExpectedEndLocked(int64_t now)
return expectedEnd;
}
void VSyncCallBackListener::CloseFd(int32_t fd)
void VSyncCallBackListener::SetFdClosedFlagLocked(bool fdClosed)
{
std::lock_guard<std::mutex> locker(fdMutex_);
close(fd);
fdClosed_ = true;
fdClosed_ = fdClosed;
}
void VSyncCallBackListener::RegisterFdShutDownCallback(FdShutDownCallback cb)
{
std::lock_guard<std::mutex> locker(cbMutex_);
fdShutDownCallback_ = cb;
}
VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
@ -193,6 +211,14 @@ VsyncError VSyncReceiver::Init()
}
listener_->SetName(name_);
listener_->RegisterFdShutDownCallback([this](int32_t fileDescriptor) {
std::lock_guard<std::mutex> locker(initMutex_);
if (fileDescriptor != fd_) {
VLOGE("OnShutdown Invalid fileDescriptor:%{public}d, fd_:%{public}d", fileDescriptor, fd_);
return;
}
RemoveAndCloseFdLocked();
});
looper_->AddFileDescriptorListener(fd_, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_, "vSyncTask");
init_ = true;
@ -216,11 +242,24 @@ void VSyncReceiver::ThreadCreateNotify()
VSyncReceiver::~VSyncReceiver()
{
if (fd_ != INVALID_FD) {
listener_->RegisterFdShutDownCallback(nullptr);
std::lock_guard<std::mutex> locker(initMutex_);
RemoveAndCloseFdLocked();
DestroyLocked();
}
void VSyncReceiver::RemoveAndCloseFdLocked()
{
if (looper_ != nullptr) {
looper_->RemoveFileDescriptorListener(fd_);
listener_->CloseFd(fd_);
VLOGI("%{public}s looper remove fd listener, fd=%{public}d", __func__, fd_);
}
std::lock_guard<std::mutex> locker(listener_->fdMutex_);
if (fd_ >= 0) {
close(fd_);
listener_->SetFdClosedFlagLocked(true);
fd_ = INVALID_FD;
Destroy();
}
}
@ -320,20 +359,11 @@ VsyncError VSyncReceiver::GetVSyncPeriodAndLastTimeStamp(int64_t &period, int64_
void VSyncReceiver::CloseVsyncReceiverFd()
{
std::lock_guard<std::mutex> locker(initMutex_);
if (looper_ != nullptr) {
looper_->RemoveFileDescriptorListener(fd_);
VLOGI("%{public}s looper remove fd listener, fd=%{public}d", __func__, fd_);
}
if (fd_ >= 0) {
close(fd_);
fd_ = INVALID_FD;
}
RemoveAndCloseFdLocked();
}
VsyncError VSyncReceiver::Destroy()
VsyncError VSyncReceiver::DestroyLocked()
{
std::lock_guard<std::mutex> locker(initMutex_);
if (connection_ == nullptr) {
return VSYNC_ERROR_API_FAILED;
}

View File

@ -139,16 +139,16 @@ int32_t LocalSocketPair::SendData(const void *vaddr, size_t size)
return -1;
}
ssize_t length = TEMP_FAILURE_RETRY(send(sendFd_, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL));
if (length < 0) {
if (length <= 0) {
int errnoRecord = errno;
ScopedBytrace func("SocketPair SendData failed, errno = " + std::to_string(errnoRecord) +
", sendFd_ = " + std::to_string(sendFd_) + ", receiveFd_ = " + std::to_string(receiveFd_) +
", length = " + std::to_string(length));
LOGD("%{public}s send failed:%{public}d, length = %{public}d", __func__, errnoRecord,
static_cast<int32_t>(length));
if (errnoRecord == EAGAIN) {
return ERRNO_EAGAIN;
} else {
LOGE("%{public}s send failed, errno:%{public}d, length:%{public}d, sendFd:%{public}d, receiveFd:%{public}d",
__func__, errnoRecord, static_cast<int32_t>(length), sendFd_, receiveFd_);
return ERRNO_OTHER;
}
}