RingBuffer.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "hpsocket/GlobalDef.h"
  25. #include "Singleton.h"
  26. #include "STLHelper.h"
  27. #include "FuncHelper.h"
  28. #include "RWLock.h"
  29. using namespace std;
  30. #define CACHE_LINE 64
  31. #define PACK_SIZE_OF(T) (CACHE_LINE - sizeof(T) % CACHE_LINE)
  32. #if __WORDSIZE == 32
  33. #pragma pack(push, 4)
  34. #endif
  35. // ------------------------------------------------------------------------------------------------------------- //
  36. template <class T, class index_type = DWORD, bool adjust_index = false> class CRingCache
  37. {
  38. public:
  39. enum EnGetResult {GR_FAIL = -1, GR_INVALID = 0, GR_VALID = 1};
  40. typedef T* TPTR;
  41. typedef volatile T* VTPTR;
  42. typedef unordered_set<index_type> IndexSet;
  43. typedef typename IndexSet::const_iterator IndexSetCI;
  44. typedef typename IndexSet::iterator IndexSetI;
  45. static TPTR const E_EMPTY;
  46. static TPTR const E_LOCKED;
  47. static TPTR const E_MAX_STATUS;
  48. public:
  49. static index_type& INDEX_INC(index_type& dwIndex) {if(adjust_index) ++dwIndex; return dwIndex;}
  50. static index_type& INDEX_DEC(index_type& dwIndex) {if(adjust_index) --dwIndex; return dwIndex;}
  51. private:
  52. index_type& INDEX_V2R(index_type& dwIndex) {dwIndex %= m_dwSize; if(dwIndex == 0) dwIndex = m_dwSize; return dwIndex;}
  53. VTPTR& INDEX_VAL(index_type dwIndex) {return *(m_pv + dwIndex);}
  54. public:
  55. BOOL Put(TPTR pElement, index_type& dwIndex)
  56. {
  57. ASSERT(pElement != nullptr);
  58. if(!IsValid()) return FALSE;
  59. BOOL isOK = FALSE;
  60. while(true)
  61. {
  62. if(!HasSpace())
  63. break;
  64. DWORD dwCurSeq = m_dwCurSeq;
  65. index_type dwCurIndex = dwCurSeq % m_dwSize;
  66. VTPTR& pValue = INDEX_VAL(dwCurIndex);
  67. if(pValue == E_EMPTY)
  68. {
  69. if(::InterlockedCompareExchangePointer(&pValue, pElement, E_EMPTY) == E_EMPTY)
  70. {
  71. ::InterlockedIncrement(&m_dwCount);
  72. ::InterlockedCompareExchange(&m_dwCurSeq, dwCurSeq + 1, dwCurSeq);
  73. dwIndex = INDEX_INC(dwCurIndex);
  74. isOK = TRUE;
  75. if(pElement != E_LOCKED)
  76. EmplaceIndex(dwIndex);
  77. break;
  78. }
  79. }
  80. ::InterlockedCompareExchange(&m_dwCurSeq, dwCurSeq + 1, dwCurSeq);
  81. }
  82. return isOK;
  83. }
  84. EnGetResult GetEx(index_type dwIndex, TPTR* ppElement)
  85. {
  86. return Get(INDEX_V2R(dwIndex), ppElement);
  87. }
  88. EnGetResult Get(index_type dwIndex, TPTR* ppElement)
  89. {
  90. ASSERT(dwIndex <= m_dwSize);
  91. ASSERT(ppElement != nullptr);
  92. if(!IsValid() || INDEX_DEC(dwIndex) >= m_dwSize)
  93. {
  94. *ppElement = nullptr;
  95. return GR_FAIL;
  96. }
  97. *ppElement = (TPTR)INDEX_VAL(dwIndex);
  98. return IsValidElement(*ppElement) ? GR_VALID : GR_INVALID;
  99. }
  100. BOOL SetEx(index_type dwIndex, TPTR pElement, TPTR* ppOldElement = nullptr)
  101. {
  102. return Set(INDEX_V2R(dwIndex), pElement, ppOldElement);
  103. }
  104. BOOL Set(index_type dwIndex, TPTR pElement, TPTR* ppOldElement = nullptr)
  105. {
  106. TPTR pElement2 = nullptr;
  107. if(Get(dwIndex, &pElement2) == GR_FAIL)
  108. return FALSE;
  109. if(ppOldElement != nullptr)
  110. *ppOldElement = pElement2;
  111. if(pElement == pElement2)
  112. return FALSE;
  113. int f1 = 0;
  114. int f2 = 0;
  115. if(pElement == E_EMPTY)
  116. {
  117. if(pElement2 == E_LOCKED)
  118. f1 = -1;
  119. else
  120. f1 = f2 = -1;
  121. }
  122. else if(pElement == E_LOCKED)
  123. {
  124. if(pElement2 == E_EMPTY)
  125. f1 = 1;
  126. else
  127. f2 = -1;
  128. }
  129. else
  130. {
  131. if(pElement2 == E_EMPTY)
  132. f1 = f2 = 1;
  133. else if(pElement2 == E_LOCKED)
  134. f2 = 1;
  135. }
  136. BOOL bSetValueFirst = (f1 + f2 >= 0);
  137. index_type dwOuterIndex = dwIndex;
  138. INDEX_DEC(dwIndex);
  139. if(bSetValueFirst) INDEX_VAL(dwIndex) = pElement;
  140. if(f1 > 0) ::InterlockedIncrement(&m_dwCount);
  141. if(f2 != 0) (f2 > 0) ? EmplaceIndex(dwOuterIndex) : EraseIndex(dwOuterIndex);
  142. if(f1 < 0) ::InterlockedDecrement(&m_dwCount);
  143. if(!bSetValueFirst) INDEX_VAL(dwIndex) = pElement;
  144. ASSERT(Spaces() <= Size());
  145. return TRUE;
  146. }
  147. BOOL RemoveEx(index_type dwIndex, TPTR* ppElement = nullptr)
  148. {
  149. return Remove(INDEX_V2R(dwIndex), ppElement);
  150. }
  151. BOOL Remove(index_type dwIndex, TPTR* ppElement = nullptr)
  152. {
  153. return Set(dwIndex, E_EMPTY, ppElement);
  154. }
  155. BOOL AcquireLock(index_type& dwIndex)
  156. {
  157. return Put(E_LOCKED, dwIndex);
  158. }
  159. BOOL ReleaseLock(index_type dwIndex, TPTR pElement)
  160. {
  161. ASSERT(pElement == nullptr || IsValidElement(pElement));
  162. TPTR pElement2 = nullptr;
  163. Get(dwIndex, &pElement2);
  164. ASSERT(pElement2 == E_LOCKED);
  165. if(pElement2 != E_LOCKED)
  166. return FALSE;
  167. return Set(dwIndex, pElement);
  168. }
  169. public:
  170. void Reset(DWORD dwSize = 0)
  171. {
  172. if(IsValid())
  173. Destroy();
  174. if(dwSize > 0)
  175. Create(dwSize);
  176. }
  177. BOOL GetAllElementIndexes(index_type ids[], DWORD& dwCount, BOOL bCopy = TRUE)
  178. {
  179. DWORD dwSize = Elements();
  180. if(ids == nullptr || dwCount == 0)
  181. {
  182. dwCount = dwSize;
  183. return FALSE;
  184. }
  185. if(dwSize == 0)
  186. {
  187. dwCount = 0;
  188. return TRUE;
  189. }
  190. IndexSet* pIndexes = &m_indexes;
  191. if(bCopy)
  192. {
  193. pIndexes = new IndexSet;
  194. CopyIndexes(*pIndexes);
  195. }
  196. DWORD i = 0;
  197. for(auto it = pIndexes->begin(), end = pIndexes->end(); i < dwCount && it != end; ++i, ++it)
  198. ids[i] = *it;
  199. if(bCopy) delete pIndexes;
  200. dwCount = i;
  201. return TRUE;
  202. }
  203. unique_ptr<index_type[]> GetAllElementIndexes(DWORD& dwCount, BOOL bCopy = TRUE)
  204. {
  205. dwCount = (DWORD)m_indexes.size();
  206. unique_ptr<index_type[]> ids(new index_type[dwCount]);
  207. if(dwCount > 0)
  208. GetAllElementIndexes(ids.get(), dwCount, bCopy);
  209. return ids;
  210. }
  211. IndexSet& CopyIndexes(IndexSet& indexes)
  212. {
  213. {
  214. CReadLock locallock(m_cs);
  215. indexes = m_indexes;
  216. }
  217. return indexes;
  218. }
  219. static BOOL IsValidElement(TPTR pElement) {return pElement > E_MAX_STATUS;}
  220. IndexSet& Indexes () { return m_indexes;}
  221. DWORD Size () {return m_dwSize;}
  222. DWORD Elements () {return (DWORD)m_indexes.size();}
  223. DWORD Spaces () {return m_dwSize - m_dwCount;}
  224. BOOL HasSpace () {return m_dwCount < m_dwSize;}
  225. BOOL IsEmpty () {return m_dwCount == 0;}
  226. BOOL IsValid () {return m_pv != nullptr;}
  227. private:
  228. void Create(DWORD dwSize)
  229. {
  230. ASSERT(!IsValid() && dwSize > 0);
  231. m_dwCurSeq = 0;
  232. m_dwCount = 0;
  233. m_dwSize = dwSize;
  234. m_pv = (VTPTR*)malloc(m_dwSize * sizeof(TPTR));
  235. ::ZeroMemory(m_pv, m_dwSize * sizeof(TPTR));
  236. }
  237. void Destroy()
  238. {
  239. ASSERT(IsValid());
  240. m_indexes.clear();
  241. free((void*)m_pv);
  242. m_pv = nullptr;
  243. m_dwSize = 0;
  244. m_dwCount = 0;
  245. m_dwCurSeq = 0;
  246. }
  247. void EmplaceIndex(index_type dwIndex)
  248. {
  249. CWriteLock locallock(m_cs);
  250. m_indexes.emplace(dwIndex);
  251. }
  252. void EraseIndex(index_type dwIndex)
  253. {
  254. CWriteLock locallock(m_cs);
  255. m_indexes.erase(dwIndex);
  256. }
  257. public:
  258. CRingCache (DWORD dwSize = 0)
  259. : m_pv (nullptr)
  260. , m_dwSize (0)
  261. , m_dwCount (0)
  262. , m_dwCurSeq(0)
  263. {
  264. Reset(dwSize);
  265. }
  266. ~CRingCache()
  267. {
  268. Reset(0);
  269. }
  270. private:
  271. CRingCache(const CRingCache&);
  272. CRingCache operator = (const CRingCache&);
  273. private:
  274. DWORD m_dwSize;
  275. VTPTR* m_pv;
  276. char pack1[PACK_SIZE_OF(VTPTR*)];
  277. volatile DWORD m_dwCurSeq;
  278. char pack2[PACK_SIZE_OF(DWORD)];
  279. volatile DWORD m_dwCount;
  280. char pack3[PACK_SIZE_OF(DWORD)];
  281. CSimpleRWLock m_cs;
  282. IndexSet m_indexes;
  283. };
  284. template <class T, class index_type, bool adjust_index> T* const CRingCache<T, index_type, adjust_index>::E_EMPTY = (T*)0x00;
  285. template <class T, class index_type, bool adjust_index> T* const CRingCache<T, index_type, adjust_index>::E_LOCKED = (T*)0x01;
  286. template <class T, class index_type, bool adjust_index> T* const CRingCache<T, index_type, adjust_index>::E_MAX_STATUS = (T*)0x0F;
  287. // ------------------------------------------------------------------------------------------------------------- //
  288. template <class T, class index_type = DWORD, bool adjust_index = false> class CRingCache2
  289. {
  290. public:
  291. enum EnGetResult {GR_FAIL = -1, GR_INVALID = 0, GR_VALID = 1};
  292. typedef T* TPTR;
  293. typedef volatile T* VTPTR;
  294. typedef unordered_set<index_type> IndexSet;
  295. typedef typename IndexSet::const_iterator IndexSetCI;
  296. typedef typename IndexSet::iterator IndexSetI;
  297. static TPTR const E_EMPTY;
  298. static TPTR const E_LOCKED;
  299. static TPTR const E_MAX_STATUS;
  300. static DWORD const MAX_SIZE;
  301. public:
  302. static index_type& INDEX_INC(index_type& dwIndex) {if(adjust_index) ++dwIndex; return dwIndex;}
  303. static index_type& INDEX_DEC(index_type& dwIndex) {if(adjust_index) --dwIndex; return dwIndex;}
  304. index_type& INDEX_R2V(index_type& dwIndex) {dwIndex += *(m_px + dwIndex) * m_dwSize; return dwIndex;}
  305. BOOL INDEX_V2R(index_type& dwIndex)
  306. {
  307. index_type m = dwIndex % m_dwSize;
  308. BYTE x = *(m_px + m);
  309. if(dwIndex / m_dwSize != x)
  310. return FALSE;
  311. dwIndex = m;
  312. return TRUE;
  313. }
  314. private:
  315. VTPTR& INDEX_VAL(index_type dwIndex) {return *(m_pv + dwIndex);}
  316. public:
  317. BOOL Put(TPTR pElement, index_type& dwIndex)
  318. {
  319. ASSERT(pElement != nullptr);
  320. if(!IsValid()) return FALSE;
  321. BOOL isOK = FALSE;
  322. while(true)
  323. {
  324. if(!HasSpace())
  325. break;
  326. DWORD dwCurSeq = m_dwCurSeq;
  327. index_type dwCurIndex = dwCurSeq % m_dwSize;
  328. VTPTR& pValue = INDEX_VAL(dwCurIndex);
  329. if(pValue == E_EMPTY)
  330. {
  331. if(::InterlockedCompareExchangePointer(&pValue, pElement, E_EMPTY) == E_EMPTY)
  332. {
  333. ::InterlockedIncrement(&m_dwCount);
  334. ::InterlockedCompareExchange(&m_dwCurSeq, dwCurSeq + 1, dwCurSeq);
  335. dwIndex = INDEX_INC(INDEX_R2V(dwCurIndex));
  336. isOK = TRUE;
  337. if(pElement != E_LOCKED)
  338. EmplaceIndex(dwIndex);
  339. break;
  340. }
  341. }
  342. ::InterlockedCompareExchange(&m_dwCurSeq, dwCurSeq + 1, dwCurSeq);
  343. }
  344. return isOK;
  345. }
  346. EnGetResult Get(index_type dwIndex, TPTR* ppElement, index_type* pdwRealIndex = nullptr)
  347. {
  348. ASSERT(ppElement != nullptr);
  349. if(!IsValid() || !INDEX_V2R(INDEX_DEC(dwIndex)))
  350. {
  351. *ppElement = nullptr;
  352. return GR_FAIL;
  353. }
  354. *ppElement = (TPTR)INDEX_VAL(dwIndex);
  355. if(pdwRealIndex) *pdwRealIndex = dwIndex;
  356. return IsValidElement(*ppElement) ? GR_VALID : GR_INVALID;
  357. }
  358. BOOL Set(index_type dwIndex, TPTR pElement, TPTR* ppOldElement = nullptr, index_type* pdwRealIndex = nullptr)
  359. {
  360. TPTR pElement2 = nullptr;
  361. if(pdwRealIndex == nullptr)
  362. pdwRealIndex = CreateLocalObject(index_type);
  363. if(Get(dwIndex, &pElement2, pdwRealIndex) == GR_FAIL)
  364. return FALSE;
  365. if(ppOldElement != nullptr)
  366. *ppOldElement = pElement2;
  367. if(pElement == pElement2)
  368. return FALSE;
  369. int f1 = 0;
  370. int f2 = 0;
  371. if(pElement == E_EMPTY)
  372. {
  373. if(pElement2 == E_LOCKED)
  374. f1 = -1;
  375. else
  376. f1 = f2 = -1;
  377. }
  378. else if(pElement == E_LOCKED)
  379. {
  380. if(pElement2 == E_EMPTY)
  381. f1 = 1;
  382. else
  383. f2 = -1;
  384. }
  385. else
  386. {
  387. if(pElement2 == E_EMPTY)
  388. f1 = f2 = 1;
  389. else if(pElement2 == E_LOCKED)
  390. f2 = 1;
  391. }
  392. BOOL bSetValueFirst = (f1 + f2 >= 0);
  393. index_type dwRealIndex = *pdwRealIndex;
  394. if(bSetValueFirst) INDEX_VAL(dwRealIndex) = pElement;
  395. if(f1 > 0) ::InterlockedIncrement(&m_dwCount);
  396. if(f2 != 0) (f2 > 0) ? EmplaceIndex(dwIndex) : EraseIndex(dwIndex);
  397. if(f1 < 0) {::InterlockedDecrement(&m_dwCount); ++(*(m_px + dwRealIndex));}
  398. if(!bSetValueFirst) INDEX_VAL(dwRealIndex) = pElement;
  399. ASSERT(Spaces() <= Size());
  400. return TRUE;
  401. }
  402. BOOL Remove(index_type dwIndex, TPTR* ppElement = nullptr)
  403. {
  404. return Set(dwIndex, E_EMPTY, ppElement);
  405. }
  406. BOOL AcquireLock(index_type& dwIndex)
  407. {
  408. return Put(E_LOCKED, dwIndex);
  409. }
  410. BOOL ReleaseLock(index_type dwIndex, TPTR pElement)
  411. {
  412. ASSERT(pElement == nullptr || IsValidElement(pElement));
  413. TPTR pElement2 = nullptr;
  414. Get(dwIndex, &pElement2);
  415. ASSERT(pElement2 == E_LOCKED);
  416. if(pElement2 != E_LOCKED)
  417. return FALSE;
  418. return Set(dwIndex, pElement);
  419. }
  420. public:
  421. void Reset(DWORD dwSize = 0)
  422. {
  423. if(IsValid())
  424. Destroy();
  425. if(dwSize > 0)
  426. Create(dwSize);
  427. }
  428. BOOL GetAllElementIndexes(index_type ids[], DWORD& dwCount, BOOL bCopy = TRUE)
  429. {
  430. DWORD dwSize = Elements();
  431. if(ids == nullptr || dwCount == 0)
  432. {
  433. dwCount = dwSize;
  434. return FALSE;
  435. }
  436. if(dwSize == 0)
  437. {
  438. dwCount = 0;
  439. return TRUE;
  440. }
  441. IndexSet* pIndexes = &m_indexes;
  442. if(bCopy)
  443. {
  444. pIndexes = new IndexSet;
  445. CopyIndexes(*pIndexes);
  446. }
  447. DWORD i = 0;
  448. for(auto it = pIndexes->begin(), end = pIndexes->end(); i < dwCount && it != end; ++i, ++it)
  449. ids[i] = *it;
  450. if(bCopy) delete pIndexes;
  451. dwCount = i;
  452. return TRUE;
  453. }
  454. unique_ptr<index_type[]> GetAllElementIndexes(DWORD& dwCount, BOOL bCopy = TRUE)
  455. {
  456. dwCount = (DWORD)m_indexes.size();
  457. unique_ptr<index_type[]> ids(new index_type[dwCount]);
  458. if(dwCount > 0)
  459. GetAllElementIndexes(ids.get(), dwCount, bCopy);
  460. return ids;
  461. }
  462. IndexSet& CopyIndexes(IndexSet& indexes)
  463. {
  464. {
  465. CReadLock locallock(m_cs);
  466. indexes = m_indexes;
  467. }
  468. return indexes;
  469. }
  470. static BOOL IsValidElement(TPTR pElement) {return pElement > E_MAX_STATUS;}
  471. IndexSet& Indexes () {return m_indexes;}
  472. DWORD Size () {return m_dwSize;}
  473. DWORD Elements () {return (DWORD)m_indexes.size();}
  474. DWORD Spaces () {return m_dwSize - m_dwCount;}
  475. BOOL HasSpace () {return m_dwCount < m_dwSize;}
  476. BOOL IsEmpty () {return m_dwCount == 0;}
  477. BOOL IsValid () {return m_pv != nullptr;}
  478. private:
  479. void Create(DWORD dwSize)
  480. {
  481. ASSERT(!IsValid() && dwSize > 0 && dwSize <= MAX_SIZE);
  482. m_dwCurSeq = 0;
  483. m_dwCount = 0;
  484. m_dwSize = dwSize;
  485. m_pv = (VTPTR*)malloc(m_dwSize * sizeof(TPTR));
  486. m_px = (BYTE*)malloc(m_dwSize * sizeof(BYTE));
  487. ::ZeroMemory(m_pv, m_dwSize * sizeof(TPTR));
  488. ::ZeroMemory(m_px, m_dwSize * sizeof(BYTE));
  489. }
  490. void Destroy()
  491. {
  492. ASSERT(IsValid());
  493. m_indexes.clear();
  494. free((void*)m_pv);
  495. free((void*)m_px);
  496. m_pv = nullptr;
  497. m_px = nullptr;
  498. m_dwSize = 0;
  499. m_dwCount = 0;
  500. m_dwCurSeq = 0;
  501. }
  502. void EmplaceIndex(index_type dwIndex)
  503. {
  504. CWriteLock locallock(m_cs);
  505. m_indexes.emplace(dwIndex);
  506. }
  507. void EraseIndex(index_type dwIndex)
  508. {
  509. CWriteLock locallock(m_cs);
  510. m_indexes.erase(dwIndex);
  511. }
  512. public:
  513. CRingCache2 (DWORD dwSize = 0)
  514. : m_pv (nullptr)
  515. , m_px (nullptr)
  516. , m_dwSize (0)
  517. , m_dwCount (0)
  518. , m_dwCurSeq(0)
  519. {
  520. Reset(dwSize);
  521. }
  522. ~CRingCache2()
  523. {
  524. Reset(0);
  525. }
  526. DECLARE_NO_COPY_CLASS(CRingCache2)
  527. private:
  528. DWORD m_dwSize;
  529. VTPTR* m_pv;
  530. char pack1[PACK_SIZE_OF(VTPTR*)];
  531. BYTE* m_px;
  532. char pack2[PACK_SIZE_OF(BYTE*)];
  533. volatile DWORD m_dwCurSeq;
  534. char pack3[PACK_SIZE_OF(DWORD)];
  535. volatile DWORD m_dwCount;
  536. char pack4[PACK_SIZE_OF(DWORD)];
  537. CSimpleRWLock m_cs;
  538. IndexSet m_indexes;
  539. };
  540. template <class T, class index_type, bool adjust_index> T* const CRingCache2<T, index_type, adjust_index>::E_EMPTY = (T*)0x00;
  541. template <class T, class index_type, bool adjust_index> T* const CRingCache2<T, index_type, adjust_index>::E_LOCKED = (T*)0x01;
  542. template <class T, class index_type, bool adjust_index> T* const CRingCache2<T, index_type, adjust_index>::E_MAX_STATUS = (T*)0x0F;
  543. template <class T, class index_type, bool adjust_index> DWORD const CRingCache2<T, index_type, adjust_index>::MAX_SIZE =
  544. #if __WORDSIZE == 32
  545. 0x00FFFFFF
  546. #else
  547. 0xFFFFFFFF
  548. #endif
  549. ;
  550. // ------------------------------------------------------------------------------------------------------------- //
  551. template <class T> class CRingPool
  552. {
  553. private:
  554. typedef T* TPTR;
  555. typedef volatile T* VTPTR;
  556. static TPTR const E_EMPTY;
  557. static TPTR const E_LOCKED;
  558. static TPTR const E_MAX_STATUS;
  559. private:
  560. VTPTR& INDEX_VAL(DWORD dwIndex) {return *(m_pv + dwIndex);}
  561. public:
  562. BOOL TryPut(TPTR pElement)
  563. {
  564. ASSERT(pElement != nullptr);
  565. if(!IsValid()) return FALSE;
  566. BOOL isOK = FALSE;
  567. for(DWORD i = 0; i < m_dwSize; i++)
  568. {
  569. DWORD seqPut = m_seqPut;
  570. if(!HasPutSpace(seqPut))
  571. break;
  572. DWORD dwIndex = seqPut % m_dwSize;
  573. VTPTR& pValue = INDEX_VAL(dwIndex);
  574. TPTR pCurrent = (TPTR)pValue;
  575. if(pCurrent == E_EMPTY)
  576. {
  577. if(::InterlockedCompareExchangePointer(&pValue, pElement, pCurrent) == pCurrent)
  578. {
  579. ::InterlockedCompareExchange(&m_seqPut, seqPut + 1, seqPut);
  580. isOK = TRUE;
  581. break;
  582. }
  583. }
  584. ::InterlockedCompareExchange(&m_seqPut, seqPut + 1, seqPut);
  585. }
  586. return isOK;
  587. }
  588. BOOL TryGet(TPTR* ppElement)
  589. {
  590. ASSERT(ppElement != nullptr);
  591. if(!IsValid()) return FALSE;
  592. BOOL isOK = FALSE;
  593. while(true)
  594. {
  595. DWORD seqGet = m_seqGet;
  596. if(!HasGetSpace(seqGet))
  597. break;
  598. DWORD dwIndex = seqGet % m_dwSize;
  599. VTPTR& pValue = INDEX_VAL(dwIndex);
  600. TPTR pCurrent = (TPTR)pValue;
  601. if(pCurrent > E_MAX_STATUS)
  602. {
  603. if(::InterlockedCompareExchangePointer(&pValue, E_EMPTY, pCurrent) == pCurrent)
  604. {
  605. ::InterlockedCompareExchange(&m_seqGet, seqGet + 1, seqGet);
  606. *(ppElement) = pCurrent;
  607. isOK = TRUE;
  608. break;
  609. }
  610. }
  611. ::InterlockedCompareExchange(&m_seqGet, seqGet + 1, seqGet);
  612. }
  613. return isOK;
  614. }
  615. BOOL TryLock(TPTR* ppElement, DWORD& dwIndex)
  616. {
  617. ASSERT(ppElement != nullptr);
  618. if(!IsValid()) return FALSE;
  619. BOOL isOK = FALSE;
  620. while(true)
  621. {
  622. DWORD seqGet = m_seqGet;
  623. if(!HasGetSpace(seqGet))
  624. break;
  625. dwIndex = seqGet % m_dwSize;
  626. VTPTR& pValue = INDEX_VAL(dwIndex);
  627. TPTR pCurrent = (TPTR)pValue;
  628. if(pCurrent > E_MAX_STATUS)
  629. {
  630. if(::InterlockedCompareExchangePointer(&pValue, E_LOCKED, pCurrent) == pCurrent)
  631. {
  632. ::InterlockedCompareExchange(&m_seqGet, seqGet + 1, seqGet);
  633. *(ppElement) = pCurrent;
  634. isOK = TRUE;
  635. break;
  636. }
  637. }
  638. ::InterlockedCompareExchange(&m_seqGet, seqGet + 1, seqGet);
  639. }
  640. return isOK;
  641. }
  642. BOOL ReleaseLock(TPTR pElement, DWORD dwIndex)
  643. {
  644. ASSERT(dwIndex < m_dwSize);
  645. ASSERT(pElement == nullptr || pElement > E_MAX_STATUS);
  646. if(!IsValid()) return FALSE;
  647. VTPTR& pValue = INDEX_VAL(dwIndex);
  648. ENSURE(pValue == E_LOCKED);
  649. if(pElement == nullptr)
  650. pValue = E_EMPTY;
  651. else
  652. pValue = pElement;
  653. return TRUE;
  654. }
  655. public:
  656. void Reset(DWORD dwSize = 0)
  657. {
  658. if(IsValid())
  659. Destroy();
  660. if(dwSize > 0)
  661. Create(dwSize);
  662. }
  663. void Clear()
  664. {
  665. for(DWORD dwIndex = 0; dwIndex < m_dwSize; dwIndex++)
  666. {
  667. VTPTR& pValue = INDEX_VAL(dwIndex);
  668. if(pValue > E_MAX_STATUS)
  669. {
  670. T::Destruct((TPTR)pValue);
  671. pValue = E_EMPTY;
  672. }
  673. }
  674. Reset();
  675. }
  676. DWORD Size() {return m_dwSize;}
  677. DWORD Elements() {return m_seqPut - m_seqGet;}
  678. BOOL IsFull() {return Elements() == Size();}
  679. BOOL IsEmpty() {return Elements() == 0;}
  680. BOOL IsValid() {return m_pv != nullptr;}
  681. private:
  682. BOOL HasPutSpace(DWORD seqPut)
  683. {
  684. return ((int)(seqPut - m_seqGet) < (int)m_dwSize);
  685. }
  686. BOOL HasGetSpace(DWORD seqGet)
  687. {
  688. return ((int)(m_seqPut - seqGet) > 0);
  689. }
  690. void Create(DWORD dwSize)
  691. {
  692. ASSERT(!IsValid() && dwSize > 0);
  693. m_seqPut = 0;
  694. m_seqGet = 0;
  695. m_dwSize = dwSize;
  696. m_pv = (VTPTR*)malloc(m_dwSize * sizeof(TPTR));
  697. ::ZeroMemory(m_pv, m_dwSize * sizeof(TPTR));
  698. }
  699. void Destroy()
  700. {
  701. ASSERT(IsValid());
  702. free((void*)m_pv);
  703. m_pv = nullptr;
  704. m_dwSize = 0;
  705. m_seqPut = 0;
  706. m_seqGet = 0;
  707. }
  708. public:
  709. CRingPool(DWORD dwSize = 0)
  710. : m_pv(nullptr)
  711. , m_dwSize(0)
  712. , m_seqPut(0)
  713. , m_seqGet(0)
  714. {
  715. Reset(dwSize);
  716. }
  717. ~CRingPool()
  718. {
  719. Reset(0);
  720. }
  721. private:
  722. CRingPool(const CRingPool&);
  723. CRingPool operator = (const CRingPool&);
  724. private:
  725. DWORD m_dwSize;
  726. VTPTR* m_pv;
  727. char pack1[PACK_SIZE_OF(VTPTR*)];
  728. volatile DWORD m_seqPut;
  729. char pack2[PACK_SIZE_OF(DWORD)];
  730. volatile DWORD m_seqGet;
  731. char pack3[PACK_SIZE_OF(DWORD)];
  732. };
  733. template <class T> T* const CRingPool<T>::E_EMPTY = (T*)0x00;
  734. template <class T> T* const CRingPool<T>::E_LOCKED = (T*)0x01;
  735. template <class T> T* const CRingPool<T>::E_MAX_STATUS = (T*)0x0F;
  736. // ------------------------------------------------------------------------------------------------------------- //
  737. template <class T> class CCASQueueX
  738. {
  739. private:
  740. struct Node;
  741. typedef Node* NPTR;
  742. typedef volatile Node* VNPTR;
  743. typedef volatile UINT VUINT;
  744. struct Node
  745. {
  746. T* pValue;
  747. VNPTR pNext;
  748. Node(T* val, NPTR next = nullptr)
  749. : pValue(val), pNext(next)
  750. {
  751. }
  752. };
  753. public:
  754. void PushBack(T* pVal)
  755. {
  756. ASSERT(pVal != nullptr);
  757. VNPTR pTail = nullptr;
  758. NPTR pNode = new Node(pVal);
  759. while(true)
  760. {
  761. pTail = m_pTail;
  762. if(::InterlockedCompareExchangePointer(&m_pTail, pNode, pTail) == pTail)
  763. {
  764. pTail->pNext = pNode;
  765. break;
  766. }
  767. }
  768. ::InterlockedIncrement(&m_iSize);
  769. }
  770. void UnsafePushBack(T* pVal)
  771. {
  772. ASSERT(pVal != nullptr);
  773. NPTR pNode = new Node(pVal);
  774. m_pTail->pNext = pNode;
  775. m_pTail = pNode;
  776. ::InterlockedIncrement(&m_iSize);
  777. }
  778. BOOL PopFront(T** ppVal)
  779. {
  780. ASSERT(ppVal != nullptr);
  781. if(IsEmpty())
  782. return FALSE;
  783. BOOL isOK = FALSE;
  784. NPTR pHead = nullptr;
  785. NPTR pNext = nullptr;
  786. while(true)
  787. {
  788. Lock();
  789. pHead = (NPTR)m_pHead;
  790. pNext = (NPTR)pHead->pNext;
  791. if(pNext == nullptr)
  792. {
  793. Unlock();
  794. break;
  795. }
  796. *ppVal = pNext->pValue;
  797. m_pHead = pNext;
  798. Unlock();
  799. isOK = TRUE;
  800. ::InterlockedDecrement(&m_iSize);
  801. delete pHead;
  802. break;
  803. }
  804. return isOK;
  805. }
  806. BOOL UnsafePopFront(T** ppVal)
  807. {
  808. if(!UnsafePeekFront(ppVal))
  809. return FALSE;
  810. UnsafePopFrontNotCheck();
  811. return TRUE;
  812. }
  813. BOOL UnsafePeekFront(T** ppVal)
  814. {
  815. ASSERT(ppVal != nullptr);
  816. NPTR pNext = (NPTR)m_pHead->pNext;
  817. if(pNext == nullptr)
  818. return FALSE;
  819. *ppVal = pNext->pValue;
  820. return TRUE;
  821. }
  822. void UnsafePopFrontNotCheck()
  823. {
  824. NPTR pHead = (NPTR)m_pHead;
  825. NPTR pNext = (NPTR)pHead->pNext;
  826. m_pHead = pNext;
  827. ::InterlockedDecrement(&m_iSize);
  828. delete pHead;
  829. }
  830. void UnsafeClear()
  831. {
  832. ASSERT(m_pHead != nullptr);
  833. m_dwCheckTime = 0;
  834. while(m_pHead->pNext != nullptr)
  835. UnsafePopFrontNotCheck();
  836. }
  837. public:
  838. UINT Size() {return m_iSize;}
  839. BOOL IsEmpty() {return m_iSize == 0;}
  840. void Lock() {while(!TryLock()) ::YieldProcessor();}
  841. void Unlock() {m_iLock = 0;}
  842. BOOL TryLock() {return (::InterlockedCompareExchange(&m_iLock, 1u, 0u) == 0);}
  843. DWORD GetCheckTime()
  844. {
  845. return m_dwCheckTime;
  846. }
  847. void UpdateCheckTime(DWORD dwCurrent = 0)
  848. {
  849. if(dwCurrent == 0)
  850. dwCurrent = ::TimeGetTime();
  851. m_dwCheckTime = dwCurrent;
  852. }
  853. int GetCheckTimeGap(DWORD dwCurrent = 0)
  854. {
  855. int rs = (int)GetTimeGap32(m_dwCheckTime, dwCurrent);
  856. if(rs < -60 * 1000)
  857. rs = MAXINT;
  858. return rs;
  859. }
  860. public:
  861. CCASQueueX() : m_iLock(0), m_iSize(0), m_dwCheckTime(0)
  862. {
  863. m_pHead = m_pTail = new Node(nullptr);
  864. }
  865. ~CCASQueueX()
  866. {
  867. ASSERT(m_iLock == 0);
  868. ASSERT(m_iSize == 0);
  869. ASSERT(m_pTail == m_pHead);
  870. ASSERT(m_pHead != nullptr);
  871. ASSERT(m_pHead->pNext == nullptr);
  872. UnsafeClear();
  873. delete m_pHead;
  874. }
  875. DECLARE_NO_COPY_CLASS(CCASQueueX)
  876. private:
  877. VUINT m_iLock;
  878. VUINT m_iSize;
  879. VNPTR m_pHead;
  880. VNPTR m_pTail;
  881. volatile DWORD m_dwCheckTime;
  882. };
  883. template <class T> class CCASSimpleQueueX
  884. {
  885. private:
  886. struct Node;
  887. typedef Node* NPTR;
  888. typedef volatile Node* VNPTR;
  889. typedef volatile UINT VUINT;
  890. struct Node
  891. {
  892. T tValue;
  893. VNPTR pNext;
  894. Node(T val, NPTR next = nullptr)
  895. : tValue(val), pNext(next)
  896. {
  897. }
  898. };
  899. public:
  900. void PushBack(T tVal)
  901. {
  902. VNPTR pTail = nullptr;
  903. NPTR pNode = new Node(tVal);
  904. while(true)
  905. {
  906. pTail = m_pTail;
  907. if(::InterlockedCompareExchangePointer(&m_pTail, pNode, pTail) == pTail)
  908. {
  909. pTail->pNext = pNode;
  910. break;
  911. }
  912. }
  913. ::InterlockedIncrement(&m_iSize);
  914. }
  915. void UnsafePushBack(T tVal)
  916. {
  917. NPTR pNode = new Node(tVal);
  918. m_pTail->pNext = pNode;
  919. m_pTail = pNode;
  920. ::InterlockedIncrement(&m_iSize);
  921. }
  922. BOOL PopFront(T* ptVal)
  923. {
  924. ASSERT(ptVal != nullptr);
  925. if(IsEmpty())
  926. return FALSE;
  927. BOOL isOK = FALSE;
  928. NPTR pHead = nullptr;
  929. NPTR pNext = nullptr;
  930. while(true)
  931. {
  932. Lock();
  933. pHead = (NPTR)m_pHead;
  934. pNext = (NPTR)pHead->pNext;
  935. if(pNext == nullptr)
  936. {
  937. Unlock();
  938. break;
  939. }
  940. *ptVal = pNext->tValue;
  941. m_pHead = pNext;
  942. Unlock();
  943. isOK = TRUE;
  944. ::InterlockedDecrement(&m_iSize);
  945. delete pHead;
  946. break;
  947. }
  948. return isOK;
  949. }
  950. BOOL UnsafePopFront(T* ptVal)
  951. {
  952. if(!UnsafePeekFront(ptVal))
  953. return FALSE;
  954. UnsafePopFrontNotCheck();
  955. return TRUE;
  956. }
  957. BOOL UnsafePeekFront(T* ptVal)
  958. {
  959. ASSERT(ptVal != nullptr);
  960. NPTR pNext = (NPTR)m_pHead->pNext;
  961. if(pNext == nullptr)
  962. return FALSE;
  963. *ptVal = pNext->pValue;
  964. return TRUE;
  965. }
  966. void UnsafePopFrontNotCheck()
  967. {
  968. NPTR pHead = (NPTR)m_pHead;
  969. NPTR pNext = (NPTR)pHead->pNext;
  970. m_pHead = pNext;
  971. ::InterlockedDecrement(&m_iSize);
  972. delete pHead;
  973. }
  974. void UnsafeClear()
  975. {
  976. ASSERT(m_pHead != nullptr);
  977. m_dwCheckTime = 0;
  978. while(m_pHead->pNext != nullptr)
  979. UnsafePopFrontNotCheck();
  980. }
  981. public:
  982. UINT Size() {return m_iSize;}
  983. BOOL IsEmpty() {return m_iSize == 0;}
  984. void Lock() {while(!TryLock()) ::YieldProcessor();}
  985. void Unlock() {m_iLock = 0;}
  986. BOOL TryLock() {return (::InterlockedCompareExchange(&m_iLock, 1u, 0u) == 0);}
  987. DWORD GetCheckTime()
  988. {
  989. return m_dwCheckTime;
  990. }
  991. void UpdateCheckTime(DWORD dwCurrent = 0)
  992. {
  993. if(dwCurrent == 0)
  994. dwCurrent = ::TimeGetTime();
  995. m_dwCheckTime = dwCurrent;
  996. }
  997. int GetCheckTimeGap(DWORD dwCurrent = 0)
  998. {
  999. int rs = (int)GetTimeGap32(m_dwCheckTime, dwCurrent);
  1000. if(rs < -60 * 1000)
  1001. rs = MAXINT;
  1002. return rs;
  1003. }
  1004. public:
  1005. CCASSimpleQueueX() : m_iLock(0), m_iSize(0), m_dwCheckTime(0)
  1006. {
  1007. m_pHead = m_pTail = new Node(0);
  1008. }
  1009. ~CCASSimpleQueueX()
  1010. {
  1011. ASSERT(m_iLock == 0);
  1012. ASSERT(m_iSize == 0);
  1013. ASSERT(m_pTail == m_pHead);
  1014. ASSERT(m_pHead != nullptr);
  1015. ASSERT(m_pHead->pNext == nullptr);
  1016. UnsafeClear();
  1017. delete m_pHead;
  1018. }
  1019. DECLARE_NO_COPY_CLASS(CCASSimpleQueueX)
  1020. private:
  1021. VUINT m_iLock;
  1022. VUINT m_iSize;
  1023. VNPTR m_pHead;
  1024. VNPTR m_pTail;
  1025. volatile DWORD m_dwCheckTime;
  1026. };
  1027. template <class T> class CCASQueueY
  1028. {
  1029. public:
  1030. void PushBack(T* pVal)
  1031. {
  1032. CCriSecLock locallock(m_csGuard);
  1033. UnsafePushBack(pVal);
  1034. }
  1035. void UnsafePushBack(T* pVal)
  1036. {
  1037. ASSERT(pVal != nullptr);
  1038. m_lsItems.push_back(pVal);
  1039. }
  1040. void PushFront(T* pVal)
  1041. {
  1042. CCriSecLock locallock(m_csGuard);
  1043. UnsafePushFront(pVal);
  1044. }
  1045. void UnsafePushFront(T* pVal)
  1046. {
  1047. ASSERT(pVal != nullptr);
  1048. m_lsItems.push_front(pVal);
  1049. }
  1050. BOOL PopFront(T** ppVal)
  1051. {
  1052. CCriSecLock locallock(m_csGuard);
  1053. return UnsafePopFront(ppVal);
  1054. }
  1055. BOOL UnsafePopFront(T** ppVal)
  1056. {
  1057. if(!UnsafePeekFront(ppVal))
  1058. return FALSE;
  1059. UnsafePopFrontNotCheck();
  1060. return TRUE;
  1061. }
  1062. BOOL PeekFront(T** ppVal)
  1063. {
  1064. CCriSecLock locallock(m_csGuard);
  1065. return UnsafePeekFront(ppVal);
  1066. }
  1067. BOOL UnsafePeekFront(T** ppVal)
  1068. {
  1069. ASSERT(ppVal != nullptr);
  1070. if(m_lsItems.empty())
  1071. return FALSE;
  1072. *ppVal = m_lsItems.front();
  1073. return TRUE;
  1074. }
  1075. void UnsafePopFrontNotCheck()
  1076. {
  1077. m_lsItems.pop_front();
  1078. }
  1079. void Clear()
  1080. {
  1081. CCriSecLock locallock(m_csGuard);
  1082. UnsafeClear();
  1083. }
  1084. void UnsafeClear()
  1085. {
  1086. m_dwCheckTime = 0;
  1087. m_lsItems.clear();
  1088. }
  1089. public:
  1090. ULONG Size() {return (ULONG)m_lsItems.size();}
  1091. BOOL IsEmpty() {return (BOOL)m_lsItems.empty();}
  1092. void Lock() {m_csGuard.lock();}
  1093. void Unlock() {m_csGuard.unlock();}
  1094. BOOL TryLock() {return m_csGuard.try_lock();}
  1095. CCriSec& Guard(){return m_csGuard;}
  1096. DWORD GetCheckTime()
  1097. {
  1098. return m_dwCheckTime;
  1099. }
  1100. void UpdateCheckTime(DWORD dwCurrent = 0)
  1101. {
  1102. if(dwCurrent == 0)
  1103. dwCurrent = ::TimeGetTime();
  1104. m_dwCheckTime = dwCurrent;
  1105. }
  1106. int GetCheckTimeGap(DWORD dwCurrent = 0)
  1107. {
  1108. int rs = (int)GetTimeGap32(m_dwCheckTime, dwCurrent);
  1109. if(rs < -60 * 1000)
  1110. rs = MAXINT;
  1111. return rs;
  1112. }
  1113. public:
  1114. CCASQueueY()
  1115. : m_dwCheckTime(0)
  1116. {
  1117. }
  1118. ~CCASQueueY()
  1119. {
  1120. ASSERT(IsEmpty());
  1121. UnsafeClear();
  1122. }
  1123. DECLARE_NO_COPY_CLASS(CCASQueueY)
  1124. private:
  1125. CCriSec m_csGuard;
  1126. deque<T*> m_lsItems;
  1127. volatile DWORD m_dwCheckTime;
  1128. };
  1129. template <class T> class CCASSimpleQueueY
  1130. {
  1131. public:
  1132. void PushBack(T tVal)
  1133. {
  1134. CCriSecLock locallock(m_csGuard);
  1135. UnsafePushBack(tVal);
  1136. }
  1137. void UnsafePushBack(T tVal)
  1138. {
  1139. m_lsItems.push_back(tVal);
  1140. }
  1141. void PushFront(T tVal)
  1142. {
  1143. CCriSecLock locallock(m_csGuard);
  1144. UnsafePushFront(tVal);
  1145. }
  1146. void UnsafePushFront(T tVal)
  1147. {
  1148. m_lsItems.push_front(tVal);
  1149. }
  1150. BOOL PopFront(T* ptVal)
  1151. {
  1152. CCriSecLock locallock(m_csGuard);
  1153. return UnsafePopFront(ptVal);
  1154. }
  1155. BOOL UnsafePopFront(T* ptVal)
  1156. {
  1157. if(!UnsafePeekFront(ptVal))
  1158. return FALSE;
  1159. UnsafePopFrontNotCheck();
  1160. return TRUE;
  1161. }
  1162. BOOL PeekFront(T* ptVal)
  1163. {
  1164. CCriSecLock locallock(m_csGuard);
  1165. return UnsafePeekFront(ptVal);
  1166. }
  1167. BOOL UnsafePeekFront(T* ptVal)
  1168. {
  1169. ASSERT(ptVal != nullptr);
  1170. if(m_lsItems.empty())
  1171. return FALSE;
  1172. *ptVal = m_lsItems.front();
  1173. return TRUE;
  1174. }
  1175. void UnsafePopFrontNotCheck()
  1176. {
  1177. m_lsItems.pop_front();
  1178. }
  1179. void Clear()
  1180. {
  1181. CCriSecLock locallock(m_csGuard);
  1182. UnsafeClear();
  1183. }
  1184. void UnsafeClear()
  1185. {
  1186. m_dwCheckTime = 0;
  1187. m_lsItems.clear();
  1188. }
  1189. public:
  1190. ULONG Size() {return (ULONG)m_lsItems.size();}
  1191. BOOL IsEmpty() {return (BOOL)m_lsItems.empty();}
  1192. void Lock() {m_csGuard.lock();}
  1193. void Unlock() {m_csGuard.unlock();}
  1194. BOOL TryLock() {return m_csGuard.try_lock();}
  1195. CCriSec& Guard(){return m_csGuard;}
  1196. DWORD GetCheckTime()
  1197. {
  1198. return m_dwCheckTime;
  1199. }
  1200. void UpdateCheckTime(DWORD dwCurrent = 0)
  1201. {
  1202. if(dwCurrent == 0)
  1203. dwCurrent = ::TimeGetTime();
  1204. m_dwCheckTime = dwCurrent;
  1205. }
  1206. int GetCheckTimeGap(DWORD dwCurrent = 0)
  1207. {
  1208. int rs = (int)GetTimeGap32(m_dwCheckTime, dwCurrent);
  1209. if(rs < -60 * 1000)
  1210. rs = MAXINT;
  1211. return rs;
  1212. }
  1213. public:
  1214. CCASSimpleQueueY()
  1215. : m_dwCheckTime(0)
  1216. {
  1217. }
  1218. ~CCASSimpleQueueY()
  1219. {
  1220. ASSERT(IsEmpty());
  1221. UnsafeClear();
  1222. }
  1223. DECLARE_NO_COPY_CLASS(CCASSimpleQueueY)
  1224. private:
  1225. CCriSec m_csGuard;
  1226. deque<T> m_lsItems;
  1227. volatile DWORD m_dwCheckTime;
  1228. };
  1229. template <class T> using CCASQueue = CCASQueueX<T>;
  1230. template <class T> using CCASSimpleQueue = CCASSimpleQueueX<T>;
  1231. template<typename T>
  1232. void ReleaseGCObj(CCASQueue<T>& lsGC, DWORD dwLockTime, BOOL bForce = FALSE)
  1233. {
  1234. static const int MIN_CHECK_INTERVAL = 1 * 1000;
  1235. static const int MAX_CHECK_INTERVAL = 15 * 1000;
  1236. T* pObj = nullptr;
  1237. if(bForce)
  1238. {
  1239. CLocalLock<CCASQueue<T>> locallock(lsGC);
  1240. while(lsGC.UnsafePeekFront(&pObj))
  1241. {
  1242. lsGC.UnsafePopFrontNotCheck();
  1243. T::Destruct(pObj);
  1244. }
  1245. }
  1246. else
  1247. {
  1248. if(lsGC.IsEmpty() || lsGC.GetCheckTimeGap() < MAX(MIN((int)(dwLockTime / 3), MAX_CHECK_INTERVAL), MIN_CHECK_INTERVAL))
  1249. return;
  1250. T* pFirst = nullptr;
  1251. BOOL bFirst = TRUE;
  1252. DWORD now = 0;
  1253. while(TRUE)
  1254. {
  1255. ASSERT((pObj = nullptr) == nullptr);
  1256. {
  1257. CLocalTryLock<CCASQueue<T>> locallock(lsGC);
  1258. if(!locallock.IsValid())
  1259. break;
  1260. if(bFirst)
  1261. {
  1262. bFirst = FALSE;
  1263. now = ::TimeGetTime();
  1264. lsGC.UpdateCheckTime(now);
  1265. }
  1266. if(!lsGC.UnsafePeekFront(&pObj))
  1267. break;
  1268. if((int)(now - pObj->GetFreeTime()) < (int)dwLockTime)
  1269. break;
  1270. lsGC.UnsafePopFrontNotCheck();
  1271. if(pObj->GetCount() > 0)
  1272. {
  1273. lsGC.PushBack(pObj);
  1274. if(pFirst == nullptr)
  1275. pFirst = pObj;
  1276. else if(pFirst == pObj)
  1277. break;
  1278. continue;
  1279. }
  1280. }
  1281. ASSERT(pObj != nullptr);
  1282. T::Destruct(pObj);
  1283. }
  1284. }
  1285. }
  1286. #if __WORDSIZE == 32
  1287. #pragma pack(pop)
  1288. #endif