chromium/third_party/pthreadpool/chromium/jobs.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// `_Static_assert` is used by threadpool-common.h for GCC build. Because
// `_Static_assert` is not defined in GCC C++ backend, define `_Static_assert`
// by `static_assert` as a work-around.
#if defined(__GNUC__) && \
    ((__GNUC__ > 4) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))
#define _Static_assert(predicate, message) static_assert(predicate, message)
#endif

// Configuration header.
#include "threadpool-common.h"

// Public library header.
#include <pthreadpool.h>

#if defined(_MSC_VER) && defined(_M_ARM64)
#include <arm64intr.h>

// The following ARM64 intrinsics are used by threadpool-atomics.h and should be
// defined in arm64intr.h. If arm64intr.h is not included correctly, e.g. due to
// LLVM issue: https://github.com/llvm/llvm-project/issues/62942, declare them
// here as a work-around.
//
// TODO(crbug.com/1228275): Remove this work-around once the LLVM issue is
// fixed.
#ifndef ARM64_FPCR
extern "C" {
unsigned __int32 __ldar32(unsigned __int32 volatile* _Target);
unsigned __int64 __ldar64(unsigned __int64 volatile* _Target);
void __stlr32(unsigned __int32 volatile* _Target, unsigned __int32 _Value);
void __stlr64(unsigned __int64 volatile* _Target, unsigned __int64 _Value);
}
#endif
#endif  // defined(_MSC_VER) && defined(_M_ARM64)

// Internal library headers.
#include "threadpool-atomics.h"
#include "threadpool-utils.h"

// Redeclare the following three pthreadpool internal functions with extern "C"
// linkage that are used by this C++ implementation.
#define pthreadpool_allocate
#define pthreadpool_deallocate
#define pthreadpool_parallelize

#include "threadpool-object.h"

#undef pthreadpool_allocate
#undef pthreadpool_deallocate
#undef pthreadpool_parallelize

extern "C" {
PTHREADPOOL_INTERNAL struct pthreadpool* pthreadpool_allocate(
    size_t threads_count);

PTHREADPOOL_INTERNAL void pthreadpool_deallocate(
    struct pthreadpool* threadpool);

PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
    struct pthreadpool* threadpool,
    thread_function_t thread_function,
    const void* params,
    size_t params_size,
    void* task,
    void* context,
    size_t linear_range,
    uint32_t flags);
}

// Chromium headers.
#include "base/functional/bind.h"
#include "base/synchronization/lock.h"
#include "base/system/sys_info.h"
#include "base/task/post_job.h"
#include "base/task/task_traits.h"

// According to the tests on multiple systems, there will be a performance
// regression of XNNPACK model inference when the number of work items is
// greater than `kMaxNumWorkItems`.
//
// TODO(crbug.com/1228275): Ensure `kMaxNumWorkItems` value setting makes sense.
constexpr size_t kMaxNumWorkItems =;

// Processes `threadpool` which represents the parallel computation task
// dispatched by `pthreadpool_parallelize()` in `num_work_items` items with
// `Run()` from `base::PostJob`. `GetMaxConcurrency()` is a callback used in
// `base::PostJob()` to control the maximum number of threads calling `Run()`
// concurrently.
class PthreadPoolJob {};

struct pthreadpool* pthreadpool_create(size_t threads_count) {}

// The `threadpool` struct is accessed by this method without holding a lock.
// The caller should ensure accessing a `threadpool` struct from the same
// sequence that inherently provides thread-safety.
PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
    struct pthreadpool* threadpool,
    thread_function_t thread_function,
    const void* params,
    size_t params_size,
    void* task,
    void* context,
    size_t linear_range,
    uint32_t flags) {}

void pthreadpool_destroy(struct pthreadpool* threadpool) {}