tc_singleton.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 __TC_SINGLETON_H__
  17. #define __TC_SINGLETON_H__
  18. #include "util/tc_monitor.h"
  19. #include <cassert>
  20. #include <cstdlib>
  21. namespace tars
  22. {
  23. /////////////////////////////////////////////////
  24. /**
  25. * @file tc_singleton.h
  26. * @brief 单件类 .
  27. *
  28. * 单件实现类
  29. *
  30. * 没有实现对单件生命周期的管理,使用示例代码如下:
  31. *
  32. * class A : public TC_Singleton<A, CreateStatic, DefaultLifetime>
  33. *
  34. * {
  35. *
  36. * public:
  37. *
  38. * A(){cout << "A" << endl;}
  39. *
  40. * ~A()
  41. *
  42. * {
  43. *
  44. * cout << "~A" << endl;
  45. *
  46. *
  47. * }
  48. *
  49. * void test(){cout << "test A" << endl;}
  50. *
  51. * };
  52. * 对象的创建方式由CreatePolicy指定, 有如下方式:
  53. *
  54. * CreateUsingNew: 在堆中采用new创建
  55. *
  56. * CreateStatic`: 在栈中采用static创建
  57. *
  58. * 对象生命周期管理由LifetimePolicy指定, 有如下方式:
  59. *
  60. * DefaultLifetime:缺省声明周期管理
  61. *
  62. *如果单件对象已经析够, 但是还有调用, 会触发异常
  63. *
  64. * PhoneixLifetime:不死生命周期
  65. *
  66. * 如果单件对象已经析够, 但是还有调用, 会再创建一个
  67. *
  68. * NoDestroyLifetime:不析够
  69. *
  70. * 对象创建后不会调用析够函数析够, 通常采用实例中的方式就可以了
  71. *
  72. */
  73. /////////////////////////////////////////////////
  74. /**
  75. * @brief 定义CreatePolicy:定义对象创建策略
  76. */
  77. template<typename T>
  78. class CreateUsingNew
  79. {
  80. public:
  81. /**
  82. * @brief 创建.
  83. *
  84. * @return T*
  85. */
  86. static T* create()
  87. {
  88. return new T;
  89. }
  90. /**
  91. * @brief 释放.
  92. *
  93. * @param t
  94. */
  95. static void destroy(T *t)
  96. {
  97. delete t;
  98. }
  99. };
  100. template<typename T>
  101. class CreateUsingNew1
  102. {
  103. public:
  104. /**
  105. * @brief 创建.
  106. *
  107. * @return T*
  108. */
  109. static T* create()
  110. {
  111. return new T;
  112. }
  113. /**
  114. * @brief 释放.
  115. *
  116. * @param t
  117. */
  118. static void destroy(T *t)
  119. {
  120. delete t;
  121. }
  122. };
  123. template<typename T>
  124. class CreateStatic
  125. {
  126. public:
  127. /**
  128. * @brief 最大的空间
  129. */
  130. union MaxAlign
  131. {
  132. char t_[sizeof(T)];
  133. long double longDouble_;
  134. };
  135. /**
  136. * @brief 创建.
  137. *
  138. * @return T*
  139. */
  140. static T* create()
  141. {
  142. static MaxAlign t;
  143. return new(&t) T;
  144. }
  145. /**
  146. * @brief 释放.
  147. *
  148. * @param t
  149. */
  150. static void destroy(T *t)
  151. {
  152. t->~T();
  153. }
  154. };
  155. template<typename T>
  156. class CreateRealStatic
  157. {
  158. public:
  159. /**
  160. * @brief 创建.
  161. *
  162. * @return T*
  163. */
  164. static T* create()
  165. {
  166. static T t;
  167. return &t;
  168. }
  169. /**
  170. * @brief 释放.
  171. *
  172. * @param t
  173. */
  174. static void destroy(T *t)
  175. {
  176. }
  177. };
  178. ////////////////////////////////////////////////////////////////
  179. /**
  180. * @brief 定义LifetimePolicy:定义对象的声明周期管理
  181. * 进程退出时销毁对象
  182. */
  183. template<typename T>
  184. class DefaultLifetime
  185. {
  186. public:
  187. static void deadReference()
  188. {
  189. throw std::logic_error("singleton object has dead.");
  190. }
  191. static void scheduleDestruction(T*, void (*pFun)())
  192. {
  193. std::atexit(pFun);
  194. }
  195. };
  196. /**
  197. * @brief,
  198. * 对象被销毁后可以重生(比如log,全局任何时候都需要)
  199. *
  200. * @author jarod (7/29/2015)
  201. */
  202. template<typename T>
  203. class PhoneixLifetime
  204. {
  205. public:
  206. static void deadReference()
  207. {
  208. _bDestroyedOnce = true;
  209. }
  210. static void scheduleDestruction(T*, void (*pFun)())
  211. {
  212. if(!_bDestroyedOnce)
  213. std::atexit(pFun);
  214. }
  215. private:
  216. static bool _bDestroyedOnce;
  217. };
  218. template <class T>
  219. bool PhoneixLifetime<T>::_bDestroyedOnce = false;
  220. /**
  221. * @brief 不做对象销毁
  222. *
  223. * @author jarod (7/29/2015)
  224. */
  225. template <typename T>
  226. struct NoDestroyLifetime
  227. {
  228. static void scheduleDestruction(T*, void (*)())
  229. {
  230. }
  231. static void deadReference()
  232. {
  233. }
  234. };
  235. //////////////////////////////////////////////////////////////////////
  236. // Singleton
  237. template
  238. <
  239. typename T,
  240. template<typename> class CreatePolicy = CreateUsingNew,
  241. template<typename> class LifetimePolicy = DefaultLifetime
  242. >
  243. class TC_Singleton
  244. {
  245. public:
  246. typedef T instance_type;
  247. typedef volatile T volatile_type;
  248. typedef CreatePolicy<T> TCreatePolicy;
  249. /**
  250. * @brief 获取实例
  251. *
  252. * @return T*
  253. */
  254. static T *getInstance()
  255. {
  256. static std::mutex __mutex_singleton;
  257. auto sin= __pInstance.load();
  258. if ( !sin ){
  259. std::lock_guard<std::mutex> myLock(__mutex_singleton);
  260. sin= __pInstance.load();
  261. if( !sin ){
  262. if(__destroyed)
  263. {
  264. LifetimePolicy<T>::deadReference();
  265. __destroyed = false;
  266. }
  267. sin = CreatePolicy<T>::create();
  268. __pInstance.store(sin);
  269. LifetimePolicy<T>::scheduleDestruction(__pInstance, &destroySingleton);
  270. }
  271. }
  272. return sin;
  273. }
  274. virtual ~TC_Singleton(){};
  275. protected:
  276. static void destroySingleton()
  277. {
  278. assert(!__destroyed);
  279. CreatePolicy<T>::destroy((T*)__pInstance);
  280. __pInstance = NULL;
  281. __destroyed = true;
  282. }
  283. protected:
  284. static atomic<T*> __pInstance;
  285. static bool __destroyed;
  286. protected:
  287. TC_Singleton() = default;
  288. TC_Singleton (const TC_Singleton &) = default;
  289. TC_Singleton &operator=(const TC_Singleton &) = default;
  290. };
  291. template <class T, template<class> class CreatePolicy, template<class> class LifetimePolicy>
  292. bool TC_Singleton<T, CreatePolicy, LifetimePolicy>::__destroyed = false;
  293. template <class T, template<class> class CreatePolicy, template<class> class LifetimePolicy>
  294. atomic<T*> TC_Singleton<T, CreatePolicy, LifetimePolicy>::__pInstance = {nullptr};
  295. }
  296. #endif