PHP 7.4.33
Preview: BufferedRuntimeExecutor.cpp Size: 1.52 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/react-native/ReactCommon/react/runtime/BufferedRuntimeExecutor.cpp
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include "BufferedRuntimeExecutor.h"

#include <algorithm>

namespace facebook::react {

BufferedRuntimeExecutor::BufferedRuntimeExecutor(
    RuntimeExecutor runtimeExecutor)
    : runtimeExecutor_(runtimeExecutor),
      isBufferingEnabled_(true),
      lastIndex_(0) {}

void BufferedRuntimeExecutor::execute(Work&& callback) {
  if (!isBufferingEnabled_) {
    // Fast path: Schedule directly to RuntimeExecutor, without locking
    runtimeExecutor_(std::move(callback));
    return;
  }

  /**
   * Note: std::mutex doesn't have a FIFO ordering.
   * To preserve the order of the buffered work, use a priority queue and
   * track the last known work index.
   */
  uint64_t newIndex = lastIndex_++;
  std::scoped_lock guard(lock_);
  if (isBufferingEnabled_) {
    queue_.push({.index_ = newIndex, .work_ = std::move(callback)});
    return;
  }

  // Force flush the queue to maintain the execution order.
  unsafeFlush();

  runtimeExecutor_(std::move(callback));
}

void BufferedRuntimeExecutor::flush() {
  std::scoped_lock guard(lock_);
  unsafeFlush();
  isBufferingEnabled_ = false;
}

void BufferedRuntimeExecutor::unsafeFlush() {
  while (queue_.size() > 0) {
    const BufferedWork& bufferedWork = queue_.top();
    Work work = std::move(bufferedWork.work_);
    runtimeExecutor_(std::move(work));
    queue_.pop();
  }
}

} // namespace facebook::react

Directory Contents

Dirs: 5 × Files: 16
Name Size Perms Modified Actions
hermes DIR
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
iostests DIR
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
platform DIR
- drwxr-xr-x 2025-03-28 11:04:41
Edit Download
tests DIR
- drwxr-xr-x 2025-03-28 11:04:39
Edit Download
442 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
858 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
856 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.52 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.23 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
846 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
727 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.89 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
800 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.64 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
2.47 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
24.44 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.78 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
197 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
14.94 KB lrw-r--r-- 2025-03-28 11:04:40
Edit Download
2.76 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).