tc_shm.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_shm.h"
  17. #include "util/tc_common.h"
  18. #include <cassert>
  19. #include <errno.h>
  20. namespace tars
  21. {
  22. TC_Shm::TC_Shm(size_t iShmSize, key_t iKey, bool bOwner) : _pshm(NULL)
  23. {
  24. init(iShmSize, iKey, bOwner);
  25. }
  26. TC_Shm::~TC_Shm()
  27. {
  28. if (_bOwner)
  29. {
  30. detach();
  31. }
  32. }
  33. void TC_Shm::init(size_t iShmSize, key_t iKey, bool bOwner)
  34. {
  35. assert(_pshm == NULL);
  36. #if TARGET_PLATFORM_WINDOWS
  37. _bOwner = bOwner;
  38. // 首先试图打开一个命名的内存映射文件对象
  39. HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, TC_Common::tostr(iKey).c_str());
  40. if (NULL == hMap)
  41. {
  42. // 打开失败,创建之
  43. hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL,PAGE_READWRITE,0, (DWORD)iShmSize, TC_Common::tostr(iKey).c_str());
  44. // 映射对象的一个视图,得到指向共享内存的指针,设置里面的数据
  45. _pshm = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  46. }
  47. else
  48. { // 打开成功,映射对象的一个视图,得到指向共享内存的指针,显示出里面的数据
  49. _pshm = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  50. }
  51. _shemID = hMap;
  52. _shmKey = iKey;
  53. #else
  54. _bOwner = bOwner;
  55. //注意_bCreate的赋值位置:保证多线程用一个对象的时候也不会有问题
  56. //试图创建
  57. if ((_shemID = shmget(iKey, iShmSize, IPC_CREAT | IPC_EXCL | 0666)) < 0)
  58. {
  59. _bCreate = false;
  60. //有可能是已经存在同样的key_shm,则试图连接
  61. if ((_shemID = shmget(iKey, iShmSize, 0666)) < 0)
  62. {
  63. THROW_EXCEPTION_SYSCODE(TC_Shm_Exception, "[TC_Shm::init()] shmget error");
  64. // throw TC_Shm_Exception("[TC_Shm::init()] shmget error", TC_Exception::getSystemCode());
  65. }
  66. }
  67. else
  68. {
  69. _bCreate = true;
  70. }
  71. //try to access shm
  72. if ((_pshm = shmat(_shemID, NULL, 0)) == (char *) - 1)
  73. {
  74. THROW_EXCEPTION_SYSCODE(TC_Shm_Exception, "[TC_Shm::init()] shmat error");
  75. // throw TC_Shm_Exception("[TC_Shm::init()] shmat error", TC_Exception::getSystemCode());
  76. }
  77. _shmSize = iShmSize;
  78. _shmKey = iKey;
  79. #endif
  80. }
  81. int TC_Shm::detach()
  82. {
  83. #if TARGET_PLATFORM_WINDOWS
  84. if (_pshm != NULL)
  85. {
  86. UnmapViewOfFile(_pshm);
  87. CloseHandle(_shemID);
  88. _pshm = NULL;
  89. }
  90. return 0;
  91. #else
  92. int iRetCode = 0;
  93. if (_pshm != NULL)
  94. {
  95. iRetCode = shmdt(_pshm);
  96. _pshm = NULL;
  97. }
  98. return iRetCode;
  99. #endif
  100. }
  101. int TC_Shm::del()
  102. {
  103. #if TARGET_PLATFORM_WINDOWS
  104. return 0;
  105. #else
  106. int iRetCode = 0;
  107. if (_pshm != NULL)
  108. {
  109. iRetCode = shmctl(_shemID, IPC_RMID, 0);
  110. _pshm = NULL;
  111. }
  112. return iRetCode;
  113. #endif
  114. }
  115. }