-
Notifications
You must be signed in to change notification settings - Fork 6
/
msdtc_enlist.cpp
1324 lines (1231 loc) · 31.5 KB
/
msdtc_enlist.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Module: msdtc_enlist.cpp
*
* Description:
* This module contains routines related to
* the enlistment in MSDTC.
*
*-------
*/
#ifdef _HANDLE_ENLIST_IN_DTC_
#undef _MEMORY_DEBUG_
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif /* _WIN32_WINNT */
#define WIN32_LEAN_AND_MEAN
#include <oleTx2xa.h>
#include <XOLEHLP.h>
#define _PGDTC_FUNCS_IMPORT_
#include "connexp.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <process.h>
#include <map>
#ifndef WIN32
#include <errno.h>
#endif /* WIN32 */
#include <sql.h>
#define _MYLOG_FUNCS_IMPORT_
#include "mylog.h"
#define _PGENLIST_FUNCS_IMPLEMENT_
#include "pgenlist.h"
#include "xalibname.h"
#ifdef WIN32
#ifndef snprintf
#define snprintf _snprintf
#endif /* snprintf */
#endif /* WIN32 */
/* Define a type for defining a constant string expression */
#ifndef CSTR
#define CSTR static const char * const
#endif /* CSTR */
EXTERN_C {
HINSTANCE s_hModule; /* Saved module handle. */
}
/* This is where the Driver Manager attaches to this Driver */
BOOL WINAPI
DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
s_hModule = (HINSTANCE) hInst; /* Save for dialog boxes */
break;
case DLL_PROCESS_DETACH:
mylog("DETACHING pgenlist\n");
break;
}
return TRUE;
}
/*
* A comment About locks used in this module
*
* the locks should be acquired with stronger to weaker order.
*
* 1:ELOCK -- the strongest per IAsyncPG object lock
* When the *isolated* or *dtcconn* member of an IAsyncPG object
* is changed, this lock should be held.
* While an IAsyncPG object accesses a psqlodbc connection,
* this lock should be held.
*
* 2:[CONN_CS] -- per psqlodbc connection lock
* This lock would be held for a pretty long time while accessing
* the psqlodbc connection assigned to an IAsyncPG object. You
* can use the connecion safely by holding a ELOCK for the
* IAsyncPG object because the assignment is ensured to be
* fixed while the ELOCK is held.
*
* 3:LIFELOCK -- a global lock to ensure the lives of IAsyncPG objects
* While this lock is held, IAsyncPG objects would never die.
*
* 4:SLOCK -- the short term per IAsyncPG object lock
* When any member of an IAsyncPG object is changed, this lock
* should be held.
*/
static class INIT_CRIT
{
public:
CRITICAL_SECTION life_cs; /* for asdum member of ConnectionClass */
INIT_CRIT() {
InitializeCriticalSection(&life_cs);
}
~INIT_CRIT() {
DeleteCriticalSection(&life_cs);
}
} init_crit;
#define LIFELOCK_ACQUIRE EnterCriticalSection(&init_crit.life_cs)
#define LIFELOCK_RELEASE LeaveCriticalSection(&init_crit.life_cs)
/*
* Some helper macros about connection handling.
*/
#define CONN_CS_ACQUIRE(conn) PgDtc_lock_cntrl((conn), TRUE, FALSE)
#define TRY_CONN_CS_ACQUIRE(conn) PgDtc_lock_cntrl((conn), TRUE, TRUE)
#define CONN_CS_RELEASE(conn) PgDtc_lock_cntrl((conn), FALSE, FALSE)
#define CONN_IS_IN_TRANS(conn) PgDtc_get_property((conn), inTrans)
static const char *XidToText(const XID &xid, char *rtext)
{
int glen = xid.gtrid_length, blen = xid.bqual_length;
int i, j;
for (i = 0, j = 0; i < glen; i++, j += 2)
sprintf(rtext + j, "%02x", (unsigned char) xid.data[i]);
strcat(rtext, "-"); j++;
for (; i < glen + blen; i++, j += 2)
sprintf(rtext + j, "%02x", (unsigned char) xid.data[i]);
return rtext;
}
static LONG g_cComponents = 0;
static LONG g_cServerLocks = 0;
//
// 以下のITransactionResourceAsyncオブジェクトは任意のスレッドから
// 自由にアクセス可能なように実装する。各Requestの結果を返すために
// 使用するITransactionEnlistmentAsyncインターフェイスもそのように
// 実装されている(と思われる、下記参照)ので呼び出しにCOMのアパー
// トメントを意識する(CoMarshalInterThreadInterfaceInStream/CoGetIn
// terfaceAndReleaseStreamを使用する)必要はない。
// このDLL内で使用するITransactionResourceAsyncとITransactionEnlist
// mentAsyncのインターフェイスポインターは任意のスレッドから直接使用
// することができる。
//
// OLE Transactions Standard
//
// OLE Transactions is the Microsoft interface standard for transaction
// management. Applications use OLE Transactions-compliant interfaces to
// initiate, commit, abort, and inquire about transactions. Resource
// managers use OLE Transactions-compliant interfaces to enlist in
// transactions, to propagate transactions to other resource managers,
// to propagate transactions from process to process or from system to
// system, and to participate in the two-phase commit protocol.
//
// The Microsoft DTC system implements most OLE Transactions-compliant
// objects, interfaces, and methods. Resource managers that wish to use
// OLE Transactions must implement some OLE Transactions-compliant objects,
// interfaces, and methods.
//
// The OLE Transactions specification is based on COM but it differs in the
// following respects:
//
// OLE Transactions objects cannot be created using the COM CoCreate APIs.
// References to OLE Transactions objects are always direct. Therefore,
// no proxies or stubs are created for inter-apartment, inter-process,
// or inter-node calls and OLE Transactions references cannot be marshaled
// using standard COM marshaling.
// All references to OLE Transactions objects and their sinks are completely
// free threaded and cannot rely upon COM concurrency control models.
// For example, you cannot pass a reference to an IResourceManagerSink
// interface on a single-threaded apartment and expect the callback to occur
// only on the same single-threaded apartment.
class IAsyncPG : public ITransactionResourceAsync
{
private:
IDtcToXaHelperSinglePipe *helper;
DWORD RMCookie;
void *dtcconn;
LONG refcnt;
CRITICAL_SECTION as_spin; // to make this object Both
CRITICAL_SECTION as_exec; // to make this object Both
XID xid;
bool isolated;
bool prepared;
bool done;
bool abort;
HANDLE eThread[3];
bool eFin[3];
bool requestAccepted;
HRESULT prepare_result;
HRESULT commit_result;
#ifdef _LOCK_DEBUG_
int spin_cnt;
int cs_cnt;
#endif /* _LOCK_DEBUG_ */
public:
enum {
PrepareExec = 0
,CommitExec
,AbortExec
};
ITransactionEnlistmentAsync *enlist;
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
ULONG STDMETHODCALLTYPE AddRef(void);
ULONG STDMETHODCALLTYPE Release(void);
HRESULT STDMETHODCALLTYPE PrepareRequest(BOOL fRetaining,
DWORD grfRM,
BOOL fWantMoniker,
BOOL fSinglePhase);
HRESULT STDMETHODCALLTYPE CommitRequest(DWORD grfRM, XACTUOW * pNewUOW);
HRESULT STDMETHODCALLTYPE AbortRequest(BOID * pboidReason,
BOOL fRetaining,
XACTUOW * pNewUOW);
HRESULT STDMETHODCALLTYPE TMDown(void);
IAsyncPG();
void SetHelper(IDtcToXaHelperSinglePipe *pHelper, DWORD dwRMCookie) {helper = pHelper; RMCookie = dwRMCookie;}
HRESULT RequestExec(DWORD type, HRESULT res);
HRESULT ReleaseConnection(void);
void SetConnection(void *sconn) {SLOCK_ACQUIRE(); dtcconn = sconn; SLOCK_RELEASE();}
void SetXid(const XID *ixid) {SLOCK_ACQUIRE(); xid = *ixid; SLOCK_RELEASE();}
void *separateXAConn(bool spinAcquired, bool continueConnection);
bool CloseThread(DWORD type);
private:
~IAsyncPG();
void SLOCK_ACQUIRE() {EnterCriticalSection(&as_spin);}
void SLOCK_RELEASE() {LeaveCriticalSection(&as_spin);}
void ELOCK_ACQUIRE() {EnterCriticalSection(&as_exec);}
void ELOCK_RELEASE() {LeaveCriticalSection(&as_exec);}
void *getLockedXAConn(void);
void *generateXAConn(bool spinAcquired);
void *isolateXAConn(bool spinAcquired, bool continueConnection);
void SetPrepareResult(HRESULT res) {SLOCK_ACQUIRE(); prepared = true; prepare_result = res; SLOCK_RELEASE();}
void SetDone(HRESULT);
void Wait_pThread(bool slock_hold);
void Wait_cThread(bool slock_hold, bool once);
};
IAsyncPG::IAsyncPG(void) : helper(NULL), RMCookie(0), enlist(NULL), dtcconn(NULL), refcnt(1), isolated(false), done(false), abort(false), prepared(false), requestAccepted(false)
{
InterlockedIncrement(&g_cComponents);
InitializeCriticalSection(&as_spin);
InitializeCriticalSection(&as_exec);
eThread[0] = eThread[1] = eThread[2] = NULL;
eFin[0] = eFin[1] = eFin[2] = false;
memset(&xid, 0, sizeof(xid));
#ifdef _LOCK_DEBUG_
spin_cnt = 0;
cs_cnt = 0;
#endif /* _LOCK_DEBUG_ */
}
//
// invoked from *delete*.
// When entered ELOCK -> LIFELOCK -> SLOCK are held
// and they are released.
//
IAsyncPG::~IAsyncPG(void)
{
void *fconn = NULL;
if (dtcconn)
{
if (isolated)
fconn = dtcconn;
PgDtc_set_async(dtcconn, NULL);
dtcconn = NULL;
}
SLOCK_RELEASE();
LIFELOCK_RELEASE;
if (fconn)
{
mylog("IAsyncPG Destructor is freeing the connection\n");
PgDtc_free_connect(fconn);
}
DeleteCriticalSection(&as_spin);
ELOCK_RELEASE();
DeleteCriticalSection(&as_exec);
InterlockedDecrement(&g_cComponents);
}
HRESULT STDMETHODCALLTYPE IAsyncPG::QueryInterface(REFIID riid, void ** ppvObject)
{
mylog("%p QueryInterface called\n", this);
if (riid == IID_IUnknown || riid == IID_ITransactionResourceAsync)
{
*ppvObject = this;
AddRef();
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
//
// acquire/releases SLOCK.
//
ULONG STDMETHODCALLTYPE IAsyncPG::AddRef(void)
{
mylog("%p->AddRef called\n", this);
SLOCK_ACQUIRE();
refcnt++;
SLOCK_RELEASE();
return refcnt;
}
//
// acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
ULONG STDMETHODCALLTYPE IAsyncPG::Release(void)
{
mylog("%p->Release called refcnt=%d\n", this, refcnt);
SLOCK_ACQUIRE();
refcnt--;
if (refcnt <= 0)
{
SLOCK_RELEASE();
ELOCK_ACQUIRE();
LIFELOCK_ACQUIRE;
SLOCK_ACQUIRE();
if (refcnt <=0)
{
const int refcnt_copy = refcnt;
mylog("delete %p\n", this);
delete this;
return refcnt_copy;
}
else
{
SLOCK_RELEASE();
LIFELOCK_RELEASE;
ELOCK_RELEASE();
}
}
else
SLOCK_RELEASE();
return refcnt;
}
//
// Acquire/release SLOCK.
//
void IAsyncPG::Wait_pThread(bool slock_hold)
{
mylog("Wait_pThread %d in\n", slock_hold);
HANDLE wThread;
int wait_idx = PrepareExec;
DWORD ret;
if (!slock_hold)
SLOCK_ACQUIRE();
while (NULL != (wThread = eThread[wait_idx]) && !eFin[wait_idx])
{
SLOCK_RELEASE();
ret = WaitForSingleObject(wThread, 2000);
SLOCK_ACQUIRE();
if (WAIT_TIMEOUT != ret)
eFin[wait_idx] = true;
}
if (!slock_hold)
SLOCK_RELEASE();
mylog("Wait_pThread out\n");
}
//
// Acquire/releases SLOCK.
//
void IAsyncPG::Wait_cThread(bool slock_hold, bool once)
{
HANDLE wThread;
int wait_idx;
DWORD ret;
mylog("Wait_cThread %d,%d in\n", slock_hold, once);
if (!slock_hold)
SLOCK_ACQUIRE();
if (NULL != eThread[CommitExec])
wait_idx = CommitExec;
else
wait_idx = AbortExec;
while (NULL != (wThread = eThread[wait_idx]) && !eFin[wait_idx])
{
SLOCK_RELEASE();
ret = WaitForSingleObject(wThread, 2000);
SLOCK_ACQUIRE();
if (WAIT_TIMEOUT != ret)
eFin[wait_idx] = true;
else if (once)
break;
}
if (!slock_hold)
SLOCK_RELEASE();
mylog("Wait_cThread out\n");
}
/* Processing Prepare/Commit Request */
typedef
struct RequestPara {
DWORD type;
LPVOID lpr;
HRESULT res;
} RequestPara;
//
// Acquire/releases LIFELOCK -> SLOCK.
// may acquire/release ELOCK.
//
void IAsyncPG::SetDone(HRESULT res)
{
LIFELOCK_ACQUIRE;
SLOCK_ACQUIRE();
done = true;
if (E_FAIL == res ||
E_UNEXPECTED == res)
abort = true;
requestAccepted = true;
commit_result = res;
if (dtcconn)
{
PgDtc_set_async(dtcconn, NULL);
if (isolated)
{
SLOCK_RELEASE();
LIFELOCK_RELEASE;
ELOCK_ACQUIRE();
if (dtcconn)
{
mylog("Freeing isolated connection=%p\n", dtcconn);
PgDtc_free_connect(dtcconn);
SetConnection(NULL);
}
ELOCK_RELEASE();
}
else
{
dtcconn = NULL;
SLOCK_RELEASE();
LIFELOCK_RELEASE;
}
}
else
{
SLOCK_RELEASE();
LIFELOCK_RELEASE;
}
}
//
// Acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
void *IAsyncPG::generateXAConn(bool spinAcquired)
{
mylog("generateXAConn isolated=%d dtcconn=%p\n", isolated, dtcconn);
if (!spinAcquired)
SLOCK_ACQUIRE();
if (isolated || done)
{
SLOCK_RELEASE();
return dtcconn;
}
SLOCK_RELEASE();
ELOCK_ACQUIRE();
LIFELOCK_ACQUIRE;
SLOCK_ACQUIRE();
if (dtcconn && !isolated && !done && prepared)
{
void *sconn = dtcconn;
dtcconn = PgDtc_isolate(sconn, useAnotherRoom);
isolated = true;
SLOCK_RELEASE();
LIFELOCK_RELEASE;
// PgDtc_connect(dtcconn); may be called in getLockedXAConn
}
else
{
SLOCK_RELEASE();
LIFELOCK_RELEASE;
}
ELOCK_RELEASE();
return dtcconn;
}
//
// Acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
void *IAsyncPG::isolateXAConn(bool spinAcquired, bool continueConnection)
{
void *sconn;
mylog("isolateXAConn isolated=%d dtcconn=%p\n", isolated, dtcconn);
if (!spinAcquired)
SLOCK_ACQUIRE();
if (isolated || done || NULL == dtcconn)
{
SLOCK_RELEASE();
return dtcconn;
}
SLOCK_RELEASE();
ELOCK_ACQUIRE();
LIFELOCK_ACQUIRE;
SLOCK_ACQUIRE();
if (isolated || done || NULL == dtcconn)
{
SLOCK_RELEASE();
LIFELOCK_RELEASE;
ELOCK_RELEASE();
return dtcconn;
}
sconn = dtcconn;
dtcconn = PgDtc_isolate(sconn, continueConnection ? 0 : disposingConnection);
isolated = true;
SLOCK_RELEASE();
LIFELOCK_RELEASE;
if (continueConnection)
{
PgDtc_connect(sconn);
}
ELOCK_RELEASE();
return dtcconn;
}
//
// Acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
void *IAsyncPG::separateXAConn(bool spinAcquired, bool continueConnection)
{
mylog("%s isolated=%d dtcconn=%p\n", __FUNCTION__, isolated, dtcconn);
if (!spinAcquired)
SLOCK_ACQUIRE();
if (prepared)
return generateXAConn(true);
else
return isolateXAConn(true, continueConnection);
}
//
// [when entered]
// ELOCK is held.
//
// Acquire/releases SLOCK.
// Try to acquire CONN_CS also.
//
// [on exit]
// ELOCK is kept held.
// If the return connection != NULL
// the CONN_CS lock for the connection is held.
//
void *IAsyncPG::getLockedXAConn()
{
SLOCK_ACQUIRE();
while (!done && !isolated && NULL != dtcconn)
{
/*
* Note that COMMIT/ROLLBACK PREPARED command should be
* issued outside the transaction.
*/
if (!prepared || !CONN_IS_IN_TRANS(dtcconn))
{
if (TRY_CONN_CS_ACQUIRE(dtcconn))
{
if (prepared && CONN_IS_IN_TRANS(dtcconn))
{
CONN_CS_RELEASE(dtcconn);
}
else
break;
}
}
separateXAConn(true, true);
SLOCK_ACQUIRE(); // SLOCK was released by separateXAConn()
}
SLOCK_RELEASE();
if (isolated && NULL != dtcconn)
{
CONN_CS_ACQUIRE(dtcconn);
if (!PgDtc_get_property(dtcconn, connected))
PgDtc_connect(dtcconn);
}
return dtcconn;
}
//
// Acquire/release ELOCK -> SLOCK.
//
HRESULT IAsyncPG::RequestExec(DWORD type, HRESULT res)
{
HRESULT ret;
bool bReleaseEnlist = false;
void *econn;
char pgxid[258];
mylog("%p->RequestExec type=%d conn=%p\n", this, type, dtcconn);
XidToText(xid, pgxid);
ELOCK_ACQUIRE();
switch (type)
{
case PrepareExec:
if (done || NULL == dtcconn)
{
res = E_UNEXPECTED;
break;
}
if (econn = getLockedXAConn(), NULL != econn)
{
PgDtc_set_property(econn, inprogress, (void *) 1);
if (E_FAIL == res)
PgDtc_one_phase_operation(econn, ABORT_GLOBAL_TRANSACTION);
else if (XACT_S_SINGLEPHASE == res)
{
if (!PgDtc_one_phase_operation(econn, ONE_PHASE_COMMIT))
res = E_FAIL;
}
else
{
if (!PgDtc_two_phase_operation(econn, PREPARE_TRANSACTION, pgxid))
res = E_FAIL;
}
PgDtc_set_property(econn, inprogress, (void *) 0);
CONN_CS_RELEASE(econn);
}
if (S_OK != res)
{
SetDone(res);
bReleaseEnlist = true;
}
ret = enlist->PrepareRequestDone(res, NULL, NULL);
SetPrepareResult(res);
break;
case CommitExec:
Wait_pThread(false);
if (E_FAIL != res)
{
econn = getLockedXAConn();
if (econn)
{
PgDtc_set_property(econn, inprogress, (void *) 1);
if (!PgDtc_two_phase_operation(econn, COMMIT_PREPARED, pgxid))
res = E_FAIL;
PgDtc_set_property(econn, inprogress, (void *) 0);
CONN_CS_RELEASE(econn);
}
}
SetDone(res);
ret = enlist->CommitRequestDone(res);
bReleaseEnlist = true;
break;
case AbortExec:
Wait_pThread(false);
if (prepared && !done)
{
econn = getLockedXAConn();
if (econn)
{
PgDtc_set_property(econn, inprogress, (void *) 1);
if (!PgDtc_two_phase_operation(econn, ROLLBACK_PREPARED, pgxid))
res = E_FAIL;
PgDtc_set_property(econn, inprogress, (void *) 0);
CONN_CS_RELEASE(econn);
}
}
SetDone(res);
ret = enlist->AbortRequestDone(res);
bReleaseEnlist = true;
break;
default:
ret = -1;
}
if (bReleaseEnlist)
{
helper->ReleaseRMCookie(RMCookie, TRUE);
enlist->Release();
}
ELOCK_RELEASE();
mylog("%p->Done ret=%d\n", this, ret);
return ret;
}
//
// Acquire/releses SLOCK
// or [ELOCK -> LIFELOCK -> ] SLOCK.
//
HRESULT IAsyncPG::ReleaseConnection(void)
{
mylog("%p->ReleaseConnection\n", this);
SLOCK_ACQUIRE();
if (isolated || NULL == dtcconn)
{
SLOCK_RELEASE();
return SQL_SUCCESS;
}
Wait_pThread(true);
if (NULL != eThread[CommitExec] || NULL != eThread[AbortExec] || requestAccepted)
{
if (!done)
Wait_cThread(true, true);
}
if (!isolated && !done && dtcconn && PgDtc_get_property(dtcconn, connected))
{
isolateXAConn(true, false);
}
else
SLOCK_RELEASE();
mylog("%p->ReleaseConnection exit\n", this);
return SQL_SUCCESS;
}
EXTERN_C static unsigned WINAPI DtcRequestExec(LPVOID para);
EXTERN_C static void __cdecl ClosePrepareThread(LPVOID para);
EXTERN_C static void __cdecl CloseCommitThread(LPVOID para);
EXTERN_C static void __cdecl CloseAbortThread(LPVOID para);
//
// Acquire/release [ELOCK -> ] SLOCK.
//
HRESULT STDMETHODCALLTYPE IAsyncPG::PrepareRequest(BOOL fRetaining, DWORD grfRM,
BOOL fWantMoniker, BOOL fSinglePhase)
{
HRESULT ret, res;
RequestPara *reqp;
const DWORD reqtype = PrepareExec;
mylog("%p PrepareRequest called grhRM=%d enl=%p\n", this, grfRM, enlist);
SLOCK_ACQUIRE();
if (dtcconn && 0 != PgDtc_get_property(dtcconn, errorNumber))
res = ret = E_FAIL;
else
{
ret = S_OK;
if (fSinglePhase)
{
res = XACT_S_SINGLEPHASE;
mylog("XACT is singlePhase\n");
}
else
res = S_OK;
}
SLOCK_RELEASE();
ELOCK_ACQUIRE();
#ifdef _SLEEP_FOR_TEST_
Sleep(2000);
#endif /* _SLEEP_FOR_TEST_ */
reqp = new RequestPara;
reqp->type = reqtype;
reqp->lpr = (LPVOID) this;
reqp->res = res;
#define DONT_CALL_RETURN_FROM_HERE ???
AddRef();
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, DtcRequestExec, reqp, 0, NULL);
if (NULL == hThread)
{
delete(reqp);
ret = E_FAIL;
}
else
{
SLOCK_ACQUIRE();
eThread[reqtype] = hThread;
SLOCK_RELEASE();
/*
* We call here _beginthread not _beginthreadex
* so as not to call CloseHandle() to clean up
* the thread.
*/
_beginthread(ClosePrepareThread, 0, (void *) this);
}
ELOCK_RELEASE();
Release();
#undef return
return ret;
}
//
// Acquire/release [ELOCK -> ] SLOCK.
//
HRESULT STDMETHODCALLTYPE IAsyncPG::CommitRequest(DWORD grfRM, XACTUOW * pNewUOW)
{
HRESULT res = S_OK, ret = S_OK;
RequestPara *reqp;
const DWORD reqtype = CommitExec;
mylog("%p CommitRequest called grfRM=%d enl=%p\n", this, grfRM, enlist);
SLOCK_ACQUIRE();
if (!prepared || done)
ret = E_UNEXPECTED;
else if (S_OK != prepare_result)
ret = E_UNEXPECTED;
SLOCK_RELEASE();
if (S_OK != ret)
return ret;
#define DONT_CALL_RETURN_FROM_HERE ???
AddRef();
ELOCK_ACQUIRE();
#ifdef _SLEEP_FOR_TEST_
Sleep(1000);
#endif /* _SLEEP_FOR_TEST_ */
reqp = new RequestPara;
reqp->type = reqtype;
reqp->lpr = (LPVOID) this;
reqp->res = res;
enlist->AddRef();
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, DtcRequestExec, reqp, 0, NULL);
if (NULL == hThread)
{
delete(reqp);
enlist->Release();
ret = E_FAIL;
}
else
{
SLOCK_ACQUIRE();
eThread[reqtype] = hThread;
SLOCK_RELEASE();
/*
* We call here _beginthread not _beginthreadex
* so as not to call CloseHandle() to clean up
* the thread.
*/
_beginthread(CloseCommitThread, 0, (void *) this);
}
mylog("CommitRequest ret=%d\n", ret);
requestAccepted = true;
ELOCK_RELEASE();
Release();
#undef return
return ret;
}
//
// Acquire/release [ELOCK -> ] SLOCK.
//
HRESULT STDMETHODCALLTYPE IAsyncPG::AbortRequest(BOID * pboidReason, BOOL fRetaining,
XACTUOW * pNewUOW)
{
HRESULT res = S_OK, ret = S_OK;
RequestPara *reqp;
const DWORD reqtype = AbortExec;
mylog("%p AbortRequest called\n", this);
SLOCK_ACQUIRE();
if (done)
ret = E_UNEXPECTED;
else if (prepared && S_OK != prepare_result)
ret = E_UNEXPECTED;
SLOCK_RELEASE();
if (S_OK != ret)
return ret;
#define return DONT_CALL_RETURN_FROM_HERE ???
AddRef();
ELOCK_ACQUIRE();
if (!prepared && dtcconn)
{
PgDtc_set_property(dtcconn, inprogress, (void *) 1);
PgDtc_one_phase_operation(dtcconn, ONE_PHASE_ROLLBACK);
PgDtc_set_property(dtcconn, inprogress, (void *) 0);
}
reqp = new RequestPara;
reqp->type = reqtype;
reqp->lpr = (LPVOID) this;
reqp->res = res;
enlist->AddRef();
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, DtcRequestExec, reqp, 0, NULL);
if (NULL == hThread)
{
delete(reqp);
enlist->Release();
ret = E_FAIL;
}
else
{
SLOCK_ACQUIRE();
eThread[reqtype] = hThread;
SLOCK_RELEASE();
/*
* We call here _beginthread not _beginthreadex
* so as not to call CloseHandle() to clean up
* the thread.
*/
_beginthread(CloseAbortThread, 0, (void *) this);
}
mylog("AbortRequest ret=%d\n", ret);
requestAccepted = true;
ELOCK_RELEASE();
Release();
#undef return
return ret;
}
HRESULT STDMETHODCALLTYPE IAsyncPG::TMDown(void)
{
mylog("%p TMDown called\n", this);
return S_OK;
}
bool IAsyncPG::CloseThread(DWORD type)
{
CSTR func = "CloseThread";
HANDLE th;
DWORD ret, excode = S_OK;
bool rls_async = false;
mylog("%s for %p thread=%d\n", func, this, eThread[type]);
if (th = eThread[type], NULL == th || eFin[type])
return false;
ret = WaitForSingleObject(th, INFINITE);
if (WAIT_OBJECT_0 == ret)
{
switch (type)
{
case IAsyncPG::AbortExec:
case IAsyncPG::CommitExec:
rls_async = true;
break;
default:
GetExitCodeThread(th, &excode);
if (S_OK != excode)
rls_async = true;
}
SLOCK_ACQUIRE();
eThread[type] = NULL;
eFin[type] = true;
SLOCK_RELEASE();
CloseHandle(th);
}
mylog("%s ret=%d\n", func, ret);
return rls_async;
}
EXTERN_C static void __cdecl ClosePrepareThread(LPVOID para)
{
CSTR func = "ClosePrepareThread";
IAsyncPG *async = (IAsyncPG *) para;
bool release;
mylog("%s for %p", func, async);
if (release = async->CloseThread(IAsyncPG::PrepareExec), release)
async->Release();
mylog("%s release=%d\n", func, release);
}
EXTERN_C static void __cdecl CloseCommitThread(LPVOID para)
{
CSTR func = "CloseCommitThread";
IAsyncPG *async = (IAsyncPG *) para;
bool release;
mylog("%s for %p", func, async);
if (release = async->CloseThread(IAsyncPG::CommitExec), release)
async->Release();
mylog("%s release=%d\n", func, release);
}
EXTERN_C static void __cdecl CloseAbortThread(LPVOID para)
{
CSTR func = "CloseAbortThread";
IAsyncPG *async = (IAsyncPG *) para;
bool release;
mylog("%s for %p", func, async);
if (release = async->CloseThread(IAsyncPG::AbortExec), release)
async->Release();
mylog("%s release=%d\n", func, release);
}
EXTERN_C static unsigned WINAPI DtcRequestExec(LPVOID para)
{
RequestPara *reqp = (RequestPara *) para;
DWORD type = reqp->type;
IAsyncPG *async = (IAsyncPG *) reqp->lpr;
HRESULT res = reqp->res, ret;
mylog("DtcRequestExec type=%d", reqp->type);
delete(reqp);
ret = async->RequestExec(type, res);
mylog(" Done ret=%d\n", ret);
return ret;
}
CSTR regKey = "SOFTWARE\\Microsoft\\MSDTC\\XADLL";
static int regkeyCheck(const char *xalibname, const char *xalibpath)
{
int retcode = 0;
LONG ret;
HKEY sKey;
DWORD rSize;
ret = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, regKey, 0, KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_64KEY, &sKey);
switch (ret)
{
case ERROR_SUCCESS:
break;
case ERROR_FILE_NOT_FOUND: