Merge branch 'master' of gitee.com:openharmony/resourceschedule_ffrt into master

Signed-off-by: 董洁 <dongjie52@h-partners.com>
This commit is contained in:
董洁 2024-11-14 01:45:02 +00:00 committed by Gitee
commit 2dc0c73cc5
5 changed files with 40 additions and 7 deletions

View File

@ -52,7 +52,6 @@ SDependenceManager::SDependenceManager() : criticalMutex_(Entity::Instance()->cr
_FinishTrace(HITRACE_TAG_FFRT);
#endif
QueueMonitor::GetInstance();
GetIOPoller();
DelayedWorker::GetInstance();
}

View File

@ -29,6 +29,7 @@ constexpr unsigned int DEFAULT_CPUINDEX_LIMIT = 7;
struct IOPollerInstance: public IOPoller {
IOPollerInstance() noexcept: m_runner([&] { RunForever(); })
{
DependenceManager::Instance();
pthread_setname_np(m_runner.native_handle(), IO_POLLER_NAME);
}

View File

@ -84,12 +84,26 @@ int Poller::AddFdEvent(int op, uint32_t events, int fd, void* data, ffrt_poller_
int Poller::DelFdEvent(int fd) noexcept
{
std::unique_lock lock(m_mapMutex);
if (epoll_ctl(m_epFd, EPOLL_CTL_DEL, fd, nullptr) != 0) {
FFRT_LOGE("epoll_ctl del fd error: efd=%d, fd=%d, errorno=%d", m_epFd, fd, errno);
return -1;
}
std::unique_lock lock(m_mapMutex);
for (auto it = m_cachedTaskEvents.begin(); it != m_cachedTaskEvents.end();) {
auto& events = it->second;
events.erase(std::remove_if(events.begin(), events.end(),
[fd](const epoll_event& event) {
return event.data.fd == fd;
}), events.end());
if (events.empty()) {
it = m_cachedTaskEvents.erase(it);
} else {
++it;
}
}
m_delCntMap[fd]++;
WakeUp();
return 0;
@ -193,7 +207,7 @@ int Poller::WaitFdEvent(struct epoll_event* eventsVec, int maxevents, int timeou
m_waitTaskMap[task] = {static_cast<void*>(eventsVec), maxevents, &nfds, currTime};
if (timeout > -1) {
FFRT_LOGD("poller meet timeout={%d}", timeout);
RegisterTimer(timeout, nullptr, nullptr);
m_waitTaskMap[task].timerHandle = RegisterTimer(timeout, nullptr, nullptr);
}
m_mapMutex.unlock();
reinterpret_cast<SCPUEUTask*>(task)->waitCond_.wait(lck);
@ -221,7 +235,7 @@ int Poller::WaitFdEvent(struct epoll_event* eventsVec, int maxevents, int timeou
m_waitTaskMap[task] = {static_cast<void*>(eventsVec), maxevents, &nfds, currTime};
if (timeout > -1) {
FFRT_LOGD("poller meet timeout={%d}", timeout);
RegisterTimer(timeout, nullptr, nullptr);
m_waitTaskMap[task].timerHandle = RegisterTimer(timeout, nullptr, nullptr);
}
m_mapMutex.unlock();
// The ownership of the task belongs to m_waitTaskMap, and the task cannot be accessed any more.
@ -320,6 +334,8 @@ void Poller::WakeSyncTask(std::unordered_map<CPUEUTask*, EventVec>& syncTaskEven
return;
}
std::unordered_set<int> timerHandlesToRemove;
std::unordered_set<CPUEUTask*> tasksToWake;
m_mapMutex.lock();
for (auto& taskEventPair : syncTaskEvents) {
CPUEUTask* currTask = taskEventPair.first;
@ -330,11 +346,28 @@ void Poller::WakeSyncTask(std::unordered_map<CPUEUTask*, EventVec>& syncTaskEven
}
CopyEventsInfoToConsumer(iter->second, taskEventPair.second);
auto timerHandle = iter->second.timerHandle;
if (timerHandle > -1) {
timerHandlesToRemove.insert(timerHandle);
}
tasksToWake.insert(currTask);
m_waitTaskMap.erase(iter);
WakeTask(currTask);
}
m_mapMutex.unlock();
if (timerHandlesToRemove.size() > 0) {
std::lock_guard lock(timerMutex_);
for (auto cur = timerMap_.begin(); cur != timerMap_.end(); cur++) {
if (timerHandlesToRemove.find(cur->second.handle) != timerHandlesToRemove.end()) {
timerMap_.erase(cur);
break;
}
}
timerEmpty_.store(timerMap_.empty());
}
for (auto task : tasksToWake) {
WakeTask(task);
}
}
uint64_t Poller::GetTaskWaitTime(CPUEUTask* task) noexcept

View File

@ -86,6 +86,7 @@ struct SyncData {
int maxEvents = 0;
int* nfdsPtr = nullptr;
TimePoint waitTP;
int timerHandle = -1;
};
using EventVec = typename std::vector<epoll_event>;

View File

@ -60,7 +60,6 @@ public:
// NOTE: call destructor before FreeMem
static void FreeMem(T* t)
{
t->~T();
// unlock()内部lck记录锁的状态为非持有状态析构时访问状态变量为非持有状态则不访问实际持有的mutex
// return之前的lck析构不产生UAF问题因为return之前随着root析构锁的内存被释放
Instance()->free(t);