MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MgmtThread.hpp
1 /* Copyright (C) 2008 Sun Microsystems, Inc.
2  All rights reserved. Use is subject to license terms.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 
17 #ifndef MgmtThread_H
18 #define MgmtThread_H
19 
20 #include <ndb_global.h>
21 #include <NdbThread.h>
22 
23 class MgmtThread {
24  bool m_running;
25  const char* m_name;
26  size_t m_stack_size;
27  NDB_THREAD_PRIO m_thread_prio;
28  struct NdbThread* m_thread;
29 
30  static void* run_C(void* t) {
31  MgmtThread *thread = (MgmtThread*)t;
32  thread->run();
33  return 0;
34  }
35 public:
36  MgmtThread(); // Not implemented
37  MgmtThread(const MgmtThread&); // Not implemented
38  MgmtThread(const char* name,
39  size_t stack_size= 0, // Use default stack size
40  NDB_THREAD_PRIO thread_prio= NDB_THREAD_PRIO_LOW) :
41  m_running(true),
42  m_name(name),
43  m_stack_size(stack_size),
44  m_thread_prio(thread_prio),
45  m_thread(NULL){
46  };
47  virtual ~MgmtThread() {
48  if (m_thread)
49  stop();
50  };
51 
52  virtual void run()= 0;
53  bool start(){
54  assert(m_running);
55  m_thread = NdbThread_Create(run_C, (void**)this, m_stack_size,
56  m_name, m_thread_prio);
57  return (m_thread != NULL);
58  }
59  bool stop(){
60  void* res = 0;
61  if (!m_thread)
62  return false;
63 
64  m_running= false;
65 
66  NdbThread_WaitFor(m_thread, &res);
67  NdbThread_Destroy(&m_thread);
68  return true;
69 
70  }
71  bool is_stopped() { return !m_running; };
72 };
73 
74 #endif