jmem_policy.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef _JMEM_POLICY_H
  17. #define _JMEM_POLICY_H
  18. #include "util/tc_thread_mutex.h"
  19. #include "util/tc_sem_mutex.h"
  20. #include "util/tc_shm.h"
  21. #include "util/tc_mmap.h"
  22. namespace tars
  23. {
  24. //////////////////////////////////////////////////////////////////////
  25. // 存储策略: 内存, 共享内存, mmap(文件)
  26. /**
  27. * 内存存储
  28. */
  29. template<typename T, typename LockPolicy>
  30. class MemStorePolicy : public LockPolicy
  31. {
  32. public:
  33. /**
  34. * 初始化
  35. * @param pAddr: 指令队列空间的指针
  36. * @param iSize: 空间的指针
  37. */
  38. void create(void *pAddr, size_t iSize)
  39. {
  40. _t.create(pAddr,iSize);
  41. }
  42. /**
  43. * 连接上队列
  44. * @param pAddr: 指令队列空间的指针
  45. * @param iSize: 空间的指针
  46. */
  47. void connect(void *pAddr, size_t iSize)
  48. {
  49. _t.connect(pAddr,iSize);
  50. }
  51. protected:
  52. T _t;
  53. };
  54. /**
  55. * 共享内存存储
  56. */
  57. template<typename T, typename LockPolicy>
  58. class ShmStorePolicy : public LockPolicy
  59. {
  60. public:
  61. /**
  62. * 初始化共享存储
  63. * @param iShmKey
  64. * @param iSize
  65. */
  66. void initStore(key_t iShmKey, size_t iSize)
  67. {
  68. _shm.init(iSize, iShmKey);
  69. if(_shm.iscreate())
  70. {
  71. _t.create(_shm.getPointer(), iSize);
  72. }
  73. else
  74. {
  75. _t.connect(_shm.getPointer(), iSize);
  76. }
  77. }
  78. /**
  79. * 释放共享内存
  80. */
  81. void release()
  82. {
  83. _shm.del();
  84. }
  85. protected:
  86. TC_Shm _shm;
  87. T _t;
  88. };
  89. /**
  90. * 文件存储
  91. */
  92. template<typename T, typename LockPolicy>
  93. class FileStorePolicy : public LockPolicy
  94. {
  95. public:
  96. /**
  97. * 初始化文件
  98. * @param file, 文件路径
  99. * @param iSize, 文件大小
  100. */
  101. void initStore(const char *file, size_t iSize)
  102. {
  103. _file = file;
  104. _mmap.mmap(file, iSize);
  105. if(_mmap.iscreate())
  106. {
  107. _t.create(_mmap.getPointer(), iSize);
  108. }
  109. else
  110. {
  111. _t.connect(_mmap.getPointer(), iSize);
  112. }
  113. }
  114. /**
  115. * 扩展空间, 目前只对hashmap有效
  116. */
  117. int expand(size_t iSize)
  118. {
  119. TC_LockT<typename LockPolicy::Mutex> lock(LockPolicy::mutex());
  120. TC_Mmap m(false);
  121. m.mmap(_file.c_str(), iSize);
  122. int ret = _t.append(m.getPointer(), iSize);
  123. if(ret == 0)
  124. {
  125. _mmap.munmap();
  126. _mmap = m;
  127. _mmap.setOwner(true);
  128. }
  129. else
  130. {
  131. m.munmap();
  132. }
  133. return ret;
  134. }
  135. protected:
  136. string _file;
  137. TC_Mmap _mmap;
  138. T _t;
  139. };
  140. //////////////////////////////////////////////////////////////////////
  141. // 锁策略: 无锁, 线程锁, 进程锁
  142. /**
  143. * 无锁
  144. */
  145. class EmptyLockPolicy
  146. {
  147. public:
  148. typedef TC_EmptyMutex Mutex;
  149. Mutex &mutex() { return _mutex; }
  150. protected:
  151. Mutex _mutex;
  152. };
  153. /**
  154. * 线程锁策略
  155. */
  156. class ThreadLockPolicy
  157. {
  158. public:
  159. typedef TC_ThreadMutex Mutex;
  160. Mutex &mutex() { return _mutex; }
  161. protected:
  162. Mutex _mutex;
  163. };
  164. /**
  165. * 进程锁策略
  166. */
  167. class SemLockPolicy
  168. {
  169. public:
  170. typedef TC_SemMutex Mutex;
  171. void initLock(key_t iSemKey) { return _mutex.init(iSemKey); }
  172. Mutex &mutex() { return _mutex; }
  173. protected:
  174. Mutex _mutex;
  175. };
  176. }
  177. #endif