1 /*
2    Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef NDB_COMPONENT_H
26 #define NDB_COMPONENT_H
27 
28 #include <my_global.h>
29 #include <thr_cond.h>
30 #include <thr_mutex.h>
31 
32 extern "C" void * Ndb_component_run_C(void *);
33 
34 /**
35  * Baseclass encapsulating the different components
36  * in ndbcluster.
37  *
38  * NOTE! The intention should be to not correlate to number of
39  * threads since that is an implementation detail in each
40  * component.
41  */
42 
43 class Ndb_component
44 {
45 public:
46   virtual int init();
47   virtual int start();
48   virtual int stop();
49   virtual int deinit();
50 
51 protected:
52   /**
53    * Con/de-structor is protected...so that sub-class needs to provide own
54    */
55   Ndb_component(const char* name);
56   virtual ~Ndb_component();
57 
58   /**
59    * Component init function
60    */
61   virtual int do_init() = 0;
62 
63   /**
64    * Component run function
65    */
66   virtual void do_run() = 0;
67 
68   /**
69    * Component deinit function
70    */
71   virtual int do_deinit() = 0;
72 
73   /**
74    * Component wakeup function
75    * - called when component is set to stop, should
76    *   wakeup component from waiting
77    */
78   virtual void do_wakeup() = 0;
79 
80   /**
81    * For usage in threads main loop
82    */
83   bool is_stop_requested();
84 
85 protected:
86   void log_verbose(unsigned verbose_level, const char* fmt, ...)
87     MY_ATTRIBUTE((format(printf, 3, 4)));
88   void log_error(const char *fmt, ...)
89     MY_ATTRIBUTE((format(printf, 2, 3)));
90   void log_warning(const char *fmt, ...)
91     MY_ATTRIBUTE((format(printf, 2, 3)));
92   void log_info(const char *fmt, ...)
93     MY_ATTRIBUTE((format(printf, 2, 3)));
94 
95 private:
96 
97   enum ThreadState
98   {
99     TS_UNINIT   = 0,
100     TS_INIT     = 1,
101     TS_STARTING = 2,
102     TS_RUNNING  = 3,
103     TS_STOPPING = 4,
104     TS_STOPPED  = 5
105   };
106 
107   ThreadState m_thread_state;
108   my_thread_handle m_thread;
109   native_mutex_t m_start_stop_mutex;
110   native_cond_t m_start_stop_cond;
111 
112   const char* m_name;
113 
114   void run_impl();
115   friend void * Ndb_component_run_C(void *);
116 };
117 
118 #endif
119