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 #pragma once 18 19 /* C++ standard header files */ 20 #include <atomic> 21 #include <string> 22 23 /* MySQL includes */ 24 #include "./my_global.h" 25 #include <mysql/psi/mysql_table.h> 26 #include <mysql/thread_pool_priv.h> 27 28 /* MyRocks header files */ 29 #include "./rdb_utils.h" 30 31 namespace myrocks { 32 33 class Rdb_thread { 34 private: 35 // Disable Copying 36 Rdb_thread(const Rdb_thread &); 37 Rdb_thread &operator=(const Rdb_thread &); 38 39 // Make sure we run only once 40 std::atomic_bool m_run_once; 41 42 pthread_t m_handle; 43 44 protected: 45 mysql_mutex_t m_signal_mutex; 46 mysql_cond_t m_signal_cond; 47 bool m_stop = false; 48 49 public: Rdb_thread()50 Rdb_thread() : m_run_once(false) {} 51 52 #ifdef HAVE_PSI_INTERFACE 53 void init(my_core::PSI_mutex_key stop_bg_psi_mutex_key, 54 my_core::PSI_cond_key stop_bg_psi_cond_key); 55 int create_thread(const std::string &thread_name, 56 my_core::PSI_thread_key background_psi_thread_key); 57 #else 58 void init(); 59 int create_thread(const std::string &thread_name); 60 #endif 61 62 virtual void run(void) = 0; 63 64 void signal(const bool &stop_thread = false); 65 join()66 int join() { return pthread_join(m_handle, nullptr); } 67 68 void uninit(); 69 ~Rdb_thread()70 virtual ~Rdb_thread() {} 71 72 private: 73 static void *thread_func(void *const thread_ptr); 74 }; 75 76 /** 77 MyRocks background thread control 78 N.B. This is on top of RocksDB's own background threads 79 (@see rocksdb::CancelAllBackgroundWork()) 80 */ 81 82 class Rdb_background_thread : public Rdb_thread { 83 private: 84 bool m_save_stats = false; 85 reset()86 void reset() { 87 mysql_mutex_assert_owner(&m_signal_mutex); 88 m_stop = false; 89 m_save_stats = false; 90 } 91 92 public: 93 virtual void run() override; 94 request_save_stats()95 void request_save_stats() { 96 mysql_mutex_lock(&m_signal_mutex); 97 m_save_stats = true; 98 mysql_mutex_unlock(&m_signal_mutex); 99 } 100 }; 101 102 /* 103 Drop index thread control 104 */ 105 106 struct Rdb_drop_index_thread : public Rdb_thread { 107 virtual void run() override; 108 }; 109 110 } // namespace myrocks 111