00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_threadpool_h_
00029 #define _terimber_threadpool_h_
00030
00031 #include "base/map.h"
00032 #include "threadpool/thread.h"
00033
00034 BEGIN_TERIMBER_NAMESPACE
00035 #pragma pack(4)
00036
00039 class client_thread_info
00040 {
00041 public:
00043 client_thread_info() :
00044 _ident(0), _data(0), _client(0), _wasted_calls(0), _obj(0)
00045 {
00046 }
00047
00049 client_thread_info(thread* obj,
00050 size_t ident,
00051 void* data,
00052 terimber_thread_employer* client
00053 ) :
00054 _ident(ident), _data(data), _client(client), _wasted_calls(0), _obj(obj)
00055 {
00056 }
00057
00058 size_t _ident;
00059 void* _data;
00060 terimber_thread_employer* _client;
00061 size_t _wasted_calls;
00062 thread* _obj;
00063 };
00064
00068 class threadpool : public terimber_threadpool,
00069 public terimber_thread_employer
00070 {
00073 typedef base_map< size_t, client_thread_info > ident_info_node_t;
00076 typedef node_allocator< ident_info_node_t::_node > ident_info_map_all_t;
00079 typedef _map< size_t, client_thread_info, ident_info_map_all_t > ident_info_map_t;
00082 typedef map< terimber_thread_employer*, ident_info_map_t > client_info_map_t;
00085 typedef list< client_thread_info > queue_info_t;
00086
00087
00088 public:
00090 threadpool( size_t capacity,
00091 size_t deactivate_time_msec
00092 );
00094 ~threadpool();
00096 virtual
00097 bool
00098 borrow_thread( size_t ident,
00099 void* data,
00100 terimber_thread_employer* client,
00101 size_t stay_on_alert_time_msec
00102 );
00104 virtual
00105 bool
00106 borrow_from_range(size_t from,
00107 size_t to,
00108 void* data,
00109 terimber_thread_employer* client,
00110 size_t stay_on_alert_time_msec
00111 );
00114 virtual
00115 void
00116 revoke_client( terimber_thread_employer* client
00117 );
00119 virtual
00120 void
00121 doxray();
00123 bool on();
00125 void off();
00126
00127 protected:
00129 virtual
00130 bool
00131 v_has_job( size_t ident,
00132 void* user_data
00133 );
00135 virtual
00136 void
00137 v_do_job( size_t ident,
00138 void* user_data
00139 );
00140 private:
00142 bool
00143 _validate_client(size_t ident,
00144 terimber_thread_employer* client,
00145 client_info_map_t::iterator& it_client,
00146 ident_info_map_t::iterator& it_info
00147 );
00149 void
00150 _clean_up_clients(terimber_thread_employer* client
00151 );
00152 private:
00153 bool _on;
00154 size_t _capacity;
00155 size_t _thread_in_use;
00156 thread _housekeeper;
00157 thread_pool_t _thread_pool;
00158 queue_info_t _dispose_queue;
00159 client_info_map_t _clients_map;
00160 ident_info_map_all_t _map_allocator;
00161 mutex _clients_mtx;
00162 size_t _deactivate_time_msec;
00163 };
00164
00165 #pragma pack()
00166 END_TERIMBER_NAMESPACE
00167
00168 #endif