Source code of Windows XP (NT5)
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.

238 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. sharedat.hxx
  6. Abstract:
  7. Fixed size single writer (only by creater of object), multiple readers.
  8. Assumes rare writes, and utilizes dual incrementing counters to ensure
  9. consistent read access to data. Reader retries when its read occurs at
  10. the same time as a write.
  11. Creator ensures there is a single writer.
  12. Usage:
  13. //
  14. // Create simple data object (no vbtl/virtual functions).
  15. //
  16. struct TObject {
  17. DWORD dwData1;
  18. DWORD dwData2;
  19. };
  20. Writer:
  21. TShareData::TReadWrite Object( TEXT( "NamedSharedData" ),
  22. sizeof( TObject ),
  23. NULL );
  24. TObject *pObject = (TObject*)Object.pData();
  25. //
  26. // WriteBegin indicates that a group update has begun. This must be
  27. // done even for single data writes unless you can guarantee that the
  28. // data write appears atomic.
  29. //
  30. Object.vWriteBegin();
  31. //
  32. // Modify the data here.
  33. //
  34. pObject->dwData1 = 1;
  35. pObject->dwData2 = 2;
  36. //
  37. // WriteEnd is called when all writes to the data are complete.
  38. //
  39. Object.vWriteEnd();
  40. Reader:
  41. TShareData<TObject>::TRead Object( TEXT( "NamedSharedData" ));
  42. TObject *pObject = (TObject*)Object.pData();
  43. TShareData<TObject>::TReadSync RS( Object );
  44. DWORD dwData;
  45. do {
  46. RS.vReadBegin();
  47. dwData1 = pObject->dwData1;
  48. dwData2 = pObject->dwData2;
  49. } while( RS.vReadEnd( ));
  50. Implementation detail:
  51. Write:
  52. Increment Count[1]
  53. Modify data
  54. Increment Count[0]
  55. Read:
  56. do {
  57. c1 = Count[0]
  58. Read data and copy into separate buffer
  59. c2 = Count[1]
  60. } while ( c1 != c2 )
  61. Overactive writer can starve readers.
  62. Author:
  63. Albert Ting (AlbertT) 5-Oct-97
  64. Revision History:
  65. --*/
  66. #ifndef SHAREDAT_HXX
  67. #define SHAREDAT_HXX
  68. #define SZ_SUFFIX_FILE (TEXT( "_shrDatF" ))
  69. class TShareData {
  70. private:
  71. class TReadSync;
  72. class TBase {
  73. friend class TReadSync;
  74. protected:
  75. typedef struct TData {
  76. DWORD cbSize;
  77. DWORD dwReserved;
  78. LONG lCount1;
  79. LONG lCount2;
  80. } *pTData;
  81. HANDLE m_hMap;
  82. pTData m_pData;
  83. public:
  84. PVOID
  85. pData(
  86. VOID
  87. )
  88. {
  89. return (PBYTE)m_pData + sizeof( TData );
  90. }
  91. BOOL
  92. bValid(
  93. VOID
  94. )
  95. {
  96. return m_pData != NULL;
  97. }
  98. protected:
  99. enum {
  100. kNameBufferMax = MAX_PATH + COUNTOF( SZ_SUFFIX_FILE )
  101. };
  102. TBase(
  103. VOID
  104. );
  105. ~TBase(
  106. VOID
  107. );
  108. VOID
  109. vCleanup(
  110. VOID
  111. );
  112. BOOL
  113. bGetFullName(
  114. LPCTSTR pszName,
  115. LPTSTR pszFullName
  116. );
  117. };
  118. public:
  119. class TReadWrite : public TBase {
  120. public:
  121. TReadWrite(
  122. LPCTSTR pszName,
  123. DWORD cbSize,
  124. PSECURITY_ATTRIBUTES pSA
  125. );
  126. VOID
  127. vWriteFirst(
  128. VOID
  129. );
  130. VOID
  131. vWriteBegin(
  132. VOID
  133. );
  134. VOID
  135. vWriteEnd(
  136. VOID
  137. );
  138. };
  139. class TRead : public TBase {
  140. public:
  141. TRead(
  142. LPCTSTR pszName,
  143. DWORD cbSize
  144. );
  145. };
  146. class TReadSync {
  147. public:
  148. TReadSync(
  149. TRead& Read
  150. );
  151. BOOL
  152. bValid(
  153. VOID
  154. )
  155. {
  156. return TRUE;
  157. }
  158. VOID
  159. vReadBegin(
  160. VOID
  161. );
  162. BOOL
  163. bReadEnd(
  164. VOID
  165. );
  166. private:
  167. TRead& m_Read;
  168. LONG m_lCount;
  169. };
  170. };
  171. #endif // ifndef SHAREDAT_HXX