1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef THREAD_POOL_H 17 #define THREAD_POOL_H 18 19 #include <list> 20 #include <mutex> 21 22 #include "platform/threadpool/include/thread.h" 23 #include "utils/log/aie_log.h" 24 25 namespace OHOS { 26 namespace AI { 27 const int THREAD_STOP_TIMEOUT = 3600 * 24 * 1000; 28 const int THREAD_SLEEP_TIME = 10; 29 30 class ThreadPool { 31 FORBID_COPY_AND_ASSIGN(ThreadPool); 32 FORBID_CREATE_BY_SELF(ThreadPool); 33 34 typedef std::list<std::shared_ptr<Thread>> Threads; 35 public: 36 static ThreadPool *GetInstance(); 37 38 static void ReleaseInstance(); 39 40 /** 41 * get a Thread from thread pool 42 * 43 * @return thread 44 */ 45 std::shared_ptr<Thread> Pop(); 46 47 /** 48 * push a to thread to thread pool 49 */ 50 void Push(std::shared_ptr<Thread>& thread); 51 52 /** 53 * attention must set before start thread 54 * 55 * @param stack size: 16384B ~ 8192KB, 56 * SetStackSize(0) will set the default stack size 57 */ 58 void SetStackSize(size_t size); 59 60 int getStackSize(); 61 62 private: 63 bool StopThreads(int32_t timeOut); 64 void StopThreads(); 65 66 private: 67 static std::mutex mutex_; 68 static ThreadPool *instance_; 69 size_t stackSize_; 70 std::mutex mutex4Inner_; 71 Threads busyThreads_; 72 Threads idleThreads_; 73 }; 74 } // namespace AI 75 } // namespace OHOS 76 77 #endif // THREAD_POOL_H