1 #ifndef THREADPOOL_H_INCLUDED 2 #define THREADPOOL_H_INCLUDED 3 4 #ifdef HAVE_POOL_OF_THREADS 5 /* Copyright (C) 2012 Monty Program Ab 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; version 2 of the License. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */ 19 20 #define MAX_THREAD_GROUPS 100000 21 22 /* Threadpool parameters */ 23 extern uint threadpool_min_threads; /* Minimum threads in pool */ 24 extern uint threadpool_idle_timeout; /* Shutdown idle worker threads after this timeout */ 25 extern uint threadpool_size; /* Number of parallel executing threads */ 26 extern uint threadpool_max_size; 27 extern uint threadpool_stall_limit; /* time interval in 10 ms units for stall checks*/ 28 extern uint threadpool_max_threads; /* Maximum threads in pool */ 29 extern uint threadpool_oversubscribe; /* Maximum active threads in group */ 30 extern uint threadpool_prio_kickup_timer; /* Time before low prio item gets prio boost */ 31 #ifdef _WIN32 32 extern uint threadpool_mode; /* Thread pool implementation , windows or generic */ 33 #define TP_MODE_WINDOWS 0 34 #define TP_MODE_GENERIC 1 35 #endif 36 37 38 struct TP_connection; 39 extern void tp_callback(TP_connection *c); 40 extern void tp_timeout_handler(TP_connection *c); 41 42 43 44 /* 45 Threadpool statistics 46 */ 47 struct TP_STATISTICS 48 { 49 /* Current number of worker thread. */ 50 Atomic_counter<uint32_t> num_worker_threads; 51 }; 52 53 extern TP_STATISTICS tp_stats; 54 55 56 /* Functions to set threadpool parameters */ 57 extern void tp_set_min_threads(uint val); 58 extern void tp_set_max_threads(uint val); 59 extern void tp_set_threadpool_size(uint val); 60 extern void tp_set_threadpool_stall_limit(uint val); 61 extern int tp_get_idle_thread_count(); 62 extern int tp_get_thread_count(); 63 64 /* Activate threadpool scheduler */ 65 extern void tp_scheduler(void); 66 67 extern int show_threadpool_idle_threads(THD *thd, SHOW_VAR *var, char *buff, 68 enum enum_var_type scope); 69 70 enum TP_PRIORITY { 71 TP_PRIORITY_HIGH, 72 TP_PRIORITY_LOW, 73 TP_PRIORITY_AUTO 74 }; 75 76 77 enum TP_STATE 78 { 79 TP_STATE_IDLE, 80 TP_STATE_RUNNING, 81 TP_STATE_PENDING 82 }; 83 84 /* 85 Connection structure, encapsulates THD + structures for asynchronous 86 IO and pool. 87 88 Platform specific parts are specified in subclasses called connection_t, 89 inside threadpool_win.cc and threadpool_unix.cc 90 */ 91 92 struct TP_connection 93 { 94 THD* thd; 95 CONNECT* connect; 96 TP_STATE state; 97 TP_PRIORITY priority; 98 TP_connection(CONNECT *c) : 99 thd(0), 100 connect(c), 101 state(TP_STATE_IDLE), 102 priority(TP_PRIORITY_HIGH) 103 {} 104 105 virtual ~TP_connection() 106 {}; 107 108 /* Initialize io structures windows threadpool, epoll etc */ 109 virtual int init() = 0; 110 111 virtual void set_io_timeout(int sec) = 0; 112 113 /* Read for the next client command (async) with specified timeout */ 114 virtual int start_io() = 0; 115 116 virtual void wait_begin(int type)= 0; 117 virtual void wait_end() = 0; 118 119 }; 120 121 122 struct TP_pool 123 { 124 virtual ~TP_pool(){}; 125 virtual int init()= 0; 126 virtual TP_connection *new_connection(CONNECT *)= 0; 127 virtual void add(TP_connection *c)= 0; 128 virtual int set_max_threads(uint){ return 0; } 129 virtual int set_min_threads(uint){ return 0; } 130 virtual int set_pool_size(uint){ return 0; } 131 virtual int set_idle_timeout(uint){ return 0; } 132 virtual int set_oversubscribe(uint){ return 0; } 133 virtual int set_stall_limit(uint){ return 0; } 134 virtual int get_thread_count() { return tp_stats.num_worker_threads; } 135 virtual int get_idle_thread_count(){ return 0; } 136 }; 137 138 #ifdef _WIN32 139 struct TP_pool_win:TP_pool 140 { 141 TP_pool_win(); 142 virtual int init(); 143 virtual ~TP_pool_win(); 144 virtual TP_connection *new_connection(CONNECT *c); 145 virtual void add(TP_connection *); 146 virtual int set_max_threads(uint); 147 virtual int set_min_threads(uint); 148 }; 149 #endif 150 151 struct TP_pool_generic :TP_pool 152 { 153 TP_pool_generic(); 154 ~TP_pool_generic(); 155 virtual int init(); 156 virtual TP_connection *new_connection(CONNECT *c); 157 virtual void add(TP_connection *); 158 virtual int set_pool_size(uint); 159 virtual int set_stall_limit(uint); 160 virtual int get_idle_thread_count(); 161 }; 162 163 #endif /* HAVE_POOL_OF_THREADS */ 164 #endif /* THREADPOOL_H_INCLUDED */ 165