fd onshutdown 5.0.1

Signed-off-by: shegangbin <shegangbin1@huawei.com>
This commit is contained in:
shegangbin 2024-11-07 10:43:14 +08:00
parent f3fefffeac
commit 9d547a2f08
3 changed files with 62 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__
@ -159,7 +165,8 @@ public:
virtual VsyncError SetUiDvsyncConfig(int32_t bufferCount);
virtual VsyncError RequestNextVSyncWithMultiCallback(FrameCallback callback);
private:
VsyncError Destroy();
VsyncError DestroyLocked();
void RemoveAndCloseFdLocked();
sptr<IVSyncConnection> connection_;
sptr<IRemoteObject> token_;
std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_;

View File

@ -37,6 +37,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.
@ -45,7 +46,16 @@ 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);
std::lock_guard<std::mutex> locker(cbMutex_);
if (fdShutDownCallback_ != nullptr) {
fdShutDownCallback_(fileDescriptor);
}
}
VsyncError VSyncCallBackListener::ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount)
@ -77,7 +87,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;
@ -103,8 +113,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);
@ -137,11 +147,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,
@ -178,6 +192,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();
});
if (looper_ == nullptr) {
std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("OS_VSyncThread");
@ -195,11 +217,24 @@ VsyncError VSyncReceiver::Init()
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();
}
}
@ -299,20 +334,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;
}
}