tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fake_parallel_runner_testonly.h (2597B)


      1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style
      4 // license that can be found in the LICENSE file.
      5 
      6 #ifndef LIB_JXL_FAKE_PARALLEL_RUNNER_TESTONLY_H_
      7 #define LIB_JXL_FAKE_PARALLEL_RUNNER_TESTONLY_H_
      8 
      9 #include <jxl/parallel_runner.h>
     10 
     11 #include <cstdint>
     12 #include <vector>
     13 
     14 #include "lib/jxl/base/compiler_specific.h"
     15 #include "lib/jxl/base/random.h"
     16 
     17 namespace jxl {
     18 
     19 // A parallel runner implementation that runs all the jobs in a single thread
     20 // (the caller thread) but runs them pretending to use multiple threads and
     21 // potentially out of order. This is useful for testing conditions that only
     22 // occur under heavy load where the order of operations is different.
     23 class FakeParallelRunner {
     24 public:
     25  FakeParallelRunner(uint32_t order_seed, uint32_t num_threads)
     26      : order_seed_(order_seed), rng_(order_seed), num_threads_(num_threads) {
     27    if (num_threads_ < 1) num_threads_ = 1;
     28  }
     29 
     30  JxlParallelRetCode Run(void* jxl_opaque, JxlParallelRunInit init,
     31                         JxlParallelRunFunction func, uint32_t start,
     32                         uint32_t end) {
     33    JxlParallelRetCode ret = init(jxl_opaque, num_threads_);
     34    if (ret != 0) return ret;
     35 
     36    if (order_seed_ == 0) {
     37      for (uint32_t i = start; i < end; i++) {
     38        func(jxl_opaque, i, i % num_threads_);
     39      }
     40    } else {
     41      std::vector<uint32_t> order(end - start);
     42      for (uint32_t i = start; i < end; i++) {
     43        order[i - start] = i;
     44      }
     45      rng_.Shuffle(order.data(), order.size());
     46      for (uint32_t i = start; i < end; i++) {
     47        func(jxl_opaque, order[i - start], i % num_threads_);
     48      }
     49    }
     50    return ret;
     51  }
     52 
     53 private:
     54  // Seed for the RNG for defining the execution order. A value of 0 means
     55  // sequential order from start to end.
     56  uint32_t order_seed_;
     57 
     58  // The PRNG object, initialized with the order_seed_. Only used if the seed is
     59  // not 0.
     60  Rng rng_;
     61 
     62  // Number of fake threads. All the tasks are run on the same thread, but using
     63  // different thread_id values based on this num_threads.
     64  uint32_t num_threads_;
     65 };
     66 
     67 }  // namespace jxl
     68 
     69 extern "C" {
     70 // Function to pass as the parallel runner.
     71 JXL_INLINE JxlParallelRetCode JxlFakeParallelRunner(
     72    void* runner_opaque, void* jpegxl_opaque, JxlParallelRunInit init,
     73    JxlParallelRunFunction func, uint32_t start_range, uint32_t end_range) {
     74  return static_cast<jxl::FakeParallelRunner*>(runner_opaque)
     75      ->Run(jpegxl_opaque, init, func, start_range, end_range);
     76 }
     77 }
     78 
     79 #endif  // LIB_JXL_FAKE_PARALLEL_RUNNER_TESTONLY_H_