关键词搜索

源码搜索 ×
×

tars源码分析之14

发布2022-07-10浏览592次

详情内容

线程互斥,实现起来不难,一起看下:

  1. #include "util/tc_thread_mutex.h"
  2. #include <string.h>
  3. #include <iostream>
  4. #include <cassert>
  5. namespace tars
  6. {
  7. TC_ThreadMutex::TC_ThreadMutex()
  8. {
  9. int rc;
  10. pthread_mutexattr_t attr;
  11. rc = pthread_mutexattr_init(&attr);
  12. assert(rc == 0);
  13. rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  14. assert(rc == 0);
  15. rc = pthread_mutex_init(&_mutex, &attr);
  16. assert(rc == 0);
  17. rc = pthread_mutexattr_destroy(&attr);
  18. assert(rc == 0);
  19. if(rc != 0)
  20. {
  21. throw TC_ThreadMutex_Exception("[TC_ThreadMutex::TC_ThreadMutex] pthread_mutexattr_init error", rc);
  22. }
  23. }
  24. TC_ThreadMutex::~TC_ThreadMutex()
  25. {
  26. int rc = 0;
  27. rc = pthread_mutex_destroy(&_mutex);
  28. if(rc != 0)
  29. {
  30. cerr << "[TC_ThreadMutex::~TC_ThreadMutex] pthread_mutex_destroy error:" << string(strerror(rc)) << endl;
  31. }
  32. // assert(rc == 0);
  33. }
  34. void TC_ThreadMutex::lock() const
  35. {
  36. int rc = pthread_mutex_lock(&_mutex);
  37. if(rc != 0)
  38. {
  39. if(rc == EDEADLK)
  40. {
  41. throw TC_ThreadMutex_Exception("[TC_ThreadMutex::lock] pthread_mutex_lock dead lock error", rc);
  42. }
  43. else
  44. {
  45. throw TC_ThreadMutex_Exception("[TC_ThreadMutex::lock] pthread_mutex_lock error", rc);
  46. }
  47. }
  48. }
  49. bool TC_ThreadMutex::tryLock() const
  50. {
  51. int rc = pthread_mutex_trylock(&_mutex);
  52. if(rc != 0 && rc != EBUSY)
  53. {
  54. if(rc == EDEADLK)
  55. {
  56. throw TC_ThreadMutex_Exception("[TC_ThreadMutex::tryLock] pthread_mutex_trylock dead lock error", rc);
  57. }
  58. else
  59. {
  60. throw TC_ThreadMutex_Exception("[TC_ThreadMutex::tryLock] pthread_mutex_trylock error", rc);
  61. }
  62. }
  63. return (rc == 0);
  64. }
  65. void TC_ThreadMutex::unlock() const
  66. {
  67. int rc = pthread_mutex_unlock(&_mutex);
  68. if(rc != 0)
  69. {
  70. throw TC_ThreadMutex_Exception("[TC_ThreadMutex::unlock] pthread_mutex_unlock error", rc);
  71. }
  72. }
  73. int TC_ThreadMutex::count() const
  74. {
  75. return 0;
  76. }
  77. void TC_ThreadMutex::count(int c) const
  78. {
  79. }
  80. ///
  81. TC_ThreadRecMutex::TC_ThreadRecMutex()
  82. : _count(0)
  83. {
  84. int rc;
  85. pthread_mutexattr_t attr;
  86. rc = pthread_mutexattr_init(&attr);
  87. if(rc != 0)
  88. {
  89. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::TC_ThreadRecMutex] pthread_mutexattr_init error", rc);
  90. }
  91. rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  92. if(rc != 0)
  93. {
  94. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::TC_ThreadRecMutex] pthread_mutexattr_settype error", rc);
  95. }
  96. rc = pthread_mutex_init(&_mutex, &attr);
  97. if(rc != 0)
  98. {
  99. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::TC_ThreadRecMutex] pthread_mutex_init error", rc);
  100. }
  101. rc = pthread_mutexattr_destroy(&attr);
  102. if(rc != 0)
  103. {
  104. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::TC_ThreadRecMutex] pthread_mutexattr_destroy error", rc);
  105. }
  106. }
  107. TC_ThreadRecMutex::~TC_ThreadRecMutex()
  108. {
  109. while (_count)
  110. {
  111. unlock();
  112. }
  113. int rc = 0;
  114. rc = pthread_mutex_destroy(&_mutex);
  115. if(rc != 0)
  116. {
  117. cerr << "[TC_ThreadRecMutex::~TC_ThreadRecMutex] pthread_mutex_destroy error:" << string(strerror(rc)) << endl;
  118. }
  119. // assert(rc == 0);
  120. }
  121. int TC_ThreadRecMutex::lock() const
  122. {
  123. int rc = pthread_mutex_lock(&_mutex);
  124. if(rc != 0)
  125. {
  126. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::lock] pthread_mutex_lock error", rc);
  127. }
  128. if(++_count > 1)
  129. {
  130. rc = pthread_mutex_unlock(&_mutex);
  131. assert(rc == 0);
  132. }
  133. return rc;
  134. }
  135. int TC_ThreadRecMutex::unlock() const
  136. {
  137. if(--_count == 0)
  138. {
  139. int rc = 0;
  140. rc = pthread_mutex_unlock(&_mutex);
  141. return rc;
  142. }
  143. return 0;
  144. }
  145. bool TC_ThreadRecMutex::tryLock() const
  146. {
  147. int rc = pthread_mutex_trylock(&_mutex);
  148. if(rc != 0 )
  149. {
  150. if(rc != EBUSY)
  151. {
  152. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::tryLock] pthread_mutex_trylock error", rc);
  153. }
  154. }
  155. else if(++_count > 1)
  156. {
  157. rc = pthread_mutex_unlock(&_mutex);
  158. if(rc != 0)
  159. {
  160. throw TC_ThreadMutex_Exception("[TC_ThreadRecMutex::tryLock] pthread_mutex_unlock error", rc);
  161. }
  162. }
  163. return (rc == 0);
  164. }
  165. bool TC_ThreadRecMutex::willUnlock() const
  166. {
  167. return _count == 1;
  168. }
  169. int TC_ThreadRecMutex::count() const
  170. {
  171. int c = _count;
  172. _count = 0;
  173. return c;
  174. }
  175. void TC_ThreadRecMutex::count(int c) const
  176. {
  177. _count = c;
  178. }
  179. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载