1 | /* |
2 | Portions Copyright (c) 2015-Present, Facebook, Inc. |
3 | Portions Copyright (c) 2012, Monty Program Ab |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; version 2 of the License. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
17 | |
18 | #ifdef USE_PRAGMA_IMPLEMENTATION |
19 | #pragma implementation // gcc: Class implementation |
20 | #endif |
21 | |
22 | #include <my_global.h> |
23 | |
24 | /* The C++ file's header */ |
25 | #include "./rdb_threads.h" |
26 | |
27 | namespace myrocks { |
28 | |
29 | void *Rdb_thread::thread_func(void *const thread_ptr) { |
30 | DBUG_ASSERT(thread_ptr != nullptr); |
31 | Rdb_thread *const thread = static_cast<Rdb_thread *const>(thread_ptr); |
32 | if (!thread->m_run_once.exchange(true)) { |
33 | thread->setname(); |
34 | thread->run(); |
35 | thread->uninit(); |
36 | } |
37 | return nullptr; |
38 | } |
39 | |
40 | void Rdb_thread::init( |
41 | #ifdef HAVE_PSI_INTERFACE |
42 | my_core::PSI_mutex_key stop_bg_psi_mutex_key, |
43 | my_core::PSI_cond_key stop_bg_psi_cond_key |
44 | #endif |
45 | ) { |
46 | DBUG_ASSERT(!m_run_once); |
47 | mysql_mutex_init(stop_bg_psi_mutex_key, &m_signal_mutex, MY_MUTEX_INIT_FAST); |
48 | mysql_cond_init(stop_bg_psi_cond_key, &m_signal_cond, nullptr); |
49 | } |
50 | |
51 | void Rdb_thread::uninit() { |
52 | mysql_mutex_destroy(&m_signal_mutex); |
53 | mysql_cond_destroy(&m_signal_cond); |
54 | } |
55 | |
56 | int Rdb_thread::create_thread(const std::string &thread_name |
57 | #ifdef HAVE_PSI_INTERFACE |
58 | , |
59 | PSI_thread_key background_psi_thread_key |
60 | #endif |
61 | ) { |
62 | // Make a copy of the name so we can return without worrying that the |
63 | // caller will free the memory |
64 | m_name = thread_name; |
65 | |
66 | return mysql_thread_create(background_psi_thread_key, &m_handle, nullptr, |
67 | thread_func, this); |
68 | |
69 | } |
70 | |
71 | void Rdb_thread::signal(const bool &stop_thread) { |
72 | RDB_MUTEX_LOCK_CHECK(m_signal_mutex); |
73 | |
74 | if (stop_thread) { |
75 | m_stop = true; |
76 | } |
77 | |
78 | mysql_cond_signal(&m_signal_cond); |
79 | |
80 | RDB_MUTEX_UNLOCK_CHECK(m_signal_mutex); |
81 | } |
82 | |
83 | } // namespace myrocks |
84 | |