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