1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Copyright 2020 Rockchip Electronics Co. LTD 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License"); 5*4882a593Smuzhiyun * you may not use this file except in compliance with the License. 6*4882a593Smuzhiyun * You may obtain a copy of the License at 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software 11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS, 12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*4882a593Smuzhiyun * See the License for the specific language governing permissions and 14*4882a593Smuzhiyun * limitations under the License. 15*4882a593Smuzhiyun * 16*4882a593Smuzhiyun * src author: <mediapipe-team@google.com> 17*4882a593Smuzhiyun * new author: modified by <rimon.xu@rock-chips.com> and <martin.cheng@rock-chips.com> 18*4882a593Smuzhiyun * date: 2020-03-19 19*4882a593Smuzhiyun * reference: https://github.com/google/mediapipe 20*4882a593Smuzhiyun */ 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun #ifndef SRC_RT_TASK_TASK_GRAPH_RTEXECUTOR_H_ 24*4882a593Smuzhiyun #define SRC_RT_TASK_TASK_GRAPH_RTEXECUTOR_H_ 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun #include "rt_header.h" 27*4882a593Smuzhiyun #include "RTThreadOptions.h" 28*4882a593Smuzhiyun #include "RTThreadPool.h" 29*4882a593Smuzhiyun #include "rt_metadata.h" 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun // Abstract base class for the task queue. 32*4882a593Smuzhiyun // NOTE: The task queue orders the ready tasks by their priorities. This 33*4882a593Smuzhiyun // enables the executor to run ready tasks in priority order. 34*4882a593Smuzhiyun class RTTaskQueue { 35*4882a593Smuzhiyun public: 36*4882a593Smuzhiyun virtual ~RTTaskQueue(); 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun // Runs the next ready task in the current thread. Should be invoked by the 39*4882a593Smuzhiyun // executor. This method should be called exactly as many times as AddTask 40*4882a593Smuzhiyun // was called on the executor. 41*4882a593Smuzhiyun virtual void runNextTask() = 0; 42*4882a593Smuzhiyun }; 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun // Abstract base class for the RTExecutor. 45*4882a593Smuzhiyun class RTExecutor { 46*4882a593Smuzhiyun public: 47*4882a593Smuzhiyun virtual ~RTExecutor(); 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun // A registered RTExecutor subclass must implement the static factory method 50*4882a593Smuzhiyun // Create. The RTExecutor subclass cannot be registered without it. 51*4882a593Smuzhiyun // 52*4882a593Smuzhiyun // static ::mediapipe::StatusOr<RTExecutor*> Create( 53*4882a593Smuzhiyun // const MediaPipeOptions& extendable_options); 54*4882a593Smuzhiyun // 55*4882a593Smuzhiyun // Create validates extendable_options, then calls the constructor, and 56*4882a593Smuzhiyun // returns the newly allocated RTExecutor object. 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun // The scheduler queue calls this method to tell the executor that it has 59*4882a593Smuzhiyun // a new task to run. The executor should use its execution mechanism to 60*4882a593Smuzhiyun // invoke taskQueue->runNextTask. 61*4882a593Smuzhiyun virtual void addTask(RTTaskQueue* taskQueue, INT32 threadId = 0) { 62*4882a593Smuzhiyun schedule([taskQueue] { taskQueue->runNextTask(); }, threadId); 63*4882a593Smuzhiyun } 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun // schedule the specified "task" for execution in this executor. 66*4882a593Smuzhiyun virtual void schedule(std::function<void()> task, INT32 threadId = 0) = 0; 67*4882a593Smuzhiyun virtual INT32 getNumThreads() const = 0; 68*4882a593Smuzhiyun }; 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun // A multithreaded executor based on a thread pool. 71*4882a593Smuzhiyun class RTThreadPoolExecutor : public RTExecutor { 72*4882a593Smuzhiyun public: 73*4882a593Smuzhiyun static RTExecutor* create(RtMetaData *extendOptions); 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun explicit RTThreadPoolExecutor(INT32 numThreads); 76*4882a593Smuzhiyun explicit RTThreadPoolExecutor(const RTThreadOptions& threadOptions, INT32 numThreads, 77*4882a593Smuzhiyun RTThreadPoolMode mode = RT_THREAD_POOL_RANDOM_MODE); 78*4882a593Smuzhiyun ~RTThreadPoolExecutor() override; 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun public: 81*4882a593Smuzhiyun void schedule(std::function<void()> task, INT32 threadId = 0) override; 82*4882a593Smuzhiyun getNumThreads()83*4882a593Smuzhiyun INT32 getNumThreads() const { return mThreadPool.getNumThreads(); } 84*4882a593Smuzhiyun // Returns the thread stack size (in bytes). getStackSize()85*4882a593Smuzhiyun size_t getStackSize() const { return mStackSize; } 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun private: 88*4882a593Smuzhiyun // Saves the value of the stack size option and starts the thread pool. 89*4882a593Smuzhiyun void start(); 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun RTThreadPool mThreadPool; 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun // Records the stack size in RTThreadOptions right before we call 94*4882a593Smuzhiyun // mThreadPool.startWorkers(). 95*4882a593Smuzhiyun // 96*4882a593Smuzhiyun // The actual stack size passed to pthread_attr_setstacksize() for the 97*4882a593Smuzhiyun // worker threads differs from the stack size we specified. It includes the 98*4882a593Smuzhiyun // guard size and space for thread-local storage. (See Thread::start() in 99*4882a593Smuzhiyun // thread/thread.cc.) So the unit tests check the stack size in 100*4882a593Smuzhiyun // RTThreadOptions, in addition to trying to recover the specified stack 101*4882a593Smuzhiyun // size from the stack size returned by pthread_getattr_np(), 102*4882a593Smuzhiyun // pthread_attr_getstacksize(), and pthread_attr_getguardsize(). 103*4882a593Smuzhiyun size_t mStackSize = 0; 104*4882a593Smuzhiyun }; 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun class RTSingleExecutor : public RTExecutor { 107*4882a593Smuzhiyun public: 108*4882a593Smuzhiyun static RTExecutor* create(void *extendOptions); 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun explicit RTSingleExecutor(const RTThreadOptions& threadOptions); 111*4882a593Smuzhiyun ~RTSingleExecutor() override; 112*4882a593Smuzhiyun void schedule(std::function<void()> task, INT32 threadId = 0) override; getNumThreads()113*4882a593Smuzhiyun INT32 getNumThreads() const { return 1; } 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun private: 116*4882a593Smuzhiyun RTThreadPool mThreadPool; 117*4882a593Smuzhiyun }; 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun #endif // SRC_RT_TASK_TASK_GRAPH_RTEXECUTOR_H_ 120