tc_sem_mutex.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_SEM_MUTEX_H
  17. #define __TC_SEM_MUTEX_H
  18. #include "util/tc_platform.h"
  19. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  20. #include <sys/types.h>
  21. #include <sys/ipc.h>
  22. #include <sys/sem.h>
  23. #endif
  24. #include "util/tc_lock.h"
  25. namespace tars
  26. {
  27. /////////////////////////////////////////////////
  28. /**
  29. * @file tc_sem_mutex.h
  30. * @brief 信号量锁类.
  31. *
  32. */
  33. /////////////////////////////////////////////////
  34. /**
  35. * @brief 信号量锁异常类
  36. */
  37. struct TC_SemMutex_Exception : public TC_Exception
  38. {
  39. TC_SemMutex_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
  40. ~TC_SemMutex_Exception() throw() {};
  41. };
  42. //锁名称统一用数字类型
  43. #if TARGET_PLATFORM_WINDOWS
  44. typedef int key_t;
  45. #endif
  46. /**
  47. * @brief 进程间锁, 提供两种锁机制:共享锁和排斥锁.
  48. *
  49. * 1 对于相同的key, 不同进程初始化时连接到相同的sem上
  50. * 2 采用IPC的信号量实现
  51. * 3 信号量采用了SEM_UNDO参数, 当进程结束时会自动调整信号量
  52. */
  53. class TC_SemMutex
  54. {
  55. public:
  56. /**
  57. * @brief 构造函数
  58. */
  59. TC_SemMutex();
  60. /**
  61. * @brief 析构函数
  62. */
  63. ~TC_SemMutex();
  64. /**
  65. * @brief 构造函数.
  66. *
  67. * @param iKey, key
  68. * @throws TC_SemMutex_Exception
  69. */
  70. TC_SemMutex(key_t iKey);
  71. /**
  72. * @brief 初始化.
  73. *
  74. * @param iKey, key
  75. * @throws TC_SemMutex_Exception
  76. * @return 无
  77. */
  78. void init(key_t iKey);
  79. /**
  80. * @brief 获取共享内存Key.
  81. *
  82. * @return key_t ,共享内存key
  83. */
  84. key_t getkey() const {return _semKey;}
  85. /**
  86. * @brief 获取共享内存ID.
  87. *
  88. * @return int ,共享内存Id
  89. */
  90. int getid() const {return _semID;}
  91. /**
  92. * @brief 加读锁.
  93. *
  94. *@return int
  95. */
  96. void rlock() const;
  97. /**
  98. * @brief 解读锁.
  99. *
  100. * @return int
  101. */
  102. void unrlock() const;
  103. /**
  104. * @brief 尝试读锁.
  105. *
  106. * @return bool : 加锁成功则返回false, 否则返回false
  107. */
  108. bool tryrlock() const;
  109. /**
  110. * @brief 加写锁.
  111. *
  112. * @return int
  113. */
  114. void wlock() const;
  115. /**
  116. * @brief 解写锁
  117. */
  118. void unwlock() const;
  119. /**
  120. * @brief 尝试写锁.
  121. *
  122. * @throws TC_SemMutex_Exception
  123. * @return bool : 加锁成功则返回false, 否则返回false
  124. */
  125. bool trywlock() const;
  126. /**
  127. * @brief 写锁.
  128. *
  129. * @return int, 0 正确
  130. */
  131. void lock() const {wlock();};
  132. /**
  133. * @brief 解写锁
  134. */
  135. void unlock() const {unwlock();};
  136. /**
  137. * @brief 尝试解锁.
  138. *
  139. * @throws TC_SemMutex_Exception
  140. * @return int, 0 正确
  141. */
  142. bool trylock() const {return trywlock();};
  143. #if TARGET_PLATFORM_WINDOWS
  144. protected:
  145. void addWriter() const;
  146. void removeWriter() const;
  147. void unlockImp() const;
  148. DWORD tryReadLockOnce() const ;
  149. protected:
  150. mutable HANDLE _mutex;
  151. mutable HANDLE _readEvent;
  152. mutable HANDLE _writeEvent;
  153. mutable unsigned _readers;
  154. mutable unsigned _writersWaiting;
  155. mutable unsigned _writers;
  156. #endif
  157. /**
  158. * 信号量ID
  159. */
  160. int _semID;
  161. /**
  162. * 信号量key
  163. */
  164. key_t _semKey;
  165. };
  166. }
  167. #endif
  168. //#endif