1 /* Copyright (c) 2000, 2013, 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 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 St, Fifth Floor, Boston, MA 02110-1335  USA */
15 
16 /*
17  * sql_manager.cc
18  * This thread manages various maintenance tasks.
19  *
20  *   o Flushing the tables every flush_time seconds.
21  *   o Berkeley DB: removing unneeded log files.
22  */
23 
24 #include "mariadb.h"
25 #include "sql_priv.h"
26 #include "sql_manager.h"
27 #include "sql_base.h"                           // flush_tables
28 
29 static bool volatile manager_thread_in_use = 0;
30 static bool abort_manager = false;
31 
32 pthread_t manager_thread;
33 mysql_mutex_t LOCK_manager;
34 mysql_cond_t COND_manager;
35 
36 struct handler_cb {
37    struct handler_cb *next;
38    void (*action)(void *);
39    void *data;
40 };
41 
42 static struct handler_cb *cb_list; // protected by LOCK_manager
43 
mysql_manager_submit(void (* action)(void *),void * data)44 bool mysql_manager_submit(void (*action)(void *), void *data)
45 {
46   bool result= FALSE;
47   DBUG_ASSERT(manager_thread_in_use);
48   struct handler_cb **cb;
49   mysql_mutex_lock(&LOCK_manager);
50   cb= &cb_list;
51   while (*cb)
52     cb= &(*cb)->next;
53   *cb= (struct handler_cb *)my_malloc(PSI_INSTRUMENT_ME,
54                                       sizeof(struct handler_cb), MYF(MY_WME));
55   if (!*cb)
56     result= TRUE;
57   else
58   {
59     (*cb)->next= NULL;
60     (*cb)->action= action;
61     (*cb)->data= data;
62   }
63   mysql_cond_signal(&COND_manager);
64   mysql_mutex_unlock(&LOCK_manager);
65   return result;
66 }
67 
handle_manager(void * arg)68 pthread_handler_t handle_manager(void *arg __attribute__((unused)))
69 {
70   int error = 0;
71   struct timespec abstime;
72   bool reset_flush_time = TRUE;
73   my_thread_init();
74   DBUG_ENTER("handle_manager");
75 
76   pthread_detach_this_thread();
77   manager_thread = pthread_self();
78   mysql_mutex_lock(&LOCK_manager);
79   while (!abort_manager)
80   {
81     /* XXX: This will need to be made more general to handle different
82      * polling needs. */
83     if (flush_time)
84     {
85       if (reset_flush_time)
86       {
87 	set_timespec(abstime, flush_time);
88         reset_flush_time = FALSE;
89       }
90       while ((!error || error == EINTR) && !abort_manager && !cb_list)
91         error= mysql_cond_timedwait(&COND_manager, &LOCK_manager, &abstime);
92 
93       if (error == ETIMEDOUT || error == ETIME)
94       {
95         tc_purge();
96         error = 0;
97         reset_flush_time = TRUE;
98       }
99     }
100     else
101     {
102       while ((!error || error == EINTR) && !abort_manager && !cb_list)
103         error= mysql_cond_wait(&COND_manager, &LOCK_manager);
104     }
105 
106     struct handler_cb *cb= cb_list;
107     cb_list= NULL;
108     mysql_mutex_unlock(&LOCK_manager);
109 
110     while (cb)
111     {
112       struct handler_cb *next= cb->next;
113       cb->action(cb->data);
114       my_free(cb);
115       cb= next;
116     }
117     mysql_mutex_lock(&LOCK_manager);
118   }
119   manager_thread_in_use = 0;
120   mysql_mutex_unlock(&LOCK_manager);
121   mysql_mutex_destroy(&LOCK_manager);
122   mysql_cond_destroy(&COND_manager);
123   DBUG_LEAVE; // Can't use DBUG_RETURN after my_thread_end
124   my_thread_end();
125   return (NULL);
126 }
127 
128 
129 /* Start handle manager thread */
start_handle_manager()130 void start_handle_manager()
131 {
132   DBUG_ENTER("start_handle_manager");
133   abort_manager = false;
134   {
135     pthread_t hThread;
136     int err;
137     manager_thread_in_use = 1;
138     mysql_cond_init(key_COND_manager, &COND_manager,NULL);
139     mysql_mutex_init(key_LOCK_manager, &LOCK_manager, NULL);
140     if ((err= mysql_thread_create(key_thread_handle_manager, &hThread,
141                                   &connection_attrib, handle_manager, 0)))
142       sql_print_warning("Can't create handle_manager thread (errno: %M)", err);
143   }
144   DBUG_VOID_RETURN;
145 }
146 
147 
148 /* Initiate shutdown of handle manager thread */
stop_handle_manager()149 void stop_handle_manager()
150 {
151   DBUG_ENTER("stop_handle_manager");
152   if (manager_thread_in_use)
153   {
154     mysql_mutex_lock(&LOCK_manager);
155     abort_manager = true;
156     DBUG_PRINT("quit", ("initiate shutdown of handle manager thread: %lu",
157                         (ulong)manager_thread));
158     mysql_cond_signal(&COND_manager);
159     mysql_mutex_unlock(&LOCK_manager);
160   }
161   DBUG_VOID_RETURN;
162 }
163 
164