00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #include "Windows4Root.h"
00084 #include <windows.h>
00085
00086 #include "TGWin32ProxyBase.h"
00087 #include "TRefCnt.h"
00088 #include "TList.h"
00089 #include "TGWin32.h"
00090 #include "TROOT.h"
00091
00092
00093 class TGWin32CallBackObject : public TObject {
00094 public:
00095 TGWin32CallBack fCallBack;
00096 void *fParam;
00097
00098 TGWin32CallBackObject(TGWin32CallBack cb,void *p):fCallBack(cb),fParam(p) {}
00099 ~TGWin32CallBackObject() { if (fParam) delete fParam; }
00100 };
00101
00102
00103 class TGWin32ProxyBasePrivate {
00104 public:
00105 HANDLE fEvent;
00106 TGWin32ProxyBasePrivate();
00107 ~TGWin32ProxyBasePrivate();
00108 };
00109
00110
00111 TGWin32ProxyBasePrivate::TGWin32ProxyBasePrivate()
00112 {
00113
00114
00115 fEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
00116 }
00117
00118
00119 TGWin32ProxyBasePrivate::~TGWin32ProxyBasePrivate()
00120 {
00121
00122
00123 if (fEvent) ::CloseHandle(fEvent);
00124 fEvent = 0;
00125 }
00126
00127
00128 ULong_t TGWin32ProxyBase::fgPostMessageId = 0;
00129 ULong_t TGWin32ProxyBase::fgPingMessageId = 0;
00130 ULong_t TGWin32ProxyBase::fgMainThreadId = 0;
00131 ULong_t TGWin32ProxyBase::fgUserThreadId = 0;
00132 Long_t TGWin32ProxyBase::fgLock = 0;
00133 UInt_t TGWin32ProxyBase::fMaxResponseTime = 0;
00134
00135
00136
00137 TGWin32ProxyBase::TGWin32ProxyBase()
00138 {
00139
00140
00141 fIsVirtualX = kFALSE;
00142 fCallBack = 0;
00143 fParam = 0;
00144 fListOfCallBacks = new TList();
00145 fBatchLimit = 100;
00146 fId = ::GetCurrentThreadId();
00147 fPimpl = new TGWin32ProxyBasePrivate();
00148
00149 if (!fgPostMessageId) fgPostMessageId = ::RegisterWindowMessage("TGWin32ProxyBase::Post");
00150 if (!fgPingMessageId) fgPingMessageId = ::RegisterWindowMessage("TGWin32ProxyBase::Ping");
00151 }
00152
00153
00154 TGWin32ProxyBase::~TGWin32ProxyBase()
00155 {
00156
00157
00158 fListOfCallBacks->Delete();
00159 delete fListOfCallBacks;
00160 fListOfCallBacks = 0;
00161
00162 delete fPimpl;
00163 }
00164
00165
00166 void TGWin32ProxyBase::Lock()
00167 {
00168
00169
00170 TGWin32::Lock();
00171 }
00172
00173
00174 void TGWin32ProxyBase::Unlock()
00175 {
00176
00177
00178 TGWin32::Unlock();
00179 }
00180
00181
00182 void TGWin32ProxyBase::GlobalLock()
00183 {
00184
00185
00186 if (IsGloballyLocked()) return;
00187 ::InterlockedIncrement(&fgLock);
00188 }
00189
00190
00191 void TGWin32ProxyBase::GlobalUnlock()
00192 {
00193
00194
00195 if (!IsGloballyLocked()) return;
00196 ::InterlockedDecrement(&fgLock);
00197 }
00198
00199
00200 Bool_t TGWin32ProxyBase::Ping()
00201 {
00202
00203
00204 return ::PostThreadMessage(fgMainThreadId, fgPingMessageId, (WPARAM)0, 0L);
00205 }
00206
00207
00208 Double_t TGWin32ProxyBase::GetMilliSeconds()
00209 {
00210
00211
00212 static LARGE_INTEGER freq;
00213 static Bool_t first = kTRUE;
00214 LARGE_INTEGER count;
00215 static Double_t overhead = 0;
00216
00217 if (first) {
00218 LARGE_INTEGER count0;
00219 ::QueryPerformanceFrequency(&freq);
00220 ::QueryPerformanceCounter(&count0);
00221 if (1) {
00222 Double_t dummy;
00223 dummy = ((Double_t)count0.QuadPart - overhead)*1000./((Double_t)freq.QuadPart);
00224 }
00225 ::QueryPerformanceCounter(&count);
00226 overhead = (Double_t)count.QuadPart - (Double_t)count0.QuadPart;
00227 first = kFALSE;
00228 }
00229
00230 ::QueryPerformanceCounter(&count);
00231 return ((Double_t)count.QuadPart - overhead)*1000./((Double_t)freq.QuadPart);
00232 }
00233
00234
00235 void TGWin32ProxyBase::ExecuteCallBack(Bool_t sync)
00236 {
00237
00238
00239
00240
00241 if (fListOfCallBacks && fListOfCallBacks->GetSize()) {
00242 TIter next(fListOfCallBacks);
00243 TGWin32CallBackObject *obj;
00244
00245 while ((obj = (TGWin32CallBackObject*)next())) {
00246 obj->fCallBack(obj->fParam);
00247 }
00248 }
00249 if (sync) {
00250 if (fCallBack) fCallBack(fParam);
00251 ::SetEvent(fPimpl->fEvent);
00252 }
00253 }
00254
00255
00256 Bool_t TGWin32ProxyBase::ForwardCallBack(Bool_t sync)
00257 {
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 Int_t wait = 0;
00268
00269 if (!fgMainThreadId) return kFALSE;
00270
00271 while (IsGloballyLocked()) {
00272 Ping();
00273 if (GetCurrentThreadId() == fgMainThreadId)
00274 break;
00275 ::SleepEx(10, 1);
00276 if (!fgMainThreadId) return kFALSE;
00277 }
00278
00279 Bool_t batch = !sync && (fListOfCallBacks->GetSize() < fBatchLimit);
00280
00281
00282
00283 if (!fgUserThreadId && fIsVirtualX &&
00284 (GetCurrentThreadId() != fgMainThreadId) &&
00285 (fListOfCallBacks->GetSize() < fBatchLimit))
00286 batch = kTRUE;
00287
00288 if (batch) {
00289 fListOfCallBacks->Add(new TGWin32CallBackObject(fCallBack, fParam));
00290 return kTRUE;
00291 }
00292
00293 while (!::PostThreadMessage(fgMainThreadId, fgPostMessageId, (WPARAM)this, 0L)) {
00294
00295 ::SleepEx(50, 1);
00296 if (wait++ > 5) return kFALSE;
00297 }
00298
00299 Int_t cnt = 0;
00300
00301 DWORD res = WAIT_TIMEOUT;
00302 while (res == WAIT_TIMEOUT) {
00303 res = ::WaitForSingleObject(fPimpl->fEvent, 100);
00304 if ((GetCurrentThreadId() == fgMainThreadId) ||
00305 (!gROOT->IsLineProcessing() && IsGloballyLocked())) {
00306 break;
00307 }
00308 if (cnt++ > 20) break;
00309 }
00310 ::ResetEvent(fPimpl->fEvent);
00311
00312 if (res == WAIT_TIMEOUT) {
00313 GlobalLock();
00314 return kTRUE;
00315 }
00316
00317 fListOfCallBacks->Delete();
00318 return kFALSE;
00319 }
00320
00321
00322 Bool_t TGWin32ProxyBase::IsGloballyLocked()
00323 {
00324
00325
00326 return fgLock;
00327 }
00328
00329
00330 void TGWin32ProxyBase::SendExitMessage()
00331 {
00332
00333
00334 ::PostThreadMessage(fgMainThreadId, WM_QUIT, 0, 0L);
00335 }
00336