1 /* 2 * thread_pool.h - Thread Pool 3 * 4 * Copyright (c) 2017 Intel Corporation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * Author: Wind Yuan <feng.yuan@intel.com> 19 */ 20 21 #ifndef XCAM_THREAD_POOL_H 22 #define XCAM_THREAD_POOL_H 23 24 #include <xcam_std.h> 25 #include <safe_list.h> 26 #include <xcam_thread.h> 27 28 namespace XCam { 29 30 class UserThread; 31 32 class ThreadPool 33 : public RefObj 34 { 35 friend class UserThread; 36 typedef std::list<SmartPtr<UserThread> > UserThreadList; 37 38 public: 39 class UserData { 40 public: UserData()41 UserData () {} ~UserData()42 virtual ~UserData () {} 43 virtual XCamReturn run () = 0; done(XCamReturn)44 virtual void done (XCamReturn) {} 45 private: 46 XCAM_DEAD_COPY (UserData); 47 }; 48 49 public: 50 explicit ThreadPool (const char *name); 51 virtual ~ThreadPool (); 52 bool set_threads (uint32_t min, uint32_t max); get_name()53 const char *get_name () const { 54 return _name; 55 } 56 bool is_running (); 57 58 XCamReturn start (); 59 XCamReturn stop (); 60 XCamReturn queue (const SmartPtr<UserData> &data); 61 62 protected: 63 bool dispatch (const SmartPtr<UserData> &data); 64 XCamReturn create_user_thread_unsafe (); 65 66 private: 67 XCAM_DEAD_COPY (ThreadPool); 68 69 private: 70 char *_name; 71 uint32_t _min_threads; 72 uint32_t _max_threads; 73 uint32_t _allocated_threads; 74 uint32_t _free_threads; 75 bool _running; 76 UserThreadList _thread_list; 77 Mutex _mutex; 78 79 SafeList<UserData> _data_queue; 80 }; 81 82 } 83 84 #endif // XCAM_THREAD_POOL_H 85