allegdb.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*-------------------------------------------------------------------------
  2. allegdb.cpp
  3. Implementation of OLE DB layer for Allegiance
  4. Owner:
  5. Copyright 1986-2000 Microsoft Corporation, All Rights Reserved
  6. *-----------------------------------------------------------------------*/
  7. #include "pch.h"
  8. #include "allegdb.h"
  9. unsigned CALLBACK SQLQueueProc(void * pvSQLQueueThread)
  10. {
  11. CSQLQueueThread * pSQLQueueThread = (CSQLQueueThread *) pvSQLQueueThread;
  12. HANDLE rgeventHandles[1];
  13. rgeventHandles[0] = pSQLQueueThread->GetEventDie();
  14. DWORD cHandles = sizeof(rgeventHandles) / sizeof(rgeventHandles[0]);
  15. DWORD dwWait = WAIT_TIMEOUT;
  16. do
  17. {
  18. //Wait until either we get a message or we ran out of time
  19. dwWait = MsgWaitForMultipleObjects(cHandles, rgeventHandles, FALSE, INFINITE, QS_ALLINPUT);
  20. // Process the message queue, if any messages were received
  21. static MSG msg;
  22. while (WAIT_OBJECT_0 != dwWait && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  23. {
  24. // dispatch Windows Messages to allow for the admin tool's COM to work
  25. TranslateMessage(&msg);
  26. switch (msg.message)
  27. {
  28. case wm_sql_addquery:
  29. {
  30. pSQLQueueThread->AddQuery((CSQLQuery*) msg.lParam);
  31. break;
  32. }
  33. case WM_QUIT:
  34. dwWait = WAIT_OBJECT_0; // let the thread be shutdown by sending a quit, or signalling pSqlThread->m_hEventExit
  35. break;
  36. default:
  37. ZError("SQLThreadProc: Unexpected thread message.\n");
  38. }
  39. }
  40. } while (WAIT_OBJECT_0 != dwWait);
  41. return 0;
  42. }
  43. unsigned CALLBACK SQLThreadProc(void * pvsqlThread)
  44. {
  45. CSQLThread * pSqlThread = (CSQLThread *) pvsqlThread;
  46. CSQLCore * psql = pSqlThread->GetSQLCore();
  47. HRESULT hr = E_FAIL;
  48. hr = pSqlThread->Open();
  49. if (FAILED(hr))
  50. return hr;
  51. HANDLE rgeventHandles[2];
  52. rgeventHandles[0] = pSqlThread->GetEventDie();
  53. rgeventHandles[1] = pSqlThread->GetNotify() ? psql->GetNotifySemaphore() : psql->GetSilentSemaphore();
  54. DWORD cHandles = sizeof(rgeventHandles) / sizeof(rgeventHandles[0]);
  55. DWORD dwWait = WAIT_TIMEOUT;
  56. do
  57. {
  58. //Wait until either we get a message or we ran out of time
  59. dwWait = WaitForMultipleObjects(cHandles, rgeventHandles, FALSE, INFINITE);
  60. switch (dwWait)
  61. {
  62. case WAIT_OBJECT_0: // pSqlThread->m_hEventExit
  63. break;
  64. case WAIT_OBJECT_0 + 1: // spemaphore for the queue we're servicing
  65. // We may not actually get the query that we were signaled for, but that doesn't matter
  66. hr = pSqlThread->ServiceQuery(pSqlThread->GetNotify() ? psql->GetNotifyQuery() : psql->GetSilentQuery());
  67. break;
  68. default:
  69. ZError("Unexpected object signaled in SQLThreadProc.\n");
  70. }
  71. } while (WAIT_OBJECT_0 != dwWait);
  72. return 0;
  73. }
  74. HRESULT CSQLCore::Init(LPCOLESTR strSQLConfig, DWORD nThreadIDNotify, DWORD cSilentThreads,
  75. DWORD cNotifyThreads)
  76. {
  77. HRESULT hr = E_FAIL;
  78. if (cSilentThreads + cNotifyThreads < 1 ||
  79. cSilentThreads + cNotifyThreads > 20) // must have between 1 and 20 threads
  80. return E_INVALIDARG;
  81. m_nThreadIDNotify = nThreadIDNotify;
  82. m_cSilentThreads = cSilentThreads;
  83. m_cNotifyThreads = cNotifyThreads;
  84. // create event used to signal that ALL sql threads should exit
  85. m_hKillSQLEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  86. assert(m_hKillSQLEvent);
  87. hr = m_connection.OpenFromInitializationString(strSQLConfig);
  88. if (FAILED(hr))
  89. {
  90. DumpErrorInfo(m_connection.m_spInit, IID_IDBInitialize, NULL);
  91. return hr;
  92. }
  93. m_hQueryNotify = CreateSemaphore(NULL, 0, MAXLONG, NULL);
  94. m_hQuerySilent = CreateSemaphore(NULL, 0, MAXLONG, NULL);
  95. m_pthdQueue = new CSQLQueueThread(m_hKillSQLEvent, this);
  96. assert (m_pthdQueue);
  97. m_pargSilentThreads = (CSQLThread **) new char[sizeof(CSQLThread *) * m_cSilentThreads];
  98. m_pargNotifyThreads = (CSQLThread **) new char[sizeof(CSQLThread *) * m_cNotifyThreads];
  99. DWORD i = 0;
  100. for (i = 0; i < m_cNotifyThreads; i++)
  101. {
  102. m_pargNotifyThreads[i] = new CSQLThread(m_hKillSQLEvent, true, this);
  103. }
  104. for (i = 0; i < m_cSilentThreads; i++)
  105. {
  106. m_pargSilentThreads[i] = new CSQLThread(m_hKillSQLEvent, false, this);
  107. }
  108. return hr;
  109. }
  110. CSQLCore::~CSQLCore()
  111. {
  112. DWORD i = 0;
  113. for (i = 0; i < m_cNotifyThreads && m_pargNotifyThreads; i++)
  114. {
  115. // TODO: cleanup m_listQuries
  116. delete m_pargNotifyThreads[i];
  117. }
  118. delete [] m_pargNotifyThreads;
  119. for (i = 0; i < m_cSilentThreads && m_pargSilentThreads; i++)
  120. {
  121. // TODO: cleanup m_listQuries
  122. delete m_pargSilentThreads[i];
  123. }
  124. delete [] m_pargSilentThreads;
  125. CloseHandle(m_hQueryNotify);
  126. CloseHandle(m_hQuerySilent);
  127. }
  128. void CSQLCore::AddQuery(CSQLQuery * pquery)
  129. {
  130. HANDLE h = NULL;
  131. if (pquery->GetNotify())
  132. {
  133. m_listQueriesNotify.last(pquery);
  134. h = GetNotifySemaphore();
  135. }
  136. else
  137. {
  138. m_listQueriesSilent.last(pquery);
  139. h = GetSilentSemaphore();
  140. }
  141. ReleaseSemaphore(h, 1, NULL); // signal some thread to pick up the query
  142. }
  143. void CSQLCore::PostQuery(CSQLQuery * pquery)
  144. {
  145. PostThreadMessage(m_pthdQueue->GetThreadID(), wm_sql_addquery, (WPARAM) NULL, (LPARAM) pquery);
  146. }
  147. HRESULT CSQLThread::ServiceQuery(CSQLQuery * pqueryNew)
  148. {
  149. CSQLQuery * pqueryCache = NULL;
  150. REFGUID guid = pqueryNew->GetGuid();
  151. HRESULT hrQuery = E_FAIL; // it is important that ONLY results from calls to pqueryCache be assigned here
  152. // See if this query is already in the per-thread cache
  153. for (SLinkQueries * plinkQuery = m_listQueries.first(); plinkQuery && !pqueryCache; plinkQuery = plinkQuery->next())
  154. {
  155. CSQLQuery * pqueryT = plinkQuery->data();
  156. if (pqueryT->GetGuid() == guid)
  157. {
  158. // make sure we didn't accidentally use the same guid twice for different queries
  159. assert(!lstrcmp(pqueryNew->GetStrQuery(), pqueryT->GetStrQuery()));
  160. pqueryCache = pqueryT;
  161. }
  162. }
  163. if (!pqueryCache) // the first time this thread has seen this query, so let's add it to our reportoire
  164. {
  165. //pqueryCache = pqueryNew;
  166. pqueryCache = pqueryNew->Copy(NULL);
  167. m_listQueries.first(pqueryCache);
  168. hrQuery = pqueryCache->OnPrepare(m_session);
  169. }
  170. else
  171. {
  172. hrQuery = S_OK;
  173. pqueryNew->Copy(pqueryCache);
  174. }
  175. bool fRetry = SUCCEEDED(hrQuery);
  176. int cRetries = 0;
  177. while (fRetry)
  178. {
  179. hrQuery = pqueryCache->OnExecute();
  180. if (SUCCEEDED(hrQuery))
  181. {
  182. fRetry = false;
  183. }
  184. else
  185. {
  186. m_psql->DumpErrorInfo(pqueryCache->GetIUnknown(), IID_ICommand, &fRetry);
  187. if (fRetry)
  188. {
  189. debugf("Query deadlocked or timed-out. Retry #%d\n", ++cRetries);
  190. Sleep(50 * cRetries);
  191. }
  192. }
  193. }
  194. if (FAILED(hrQuery))
  195. m_psql->DumpErrorInfo(pqueryCache->GetIUnknown(), IID_ICommand, NULL);
  196. pqueryCache->Copy(pqueryNew);
  197. // We post it whether the client requested notification or not, because at least we need to let the query clean up itself
  198. if (pqueryNew->GetCallbackOnMainThread())
  199. {
  200. BOOL bPosted = PostThreadMessage(GetSQLCore()->GetNotifyThreadID(), wm_sql_querydone, (WPARAM) NULL, (LPARAM) pqueryNew);
  201. if (!bPosted)
  202. {
  203. // If the PostThreadMessage failed because the thread id is gone, we still need to clean up
  204. DWORD dwLastError = ::GetLastError();
  205. if (ERROR_INVALID_THREAD_ID == dwLastError)
  206. {
  207. pqueryNew->DataReady();
  208. }
  209. }
  210. }
  211. else
  212. {
  213. pqueryNew->DataReady();
  214. }
  215. return hrQuery;
  216. }