tc_shm.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // 首先试图打开一个命名的内存映射文件对象
  38. HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, TC_Common::tostr(iKey).c_str());
  39. if (NULL == hMap)
  40. {
  41. // 打开失败,创建之
  42. hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL,PAGE_READWRITE,0, iShmSize, TC_Common::tostr(iKey).c_str());
  43. // 映射对象的一个视图,得到指向共享内存的指针,设置里面的数据
  44. _pshm = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  45. }
  46. else
  47. { // 打开成功,映射对象的一个视图,得到指向共享内存的指针,显示出里面的数据
  48. _pshm = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  49. }
  50. _shemID = hMap;
  51. _shmKey = iKey;
  52. #else
  53. _bOwner = bOwner;
  54. //注意_bCreate的赋值位置:保证多线程用一个对象的时候也不会有问题
  55. //试图创建
  56. if ((_shemID = shmget(iKey, iShmSize, IPC_CREAT | IPC_EXCL | 0666)) < 0)
  57. {
  58. _bCreate = false;
  59. //有可能是已经存在同样的key_shm,则试图连接
  60. if ((_shemID = shmget(iKey, iShmSize, 0666)) < 0)
  61. {
  62. THROW_EXCEPTION_SYSCODE(TC_Shm_Exception, "[TC_Shm::init()] shmget error");
  63. // throw TC_Shm_Exception("[TC_Shm::init()] shmget error", TC_Exception::getSystemCode());
  64. }
  65. }
  66. else
  67. {
  68. _bCreate = true;
  69. }
  70. //try to access shm
  71. if ((_pshm = shmat(_shemID, NULL, 0)) == (char *) - 1)
  72. {
  73. THROW_EXCEPTION_SYSCODE(TC_Shm_Exception, "[TC_Shm::init()] shmat error");
  74. // throw TC_Shm_Exception("[TC_Shm::init()] shmat error", TC_Exception::getSystemCode());
  75. }
  76. _shmSize = iShmSize;
  77. _shmKey = iKey;
  78. #endif
  79. }
  80. int TC_Shm::detach()
  81. {
  82. #if TARGET_PLATFORM_WINDOWS
  83. if (_pshm != NULL)
  84. {
  85. UnmapViewOfFile(_pshm);
  86. CloseHandle(_shemID);
  87. }
  88. return 0;
  89. #else
  90. int iRetCode = 0;
  91. if (_pshm != NULL)
  92. {
  93. iRetCode = shmdt(_pshm);
  94. _pshm = NULL;
  95. }
  96. return iRetCode;
  97. #endif
  98. }
  99. int TC_Shm::del()
  100. {
  101. #if TARGET_PLATFORM_WINDOWS
  102. return 0;
  103. #else
  104. int iRetCode = 0;
  105. if (_pshm != NULL)
  106. {
  107. iRetCode = shmctl(_shemID, IPC_RMID, 0);
  108. _pshm = NULL;
  109. }
  110. return iRetCode;
  111. #endif
  112. }
  113. }