Commit e69ead5b authored by Simon Giesecke's avatar Simon Giesecke
Browse files

Bug 1663924 - Use std::find_if in ConnectionPool::IdleTimerCallback....

Bug 1663924 - Use std::find_if in ConnectionPool::IdleTimerCallback. r=dom-workers-and-storage-reviewers,ttung

Differential Revision: https://phabricator.services.mozilla.com/D90267
parent d7d69265
Loading
Loading
Loading
Loading
+31 −34
Original line number Original line Diff line number Diff line
@@ -8238,60 +8238,57 @@ ConnectionPool::~ConnectionPool() {
// static
// static
void ConnectionPool::IdleTimerCallback(nsITimer* aTimer, void* aClosure) {
void ConnectionPool::IdleTimerCallback(nsITimer* aTimer, void* aClosure) {
  MOZ_ASSERT(aTimer);
  MOZ_ASSERT(aTimer);
  MOZ_ASSERT(aClosure);
  AUTO_PROFILER_LABEL("ConnectionPool::IdleTimerCallback", DOM);
  AUTO_PROFILER_LABEL("ConnectionPool::IdleTimerCallback", DOM);
  auto* self = static_cast<ConnectionPool*>(aClosure);
  auto& self = *static_cast<ConnectionPool*>(aClosure);
  MOZ_ASSERT(self);
  MOZ_ASSERT(self.mIdleTimer);
  MOZ_ASSERT(self->mIdleTimer);
  MOZ_ASSERT(SameCOMIdentity(self.mIdleTimer, aTimer));
  MOZ_ASSERT(SameCOMIdentity(self->mIdleTimer, aTimer));
  MOZ_ASSERT(!self.mTargetIdleTime.IsNull());
  MOZ_ASSERT(!self->mTargetIdleTime.IsNull());
  MOZ_ASSERT_IF(self.mIdleDatabases.IsEmpty(), !self.mIdleThreads.IsEmpty());
  MOZ_ASSERT_IF(self->mIdleDatabases.IsEmpty(), !self->mIdleThreads.IsEmpty());
  MOZ_ASSERT_IF(self.mIdleThreads.IsEmpty(), !self.mIdleDatabases.IsEmpty());
  MOZ_ASSERT_IF(self->mIdleThreads.IsEmpty(), !self->mIdleDatabases.IsEmpty());
  self->mTargetIdleTime = TimeStamp();
  self.mTargetIdleTime = TimeStamp();
  // Cheat a little.
  // Cheat a little.
  TimeStamp now = TimeStamp::NowLoRes() + TimeDuration::FromMilliseconds(500);
  const TimeStamp now =
      TimeStamp::NowLoRes() + TimeDuration::FromMilliseconds(500);
  uint32_t index = 0;
  // XXX Move this to ArrayAlgorithm.h?
  for (uint32_t count = self->mIdleDatabases.Length(); index < count; index++) {
  const auto removeUntil = [](auto& array, auto&& cond) {
    IdleDatabaseInfo& info = self->mIdleDatabases[index];
    const auto begin = array.begin(), end = array.end();
    array.RemoveElementsRange(
        begin, std::find_if(begin, end, std::forward<decltype(cond)>(cond)));
  };
  removeUntil(self.mIdleDatabases, [now, &self](const auto& info) {
    if (now >= info.mIdleTime) {
    if (now >= info.mIdleTime) {
      if ((*info.mDatabaseInfo)->mIdle) {
      if ((*info.mDatabaseInfo)->mIdle) {
        self->PerformIdleDatabaseMaintenance(*info.mDatabaseInfo.ref());
        self.PerformIdleDatabaseMaintenance(*info.mDatabaseInfo.ref());
      } else {
        self->CloseDatabase(*info.mDatabaseInfo.ref());
      }
      } else {
      } else {
      break;
        self.CloseDatabase(*info.mDatabaseInfo.ref());
    }
      }
      }
  if (index) {
      return false;
    self->mIdleDatabases.RemoveElementsAt(0, index);
    index = 0;
    }
    }
  for (uint32_t count = self->mIdleThreads.Length(); index < count; index++) {
    return true;
    IdleThreadInfo& info = self->mIdleThreads[index];
  });
  removeUntil(self.mIdleThreads, [now, &self](auto& info) {
    info.mThreadInfo.AssertValid();
    info.mThreadInfo.AssertValid();
    if (now >= info.mIdleTime) {
    if (now >= info.mIdleTime) {
      self->ShutdownThread(std::move(info.mThreadInfo));
      self.ShutdownThread(std::move(info.mThreadInfo));
    } else {
      break;
    }
  }
  if (index) {
      return false;
    self->mIdleThreads.RemoveElementsAt(0, index);
    }
    }
  self->AdjustIdleTimer();
    return true;
  });
  self.AdjustIdleTimer();
}
}
Result<RefPtr<DatabaseConnection>, nsresult>
Result<RefPtr<DatabaseConnection>, nsresult>