1 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22
23 /*
24 * sql_manager.cc
25 * This thread manages various maintenance tasks.
26 *
27 * o Flushing the tables every flush_time seconds.
28 */
29
30 #include "sql_manager.h"
31
32 #include "my_thread.h" // my_thread_t
33 #include "log.h" // sql_print_warning
34 #include "sql_base.h" // tdc_flush_unused_tables
35
36 static bool volatile manager_thread_in_use;
37 static bool abort_manager;
38
39 my_thread_t manager_thread;
40 mysql_mutex_t LOCK_manager;
41 mysql_cond_t COND_manager;
42
handle_manager(void * arg MY_ATTRIBUTE ((unused)))43 extern "C" void *handle_manager(void *arg MY_ATTRIBUTE((unused)))
44 {
45 int error = 0;
46 struct timespec abstime;
47 bool reset_flush_time = TRUE;
48 my_thread_init();
49 DBUG_ENTER("handle_manager");
50
51 manager_thread= my_thread_self();
52 manager_thread_in_use = 1;
53
54 for (;;)
55 {
56 mysql_mutex_lock(&LOCK_manager);
57 /* XXX: This will need to be made more general to handle different
58 * polling needs. */
59 if (flush_time)
60 {
61 if (reset_flush_time)
62 {
63 set_timespec(&abstime, flush_time);
64 reset_flush_time = FALSE;
65 }
66 while ((!error || error == EINTR) && !abort_manager)
67 error= mysql_cond_timedwait(&COND_manager, &LOCK_manager, &abstime);
68 }
69 else
70 {
71 while ((!error || error == EINTR) && !abort_manager)
72 error= mysql_cond_wait(&COND_manager, &LOCK_manager);
73 }
74 mysql_mutex_unlock(&LOCK_manager);
75
76 if (abort_manager)
77 break;
78
79 if (error == ETIMEDOUT || error == ETIME)
80 {
81 tdc_flush_unused_tables();
82 error = 0;
83 reset_flush_time = TRUE;
84 }
85
86 }
87 manager_thread_in_use = 0;
88 DBUG_LEAVE; // Can't use DBUG_RETURN after my_thread_end
89 my_thread_end();
90 return (NULL);
91 }
92
93
94 /* Start handle manager thread */
start_handle_manager()95 void start_handle_manager()
96 {
97 DBUG_ENTER("start_handle_manager");
98 abort_manager = false;
99 if (flush_time && flush_time != ~(ulong) 0L)
100 {
101 my_thread_handle hThread;
102 int error;
103 if ((error= mysql_thread_create(key_thread_handle_manager,
104 &hThread, &connection_attrib,
105 handle_manager, 0)))
106 sql_print_warning("Can't create handle_manager thread (errno= %d)",
107 error);
108 }
109 DBUG_VOID_RETURN;
110 }
111
112
113 /* Initiate shutdown of handle manager thread */
stop_handle_manager()114 void stop_handle_manager()
115 {
116 DBUG_ENTER("stop_handle_manager");
117 abort_manager = true;
118 mysql_mutex_lock(&LOCK_manager);
119 if (manager_thread_in_use)
120 {
121 DBUG_PRINT("quit", ("initiate shutdown of handle manager thread: 0x%lx",
122 (ulong)manager_thread));
123 mysql_cond_signal(&COND_manager);
124 }
125 mysql_mutex_unlock(&LOCK_manager);
126 DBUG_VOID_RETURN;
127 }
128
129