xref: /aosp_15_r20/external/eigen/unsupported/Eigen/CXX11/src/ThreadPool/ThreadPoolInterface.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <[email protected]>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_THREADPOOL_THREAD_POOL_INTERFACE_H
11 #define EIGEN_CXX11_THREADPOOL_THREAD_POOL_INTERFACE_H
12 
13 namespace Eigen {
14 
15 // This defines an interface that ThreadPoolDevice can take to use
16 // custom thread pools underneath.
17 class ThreadPoolInterface {
18  public:
19   // Submits a closure to be run by a thread in the pool.
20   virtual void Schedule(std::function<void()> fn) = 0;
21 
22   // Submits a closure to be run by threads in the range [start, end) in the
23   // pool.
ScheduleWithHint(std::function<void ()> fn,int,int)24   virtual void ScheduleWithHint(std::function<void()> fn, int /*start*/,
25                                 int /*end*/) {
26     // Just defer to Schedule in case sub-classes aren't interested in
27     // overriding this functionality.
28     Schedule(fn);
29   }
30 
31   // If implemented, stop processing the closures that have been enqueued.
32   // Currently running closures may still be processed.
33   // If not implemented, does nothing.
Cancel()34   virtual void Cancel() {}
35 
36   // Returns the number of threads in the pool.
37   virtual int NumThreads() const = 0;
38 
39   // Returns a logical thread index between 0 and NumThreads() - 1 if called
40   // from one of the threads in the pool. Returns -1 otherwise.
41   virtual int CurrentThreadId() const = 0;
42 
~ThreadPoolInterface()43   virtual ~ThreadPoolInterface() {}
44 };
45 
46 }  // namespace Eigen
47 
48 #endif  // EIGEN_CXX11_THREADPOOL_THREAD_POOL_INTERFACE_H
49