1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CORE_MINDRT_RUNTIME_ACTOR_THREADPOOL_H_ 18 #define MINDSPORE_CORE_MINDRT_RUNTIME_ACTOR_THREADPOOL_H_ 19 20 #include <queue> 21 #include <vector> 22 #include <mutex> 23 #include <atomic> 24 #include <condition_variable> 25 #include "thread/threadpool.h" 26 #include "thread/core_affinity.h" 27 #include "actor/actor.h" 28 #include "thread/hqueue.h" 29 #define USE_HQUEUE 30 namespace mindspore { 31 class ActorThreadPool; 32 33 class ActorWorker : public Worker { 34 public: 35 void CreateThread(ActorThreadPool *pool); 36 bool ActorActive(); 37 38 private: 39 void RunWithSpin(); 40 bool RunQueueActorTask(); 41 42 ActorThreadPool *pool_{nullptr}; 43 }; 44 45 class ActorThreadPool : public ThreadPool { 46 public: 47 // create ThreadPool that contains actor thread and kernel thread 48 static ActorThreadPool *CreateThreadPool(size_t actor_thread_num, size_t all_thread_num, BindMode bind_mode); 49 50 static ActorThreadPool *CreateThreadPool(size_t actor_thread_num, size_t all_thread_num, 51 const std::vector<int> &core_list); 52 // create ThreadPool that contains only actor thread 53 static ActorThreadPool *CreateThreadPool(size_t thread_num); 54 ~ActorThreadPool() override; 55 56 void PushActorToQueue(ActorBase *actor); 57 ActorBase *PopActorFromQueue(); 58 59 private: ActorThreadPool()60 ActorThreadPool() {} 61 int CreateThreads(size_t actor_thread_num, size_t all_thread_num, const std::vector<int> &core_list); 62 size_t actor_thread_num_{0}; 63 64 std::mutex actor_mutex_; 65 std::condition_variable actor_cond_; 66 #ifdef USE_HQUEUE 67 HQueue<ActorBase> actor_queue_; 68 #else 69 std::queue<ActorBase *> actor_queue_; 70 #endif 71 }; 72 } // namespace mindspore 73 #endif // MINDSPORE_CORE_MINDRT_RUNTIME_ACTOR_THREADPOOL_H_ 74