| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  ThreadPool.hpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/06/30.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef CPU_INTHREADPOOL_H
 | 
					
						
							|  |  |  | #define CPU_INTHREADPOOL_H
 | 
					
						
							|  |  |  | #ifdef MNN_USE_THREAD_POOL
 | 
					
						
							|  |  |  | #include <condition_variable>
 | 
					
						
							|  |  |  | #include <functional>
 | 
					
						
							|  |  |  | #include <mutex>
 | 
					
						
							|  |  |  | #include <thread>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2019-07-19 17:09:09 +08:00
										 |  |  | #include <atomic>
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include <MNN/MNNDefine.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  | namespace MNN { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | class MNN_PUBLIC ThreadPool { | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  | public: | 
					
						
							|  |  |  |     typedef std::pair<std::function<void(int)>, int> TASK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int number() const { | 
					
						
							|  |  |  |         return mNumberThread; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-19 17:09:09 +08:00
										 |  |  |     static void enqueue(TASK&& task, int index); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static void active(); | 
					
						
							|  |  |  |     static void deactive(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static int acquireWorkIndex(); | 
					
						
							|  |  |  |     static void releaseWorkIndex(int index); | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 17:09:09 +08:00
										 |  |  |     static int init(int number); | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  |     static void destroy(); | 
					
						
							| 
									
										
										
										
											2019-07-19 17:09:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-07-19 17:09:09 +08:00
										 |  |  |     void enqueueInternal(TASK&& task, int index); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  |     static ThreadPool* gInstance; | 
					
						
							|  |  |  |     ThreadPool(int number = 0); | 
					
						
							|  |  |  |     ~ThreadPool(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 14:47:02 +08:00
										 |  |  |     std::vector<std::thread> mWorkers; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     std::vector<bool> mTaskAvailable; | 
					
						
							| 
									
										
										
										
											2020-11-04 14:47:02 +08:00
										 |  |  |     std::atomic<bool> mStop = {false}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |     std::vector<std::pair<TASK, std::vector<std::atomic_bool*>>> mTasks; | 
					
						
							| 
									
										
										
										
											2020-11-04 14:47:02 +08:00
										 |  |  |     std::condition_variable mCondition; | 
					
						
							|  |  |  |     std::mutex mQueueMutex; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-19 17:09:09 +08:00
										 |  |  |     int mNumberThread            = 0; | 
					
						
							|  |  |  |     std::atomic_int mActiveCount = {0}; | 
					
						
							| 
									
										
										
										
											2019-07-11 13:56:52 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | } // namespace MNN
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 |