Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
3.4 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * ProtectedList:
  6. *
  7. * Wraps the List class with a mutex lock. Each public method is
  8. * protected first by accessing the mutex. The entire list can
  9. * be grabbed by using Access, ungrabbed by calling Release.
  10. * You would want to Access the list almost always when using it,
  11. * otherwise other threads could change the list without your
  12. * knowledge, exactly what this class is trying to prevent
  13. *
  14. * REVISIONS:
  15. * pcy29Nov92: Use PObj rather than PNode for return values
  16. * pcy21Apr93: OS2 FE merge
  17. * cad09Jul93: using new semaphores
  18. * cad31Aug93: removing compiler warnings
  19. * rct05Nov93: added NLM include files
  20. * rct01Feb94: List no longer inherits from container
  21. * pcy08Apr94: Trim size, use static iterators, dead code removal
  22. * mwh08Apr97: add Access,Release methods & NOTES section
  23. */
  24. #define INCL_BASE
  25. #define INCL_NOPM
  26. #include "cdefine.h"
  27. extern "C"
  28. {
  29. #if (C_OS & C_OS2)
  30. #define INCL_DOSSEMAPHORES
  31. #include <os2.h>
  32. #endif
  33. }
  34. #include "protlist.h"
  35. #if(C_OS & C_OS2)
  36. #include "mutexl2x.h"
  37. #endif
  38. #if (C_OS & C_NT)
  39. #include "mutexnt.h"
  40. #endif
  41. #if (C_OS & C_NLM)
  42. #include "nlmmutex.h"
  43. #endif
  44. #ifdef SINGLETHREADED
  45. #include "nullmutl.h"
  46. #endif
  47. ProtectedList::ProtectedList () : List(), accessLock((PMutexLock)NULL)
  48. {
  49. #ifdef SINGLETHREADED
  50. accessLock = new NullMutexLock();
  51. #else
  52. accessLock = new ApcMutexLock();
  53. #endif
  54. }
  55. ProtectedList::ProtectedList (ProtectedList* aProtectedList) :
  56. List(aProtectedList), accessLock((PMutexLock)NULL)
  57. {
  58. #ifdef SINGLETHREADED
  59. accessLock = new NullMutexLock();
  60. #else
  61. accessLock = new ApcMutexLock();
  62. #endif
  63. }
  64. ProtectedList::~ProtectedList()
  65. {
  66. Flush();
  67. delete accessLock;
  68. accessLock = NULL;
  69. }
  70. VOID ProtectedList :: Add (RObj elem)
  71. {
  72. Request();
  73. List::Add(elem);
  74. Clear();
  75. }
  76. VOID ProtectedList :: Detach (RObj elem)
  77. {
  78. Request();
  79. List::Detach(elem);
  80. Clear();
  81. }
  82. VOID ProtectedList :: Flush ()
  83. {
  84. Request();
  85. List::Flush();
  86. Clear();
  87. }
  88. VOID ProtectedList :: FlushAll ()
  89. {
  90. Request();
  91. List::FlushAll();
  92. Clear();
  93. }
  94. INT ProtectedList :: GetItemsInContainer () const
  95. {
  96. Request();
  97. INT res = List::GetItemsInContainer();
  98. Clear();
  99. return res;
  100. }
  101. RListIterator ProtectedList :: InitIterator () const
  102. {
  103. Request();
  104. RListIterator res = List::InitIterator();
  105. Clear();
  106. return res;
  107. }
  108. VOID ProtectedList :: Append (PObj elem)
  109. {
  110. Request();
  111. List::Append(elem);
  112. Clear();
  113. }
  114. PObj ProtectedList :: GetHead ()
  115. {
  116. Request();
  117. PObj res = List::GetHead();
  118. Clear();
  119. return res;
  120. }
  121. PObj ProtectedList :: Find (PObj elem)
  122. {
  123. Request();
  124. PObj res = List::Find(elem);
  125. Clear();
  126. return res;
  127. }
  128. VOID ProtectedList :: Request () const
  129. {
  130. accessLock->Request();
  131. }
  132. VOID ProtectedList :: Clear () const
  133. {
  134. accessLock->Release();
  135. }
  136. /*
  137. Use Access to lock the entire list object
  138. useful to block access completely to any other thread
  139. while one thread uses the list - don't forget to
  140. call Release when you're done - NOTE: although it
  141. is possible to still access this object w/o calling
  142. Access first, all of the public calls are protected
  143. by first trying to gain Access to the object first
  144. */
  145. VOID ProtectedList::Access() const
  146. {
  147. Request();
  148. }
  149. VOID ProtectedList::Release() const
  150. {
  151. Clear();
  152. }