MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndb_init.cpp
1 /*
2  Copyright (c) 2004, 2010, 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 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 St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 #include <my_sys.h>
20 #include <NdbMutex.h>
21 
22 class EventLogger *g_eventLogger = NULL;
23 
24 NdbMutex *g_ndb_connection_mutex = NULL;
25 
26 extern class EventLogger * create_event_logger();
27 extern void destroy_event_logger(class EventLogger ** g_eventLogger);
28 
29 // Turn on monotonic timers and conditions by setting
30 // this flag before calling ndb_init
31 int g_ndb_init_need_monotonic = 0;
32 
33 static int ndb_init_called = 0;
34 
35 extern "C" void NdbCondition_initialize(int need_monotonic);
36 extern "C" void NdbTick_Init(int need_monotonic);
37 extern "C" int NdbThread_Init();
38 extern "C" void NdbThread_End();
39 extern void NdbOut_Init();
40 
41 extern "C"
42 {
43 
44 void
45 ndb_init_internal()
46 {
47  NdbOut_Init();
48  if (!g_ndb_connection_mutex)
49  g_ndb_connection_mutex = NdbMutex_Create();
50  if (!g_eventLogger)
51  g_eventLogger = create_event_logger();
52  if ((g_ndb_connection_mutex == NULL) || (g_eventLogger == NULL))
53  {
54  {
55  const char* err = "ndb_init() failed - exit\n";
56  int res = write(2, err, strlen(err));
57  (void)res;
58  exit(1);
59  }
60  }
61  /* Always turn on monotonic unless on Solaris */
62 #ifndef __sun
63  g_ndb_init_need_monotonic = 1;
64 #endif
65  NdbTick_Init(g_ndb_init_need_monotonic);
66  NdbCondition_initialize(g_ndb_init_need_monotonic);
67  NdbThread_Init();
68 }
69 
70 int
71 ndb_init()
72 {
73  if (ndb_init_called == 0)
74  {
75  ndb_init_called = 1;
76  if (my_init())
77  {
78  const char* err = "my_init() failed - exit\n";
79  int res = write(2, err, strlen(err));
80  (void)res;
81  exit(1);
82  }
83  ndb_init_internal();
84  }
85  return 0;
86 }
87 
88 void
89 ndb_end_internal()
90 {
91  NdbThread_End();
92  if (g_ndb_connection_mutex)
93  {
94  NdbMutex_Destroy(g_ndb_connection_mutex);
95  g_ndb_connection_mutex=NULL;
96  }
97  if (g_eventLogger)
98  destroy_event_logger(&g_eventLogger);
99 }
100 
101 void
102 ndb_end(int flags)
103 {
104  if (ndb_init_called == 1)
105  {
106  my_end(flags);
107  ndb_end_internal();
108  ndb_init_called = 0;
109  }
110 }
111 
112 } /* extern "C" */