mirror of
https://gitee.com/openharmony/graphic_graphic_2d
synced 2024-11-27 09:10:54 +00:00
fix fd read after close
Signed-off-by: shegangbin <shegangbin1@huawei.com>
This commit is contained in:
parent
b255f5158e
commit
744fc9eb6f
@ -99,10 +99,13 @@ public:
|
||||
frameCallbacks_.push_back(cb);
|
||||
}
|
||||
|
||||
void CloseFd(int32_t fd);
|
||||
|
||||
private:
|
||||
void OnReadable(int32_t fileDescriptor) override;
|
||||
int64_t CalculateExpectedEndLocked(int64_t now);
|
||||
void HandleVsyncCallbacks(int64_t data[], ssize_t dataCount);
|
||||
VsyncError ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount);
|
||||
VSyncCallback vsyncCallbacks_;
|
||||
VSyncCallbackWithId vsyncCallbacksWithId_;
|
||||
void *userData_;
|
||||
@ -114,6 +117,8 @@ 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;
|
||||
};
|
||||
|
||||
#ifdef __OHOS__
|
||||
|
@ -15,6 +15,7 @@
|
||||
|
||||
#include "vsync_receiver.h"
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unistd.h>
|
||||
#include <scoped_bytrace.h>
|
||||
#include <fcntl.h>
|
||||
@ -42,13 +43,25 @@ void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
|
||||
}
|
||||
// 3 is array size.
|
||||
int64_t data[3];
|
||||
ssize_t ret = 0;
|
||||
ssize_t dataCount = 0;
|
||||
if (ReadFdInternal(fileDescriptor, data, dataCount) != VSYNC_ERROR_OK) {
|
||||
return;
|
||||
}
|
||||
HandleVsyncCallbacks(data, dataCount);
|
||||
}
|
||||
|
||||
VsyncError VSyncCallBackListener::ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(fdMutex_);
|
||||
if (fdClosed_) {
|
||||
return VSYNC_ERROR_API_FAILED;
|
||||
}
|
||||
ssize_t ret = 0;
|
||||
do {
|
||||
// only take the latest timestamp
|
||||
ret = read(fileDescriptor, data, sizeof(data));
|
||||
ret = read(fd, data, sizeof(data));
|
||||
if (ret == 0) {
|
||||
return;
|
||||
return VSYNC_ERROR_OK;
|
||||
}
|
||||
if (ret == -1) {
|
||||
if (errno == EINTR) {
|
||||
@ -60,7 +73,7 @@ void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
|
||||
}
|
||||
} while (ret != -1);
|
||||
|
||||
HandleVsyncCallbacks(data, dataCount);
|
||||
return VSYNC_ERROR_OK;
|
||||
}
|
||||
|
||||
void VSyncCallBackListener::HandleVsyncCallbacks(int64_t data[], ssize_t dataCount)
|
||||
@ -122,6 +135,13 @@ int64_t VSyncCallBackListener::CalculateExpectedEndLocked(int64_t now)
|
||||
return expectedEnd;
|
||||
}
|
||||
|
||||
void VSyncCallBackListener::CloseFd(int32_t fd)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(fdMutex_);
|
||||
close(fd);
|
||||
fdClosed_ = true;
|
||||
}
|
||||
|
||||
VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
|
||||
const sptr<IRemoteObject>& token,
|
||||
const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper,
|
||||
@ -191,7 +211,7 @@ VSyncReceiver::~VSyncReceiver()
|
||||
{
|
||||
if (fd_ != INVALID_FD) {
|
||||
looper_->RemoveFileDescriptorListener(fd_);
|
||||
close(fd_);
|
||||
listener_->CloseFd(fd_);
|
||||
fd_ = INVALID_FD;
|
||||
Destroy();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user