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.

149 lines
4.1 KiB

  1. // SharedMarker.cpp: implementation of the CSharedMarker class.
  2. //
  3. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  4. // 2000. This computer program includes Confidential, Proprietary
  5. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  6. // use, disclosure, and/or reproduction is prohibited unless authorized
  7. // in writing. All Rights Reserved.
  8. //////////////////////////////////////////////////////////////////////
  9. #include <string>
  10. #include <scuOsVersion.h>
  11. #include <slbCrc32.h>
  12. #include "iop.h"
  13. #include "iopExc.h"
  14. #include "SharedMarker.h"
  15. #include "SecurityAttributes.h"
  16. using std::string;
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. namespace iop
  21. {
  22. CSharedMarker::CSharedMarker(string const &strName)
  23. {
  24. SECURITY_ATTRIBUTES *psa = NULL;
  25. #if defined(SLBIOP_USE_SECURITY_ATTRIBUTES)
  26. CSecurityAttributes *sa = new CSecurityAttributes;
  27. CIOP::InitIOPSecurityAttrs(sa);
  28. psa = &(sa->sa);
  29. #endif
  30. // Create/open mutex that protects shared memory
  31. string MutexName = "SLBIOP_SHMARKER_MUTEX_" + strName;
  32. if (MutexName.size() >= MAX_PATH)
  33. throw Exception(ccSynchronizationObjectNameTooLong);
  34. m_hMutex = CreateMutex(psa, FALSE, MutexName.c_str());
  35. if (!m_hMutex)
  36. throw scu::OsException(GetLastError());
  37. // Map the shared memory, initialize if needed.
  38. string MappingName = "SLBIOP_SHMARKER_MAP_" + strName;
  39. if (MappingName.size() >= MAX_PATH)
  40. throw Exception(ccSynchronizationObjectNameTooLong);
  41. HANDLE hFile = INVALID_HANDLE_VALUE;
  42. Transaction foo(m_hMutex);
  43. m_hFileMap = CreateFileMapping(hFile,psa,PAGE_READWRITE,0,SharedMemorySize(),MappingName.c_str());
  44. if (!m_hFileMap)
  45. throw scu::OsException(GetLastError());
  46. bool NeedInit = false; // Flags telling if the memory need to be initialized
  47. if(GetLastError()!=ERROR_ALREADY_EXISTS) NeedInit = true;
  48. // Assign pointers to shared memory
  49. m_pShMemData = (SharedMemoryData*)MapViewOfFile(m_hFileMap,FILE_MAP_WRITE,0,0,0);
  50. if (!m_pShMemData)
  51. throw scu::OsException(GetLastError());
  52. // Initalize shared memory if I'm the first to create it
  53. if (NeedInit) Initialize();
  54. #if defined(SLBIOP_USE_SECURITY_ATTRIBUTES)
  55. delete sa;
  56. #endif
  57. }
  58. CSharedMarker::~CSharedMarker()
  59. {
  60. }
  61. CMarker CSharedMarker::Marker(CMarker::MarkerType const &Type)
  62. {
  63. const bool bRecover = true;
  64. if ((Type<0) || (Type>=CMarker::MaximumMarker))
  65. throw Exception(ccInvalidParameter);
  66. Transaction foo(m_hMutex);
  67. VerifyCheckSum(bRecover);
  68. return CMarker(Type,m_pShMemData->ShMemID,m_pShMemData->CounterList[Type]);
  69. }
  70. CMarker CSharedMarker::UpdateMarker(CMarker::MarkerType const &Type)
  71. {
  72. const bool bRecover = true;
  73. if ((Type < 0) || (Type >= CMarker::MaximumMarker))
  74. throw Exception(ccInvalidParameter);
  75. Transaction foo(m_hMutex);
  76. VerifyCheckSum(bRecover);
  77. (m_pShMemData->CounterList[Type])++;
  78. UpdateCheckSum();
  79. return CMarker(Type,m_pShMemData->ShMemID,m_pShMemData->CounterList[Type]);
  80. }
  81. void CSharedMarker::Initialize()
  82. {
  83. RPC_STATUS status = UuidCreate(&(m_pShMemData->ShMemID));
  84. if ((status!=RPC_S_OK) && (status!=RPC_S_UUID_LOCAL_ONLY))
  85. throw scu::OsException(status);
  86. for (int i=0; i<CMarker::MaximumMarker; i++)
  87. m_pShMemData->CounterList[i] = 1;
  88. UpdateCheckSum();
  89. }
  90. void CSharedMarker::VerifyCheckSum(bool bRecover)
  91. {
  92. unsigned long ChSumLen = (unsigned char*)&m_pShMemData->CheckSum - (unsigned char*)m_pShMemData;
  93. if(m_pShMemData->CheckSum!=Crc32(m_pShMemData,ChSumLen)) {
  94. if(bRecover)
  95. Initialize();
  96. else
  97. throw Exception(ccInvalidChecksum);
  98. }
  99. }
  100. void CSharedMarker::UpdateCheckSum()
  101. {
  102. unsigned long ChSumLen = (unsigned char*)&m_pShMemData->CheckSum - (unsigned char*)m_pShMemData;
  103. m_pShMemData->CheckSum = Crc32(m_pShMemData,ChSumLen);
  104. }
  105. } // namespace iop