- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathMasterImplementation.cpp
459 lines (364 loc) · 9.14 KB
/
MasterImplementation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* PROGRAM: Firebird interface.
* MODULE: MasterImplementation.cpp
* DESCRIPTION: Main firebird interface, used to get other interfaces.
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alex Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2010 Alex Peshkov <peshkoff at mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
#include"firebird.h"
#include"firebird/Interface.h"
#include<string.h>
#include"../yvalve/MasterImplementation.h"
#include"../common/classes/init.h"
#include"../common/MsgMetadata.h"
#include"../common/StatusHolder.h"
#include"../yvalve/PluginManager.h"
#include"../common/classes/GenericMap.h"
#include"../common/classes/fb_pair.h"
#include"../common/classes/rwlock.h"
#include"../common/classes/semaphore.h"
#include"../common/isc_proto.h"
#include"../common/ThreadStart.h"
#include"../common/utils_proto.h"
#include"../common/dllinst.h"
#include"../common/status.h"
#include"ibase.h"
#include"../yvalve/utl_proto.h"
usingnamespaceFirebird;
namespaceWhy {
//
// getStatus()
//
classUserStatusfinal : public Firebird::DisposeIface<Firebird::BaseStatus<UserStatus> >
{
};
IStatus* MasterImplementation::getStatus()
{
return FB_NEW UserStatus;
}
//
// getDispatcher()
//
IProvider* MasterImplementation::getDispatcher()
{
IProvider* dispatcher = FB_NEW Dispatcher;
dispatcher->addRef();
return dispatcher;
}
//
// getDtc()
//
Static<Dtc> MasterImplementation::dtc;
Dtc* MasterImplementation::getDtc()
{
return &dtc;
}
//
// getPluginManager()
//
IPluginManager* MasterImplementation::getPluginManager()
{
static Static<PluginManager> manager;
return &manager;
}
IMetadataBuilder* MasterImplementation::getMetadataBuilder(CheckStatusWrapper* status, unsigned fieldCount)
{
try
{
IMetadataBuilder* bld = FB_NEW MetadataBuilder(fieldCount);
bld->addRef();
return bld;
}
catch (const Exception& ex)
{
ex.stuffException(status);
returnNULL;
}
}
intMasterImplementation::serverMode(int mode)
{
staticint currentMode = -1;
if (mode >= 0)
currentMode = mode;
return currentMode;
}
} // namespace Why
//
// timer
//
namespaceWhy {
staticbool abortShutdownFlag = false;
voidabortShutdown()
{
abortShutdownFlag = true;
}
Thread::Handle timerThreadHandle = 0;
FB_BOOLEAN MasterImplementation::getProcessExiting()
{
if (abortShutdownFlag)
returntrue;
#ifdef WIN_NT
// Sometime, when user process exits not calling fb_shutdown and timer thread should
// be terminated already, wait for its handle with zero timeout returns WAIT_TIMEOUT.
// Usage of small non-zero timeout seems fixed such cases.
if (timerThreadHandle && WaitForSingleObject(timerThreadHandle, 10) != WAIT_TIMEOUT)
returntrue;
#endif
returnfalse;
}
namespace {
// Protects timerQueue array
GlobalPtr<Mutex> timerAccess;
// Protects from races during module unload process
// Should be taken before timerAccess
GlobalPtr<Mutex> timerPause;
GlobalPtr<Semaphore> timerWakeup;
GlobalPtr<Semaphore> timerCleanup;
// Should use atomic flag for thread stop to provide correct membar
AtomicCounter stopTimerThread(0);
structTimerEntry
{
ISC_UINT64 fireTime;
ITimer* timer;
staticconst ISC_UINT64 generate(const TimerEntry& item) { return item.fireTime; }
static THREAD_ENTRY_DECLARE timeThread(THREAD_ENTRY_PARAM);
staticvoidinit()
{
Thread::start(timeThread, 0, THREAD_high, &timerThreadHandle);
}
staticvoidcleanup();
};
typedef SortedArray<TimerEntry, InlineStorage<TimerEntry, 64>, ISC_UINT64, TimerEntry> TimerQueue;
GlobalPtr<TimerQueue> timerQueue;
InitMutex<TimerEntry> timerHolder("TimerHolder");
voidTimerEntry::cleanup()
{
{
MutexLockGuard guard(timerAccess, FB_FUNCTION);
stopTimerThread.setValue(1);
timerWakeup->release();
}
timerCleanup->tryEnter(5);
Thread::waitForCompletion(timerThreadHandle);
while (timerQueue->hasData())
{
ITimer* timer = NULL;
{
MutexLockGuard guard(timerAccess, FB_FUNCTION);
TimerEntry* e = timerQueue->end();
timer = (--e)->timer;
timerQueue->remove(e);
}
timer->release();
}
}
ISC_UINT64 curTime()
{
double rc = fb_utils::query_performance_counter();
rc *= 1000000;
rc /= fb_utils::query_performance_frequency();
returnISC_UINT64(rc);
}
TimerEntry* getTimer(ITimer* timer)
{
fb_assert(timerAccess->locked());
for (unsignedint i = 0; i < timerQueue->getCount(); ++i)
{
TimerEntry& e(timerQueue->operator[](i));
if (e.timer == timer)
{
return &e;
}
}
returnNULL;
}
THREAD_ENTRY_DECLARE TimerEntry::timeThread(THREAD_ENTRY_PARAM)
{
#ifdef WIN_NT
// The timer thread could unload plugins. Plugins almost always linked with
// dispatcher (fbclient.dll) thus, when plugin unloaded it decrement usage
// count of fbclient.dll. If application unload fbclient.dll not calling
// fb_shutdown, then last unloaded plugin will finally unload fbclient.dll
// and the code that is currently running, leading to AV.
// To prevent such scenario we increment usage count of fbclient.dll and
// will decrement it in safe way at the end of the timer thread.
char buff[MAX_PATH];
GetModuleFileName(hDllInst, buff, sizeof(buff));
HMODULE hDll = LoadLibrary(buff);
#endif
while (stopTimerThread.value() == 0
#ifdef WIN_NT
&& Firebird::dDllUnloadTID == 0
#endif
)
{
ISC_UINT64 microSeconds = 0;
{
MutexLockGuard pauseGuard(timerPause, FB_FUNCTION);
MutexLockGuard guard(timerAccess, FB_FUNCTION);
const ISC_UINT64 cur = curTime();
if (timerQueue->getCount() > 0)
{
TimerEntry e(timerQueue->operator[](0));
if (e.fireTime <= cur)
{
timerQueue->remove((FB_SIZE_T) 0);
// We must leave timerAccess mutex here to avoid deadlocks
MutexUnlockGuard ug(timerAccess, FB_FUNCTION);
e.timer->handler();
e.timer->release();
continue;
}
else
{
microSeconds = e.fireTime - cur;
}
}
}
if (microSeconds)
{
timerWakeup->tryEnter(0, microSeconds / 1000);
}
else
{
timerWakeup->enter();
}
}
timerCleanup->release();
#ifdef WIN_NT
if (Firebird::dDllUnloadTID)
{
// fb_shutdown is called as result of FreeLibrary, not by application.
// Sooner of all we are the last user of fbclient.dll, and code will be
// physically unloaded as result of FreeLibrary() call.
FreeLibraryAndExitThread(hDll, 0);
}
else
FreeLibrary(hDll); // It is safe to decrement usage count here
#endif
return0;
}
} // namespace
classTimerImplementation :
public AutoIface<ITimerControlImpl<TimerImplementation, CheckStatusWrapper> >
{
public:
// ITimerControl implementation
voidstart(CheckStatusWrapper* status, ITimer* timer, ISC_UINT64 microSeconds)
{
try
{
MutexLockGuard guard(timerAccess, FB_FUNCTION);
if (stopTimerThread.value() != 0)
{
// Ignore an attempt to start timer - anyway thread to make it fire is down
// We must leave timerAccess mutex here to avoid deadlocks
MutexUnlockGuard ug(timerAccess, FB_FUNCTION);
timer->addRef();
timer->release();
return;
}
timerHolder.init();
TimerEntry* curTimer = getTimer(timer);
if (curTimer)
timerQueue->remove(curTimer);
else
timer->addRef();
TimerEntry newTimer;
newTimer.timer = timer;
newTimer.fireTime = curTime() + microSeconds;
timerQueue->add(newTimer);
timerWakeup->release();
}
catch (const Firebird::Exception& ex)
{
ex.stuffException(status);
}
}
voidstop(CheckStatusWrapper* status, ITimer* timer)
{
try
{
MutexLockGuard guard(timerAccess, FB_FUNCTION);
TimerEntry* curTimer = getTimer(timer);
if (curTimer)
{
curTimer->timer->release();
timerQueue->remove(curTimer);
}
}
catch (const Firebird::Exception& ex)
{
ex.stuffException(status);
}
}
};
ITimerControl* MasterImplementation::getTimerControl()
{
static Static<TimerImplementation> timer;
return &timer;
}
voidshutdownTimers()
{
stopTimerThread.setValue(1);
timerHolder.cleanup();
}
Mutex& pauseTimer()
{
return timerPause;
}
} // namespace Why
//
// Util (misc calls)
//
namespaceWhy {
extern UtilInterface utilInterface; // Implemented in utl.cpp
IUtil* MasterImplementation::getUtilInterface()
{
return &utilInterface;
}
} // namespace Why
//
// ConfigManager (config info access)
//
namespaceFirebird {
extern IConfigManager* iConfigManager;
} // namespace Firebird
namespaceWhy {
IConfigManager* MasterImplementation::getConfigManager()
{
return Firebird::iConfigManager;
}
} // namespace Why
//
// get master
//
namespaceFirebird
{
typedef IMaster* IMasterPtr;
extern"C" IMasterPtr API_ROUTINE fb_get_master_interface()
{
static Static<Why::MasterImplementation> instance;
return &instance;
}
}