MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SHM_Transporter.win32.cpp
1 /*
2  Copyright (C) 2003-2006, 2008 MySQL AB
3  All rights reserved. Use is subject to license terms.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include <ndb_global.h>
21 
22 #include "SHM_Transporter.hpp"
23 #include "TransporterInternalDefinitions.hpp"
24 #include <TransporterCallback.hpp>
25 #include <NdbSleep.h>
26 #include <NdbOut.hpp>
27 
28 #include <windows.h>
29 
30 
31 void SHM_Transporter::make_error_info(char info[], int sz)
32 {
33  snprintf(info,sz,"Shm key=%d sz=%d",
34  shmKey, shmSize);
35 }
36 
37 bool
38 SHM_Transporter::connectServer(Uint32 timeOutMillis){
39  if(!_shmSegCreated)
40  {
41  char szName[32];
42  sprintf(szName, "ndb%lu", shmKey);
43  hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
44  0,
45  PAGE_READWRITE,
46  0,
47  shmSize,
48  szName);
49 
50  if(!hFileMapping)
51  {
52  reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_CREATE_SEGMENT);
53  NdbSleep_MilliSleep(timeOutMillis);
54  return false;
55  }
56  _shmSegCreated = true;
57  }
58 
59  if(!_attached){
60  shmBuf = (char*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
61  if(shmBuf == 0){
62  reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_ATTACH_SEGMENT);
63  NdbSleep_MilliSleep(timeOutMillis);
64  return false;
65  }
66  volatile Uint32 * sharedCountAttached =
67  (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
68  ++*sharedCountAttached;
69  _attached = true;
70  }
71 
72  volatile Uint32 * sharedCountAttached =
73  (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
74 
75  if(*sharedCountAttached == 2 && !setupBuffersDone) {
76  setupBuffers();
77  setupBuffersDone=true;
78  }
79  if(*sharedCountAttached > 2) {
80  reportThreadError(remoteNodeId, TE_SHM_DISCONNECT);
81  return false;
82  }
83 
84  if(setupBuffersDone) {
85  NdbSleep_MilliSleep(timeOutMillis);
86  if(*serverStatusFlag==1 && *clientStatusFlag==1)
87  return true;
88  }
89 
90  NdbSleep_MilliSleep(timeOutMillis);
91  return false;
92 }
93 
94 bool
95 SHM_Transporter::connectClient(Uint32 timeOutMillis){
96  if(!_shmSegCreated)
97  {
98  char szName[32];
99  sprintf(szName, "ndb%lu", shmKey);
100  hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
101 
102  if(!hFileMapping)
103  {
104  NdbSleep_MilliSleep(timeOutMillis);
105  return false;
106  }
107  _shmSegCreated = true;
108  }
109 
110  if(!_attached){
111  shmBuf = (char*)MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
112  if(shmBuf == 0){
113  reportThreadError(remoteNodeId, TE_SHM_UNABLE_TO_ATTACH_SEGMENT);
114  NdbSleep_MilliSleep(timeOutMillis);
115  return false;
116  }
117  volatile Uint32 * sharedCountAttached =
118  (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
119  ++*sharedCountAttached;
120  _attached = true;
121  }
122 
123  volatile Uint32 * sharedCountAttached =
124  (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
125 
126  if(*sharedCountAttached == 2 && !setupBuffersDone) {
127  setupBuffers();
128  setupBuffersDone=true;
129  }
130 
131  if(setupBuffersDone) {
132  if(*serverStatusFlag==1 && *clientStatusFlag==1)
133  return true;
134  }
135  NdbSleep_MilliSleep(timeOutMillis);
136  return false;
137 
138 }
139 
140 
141 bool
143  volatile Uint32 * sharedCountAttached =
144  (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
145  if(*sharedCountAttached != 2) {
146  report_error(TE_SHM_DISCONNECT);
147  return false;
148  }
149  return true;
150 }
151 
152 void
154  if(_attached) {
155  volatile Uint32 * sharedCountAttached =
156  (volatile Uint32*)(shmBuf + 6*sizeof(Uint32*));
157 
158  --*sharedCountAttached;
159 
160  if(!UnmapViewOfFile(shmBuf)) {
161  report_error(TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
162  return;
163  }
164 
165  _attached = false;
166  if(!isServer && _shmSegCreated)
167  _shmSegCreated = false;
168  }
169 
170  if(_shmSegCreated){
171  if(!CloseHandle(hFileMapping)) {
172  report_error(TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
173  return;
174  }
175  _shmSegCreated = false;
176  }
177  setupBuffersDone=false;
178 
179 }
180