tc_file_mutex.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_FILE_MUTEX_H
  17. #define __TC_FILE_MUTEX_H
  18. #include "util/tc_platform.h"
  19. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  20. #include <unistd.h>
  21. #include "util/tc_lock.h"
  22. namespace tars
  23. {
  24. /////////////////////////////////////////////////
  25. /**
  26. * @file tc_file_mutex.h
  27. * @brief 文件锁类.
  28. *
  29. */
  30. /////////////////////////////////////////////////
  31. /**
  32. * @brief 异常类
  33. */
  34. struct TC_FileMutex_Exception : public TC_Lock_Exception
  35. {
  36. TC_FileMutex_Exception(const string &buffer) : TC_Lock_Exception(buffer){};
  37. TC_FileMutex_Exception(const string &buffer, int err) : TC_Lock_Exception(buffer, err){};
  38. ~TC_FileMutex_Exception() throw() {};
  39. };
  40. /**
  41. * @brief 文件锁, 注意:只能在进程间加锁.
  42. */
  43. class TC_FileMutex
  44. {
  45. public:
  46. /**
  47. * @brief 构造函数.
  48. */
  49. TC_FileMutex();
  50. /**
  51. * @brief 析够函数.
  52. */
  53. virtual ~TC_FileMutex();
  54. /**
  55. * @brief 初始化文件锁.
  56. *
  57. * @param filename 欲操作的文件的名字
  58. */
  59. void init(const std::string& filename);
  60. /**
  61. * @brief 加读锁.
  62. *
  63. *@return 0-成功加锁,-1-加锁失败
  64. */
  65. int rlock();
  66. /**
  67. * @brief 解读锁.
  68. *
  69. * @return 0-成功解锁,-1-解锁失败
  70. */
  71. int unrlock();
  72. /**
  73. * @brief 尝试读锁.
  74. *
  75. * @throws TC_FileMutex_Exception
  76. * @return 加锁成功则返回false, 否则返回false
  77. */
  78. bool tryrlock();
  79. /**
  80. * @brief 加写锁.
  81. *
  82. * @return int
  83. */
  84. int wlock();
  85. /**
  86. * @brief 解写锁.
  87. */
  88. int unwlock();
  89. /**
  90. * @brief 尝试写锁.
  91. *
  92. * @return bool,加锁成功则返回false, 否则返回false
  93. * @throws TC_FileMutex_Exception
  94. */
  95. bool trywlock();
  96. /**
  97. * @brief 写锁.
  98. *
  99. * @return int, 0 正确
  100. */
  101. int lock(){return wlock();};
  102. /**
  103. * @brief 解写锁.
  104. */
  105. int unlock();
  106. /**
  107. * @brief 尝试解锁.
  108. *
  109. * @throws TC_FileMutex_Exception
  110. * @return int, 0 正确
  111. */
  112. bool trylock() {return trywlock();};
  113. protected:
  114. /**
  115. * @brief 设置锁.
  116. *
  117. * @param fd 欲设置的文件描述词
  118. * @param cmd 欲操作的指令
  119. * @param type 三种状态,分别为F_RDLCK ,F_WRLCK ,F_UNLCK
  120. * @param offset 偏移量
  121. * @param whence 锁定的起始位置,三种方式
  122. * @param len 锁定区域的大小
  123. * @return int:成功则返回0,若有错误则返回-1.
  124. */
  125. int lock(int fd, int cmd, int type, off_t offset, int whence, off_t len);
  126. /**
  127. * @brief 是否被其他进程锁了.
  128. *
  129. * @param fd 欲设置的文件描述词
  130. * @param type 三种状态,分别为F_RDLCK ,F_WRLCK ,F_UNLCK
  131. * @param offset 偏移量
  132. * @param whence 锁定的起始位置,三种方式
  133. * @param len 锁定区域的大小
  134. * @return bool 有所返回true,无锁返回false.
  135. */
  136. bool hasLock(int fd, int type, off_t offset, int whence, off_t len);
  137. private:
  138. int _fd;
  139. };
  140. }
  141. #endif
  142. #endif