cmUVHandlePtr.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #define cmUVHandlePtr_cxx
  4. #include "cmUVHandlePtr.h"
  5. #include <cassert>
  6. #include <cstdlib>
  7. #include <mutex>
  8. #include <cm3p/uv.h>
  9. namespace cm {
  10. struct uv_loop_deleter
  11. {
  12. void operator()(uv_loop_t* loop) const;
  13. };
  14. void uv_loop_deleter::operator()(uv_loop_t* loop) const
  15. {
  16. uv_run(loop, UV_RUN_DEFAULT);
  17. int result = uv_loop_close(loop);
  18. (void)result;
  19. assert(result >= 0);
  20. free(loop);
  21. }
  22. int uv_loop_ptr::init(void* data)
  23. {
  24. this->reset();
  25. this->loop.reset(static_cast<uv_loop_t*>(calloc(1, sizeof(uv_loop_t))),
  26. uv_loop_deleter());
  27. this->loop->data = data;
  28. return uv_loop_init(this->loop.get());
  29. }
  30. void uv_loop_ptr::reset()
  31. {
  32. this->loop.reset();
  33. }
  34. uv_loop_ptr::operator uv_loop_t*()
  35. {
  36. return this->loop.get();
  37. }
  38. uv_loop_t* uv_loop_ptr::operator->() const noexcept
  39. {
  40. return this->loop.get();
  41. }
  42. uv_loop_t* uv_loop_ptr::get() const
  43. {
  44. return this->loop.get();
  45. }
  46. template <typename T>
  47. static void handle_default_delete(T* type_handle)
  48. {
  49. auto* handle = reinterpret_cast<uv_handle_t*>(type_handle);
  50. if (handle) {
  51. assert(!uv_is_closing(handle));
  52. if (!uv_is_closing(handle)) {
  53. uv_close(handle, [](uv_handle_t* h) { free(h); });
  54. }
  55. }
  56. }
  57. /**
  58. * Encapsulates delete logic for a given handle type T
  59. */
  60. template <typename T>
  61. struct uv_handle_deleter
  62. {
  63. void operator()(T* type_handle) const { handle_default_delete(type_handle); }
  64. };
  65. template <typename T>
  66. void uv_handle_ptr_base_<T>::allocate(void* data)
  67. {
  68. this->reset();
  69. /*
  70. We use calloc since we know all these types are c structs
  71. and we just want to 0 init them. New would do the same thing;
  72. but casting from uv_handle_t to certain other types -- namely
  73. uv_timer_t -- triggers a cast_align warning on certain systems.
  74. */
  75. this->handle.reset(static_cast<T*>(calloc(1, sizeof(T))),
  76. uv_handle_deleter<T>());
  77. this->handle->data = data;
  78. }
  79. template <typename T>
  80. void uv_handle_ptr_base_<T>::reset()
  81. {
  82. this->handle.reset();
  83. }
  84. template <typename T>
  85. uv_handle_ptr_base_<T>::operator uv_handle_t*()
  86. {
  87. return reinterpret_cast<uv_handle_t*>(this->handle.get());
  88. }
  89. template <typename T>
  90. T* uv_handle_ptr_base_<T>::operator->() const noexcept
  91. {
  92. return this->handle.get();
  93. }
  94. template <typename T>
  95. T* uv_handle_ptr_base_<T>::get() const
  96. {
  97. return this->handle.get();
  98. }
  99. template <typename T>
  100. uv_handle_ptr_<T>::operator T*() const
  101. {
  102. return this->handle.get();
  103. }
  104. #ifndef CMAKE_BOOTSTRAP
  105. template <>
  106. struct uv_handle_deleter<uv_async_t>
  107. {
  108. /***
  109. * While uv_async_send is itself thread-safe, there are
  110. * no strong guarantees that close hasn't already been
  111. * called on the handle; and that it might be deleted
  112. * as the send call goes through. This mutex guards
  113. * against that.
  114. *
  115. * The shared_ptr here is to allow for copy construction
  116. * which is mandated by the standard for Deleter on
  117. * shared_ptrs.
  118. */
  119. std::shared_ptr<std::mutex> handleMutex;
  120. uv_handle_deleter()
  121. : handleMutex(std::make_shared<std::mutex>())
  122. {
  123. }
  124. void operator()(uv_async_t* handle)
  125. {
  126. std::lock_guard<std::mutex> lock(*this->handleMutex);
  127. handle_default_delete(handle);
  128. }
  129. };
  130. void uv_async_ptr::send()
  131. {
  132. auto* deleter =
  133. std::get_deleter<uv_handle_deleter<uv_async_t>>(this->handle);
  134. assert(deleter);
  135. std::lock_guard<std::mutex> lock(*deleter->handleMutex);
  136. if (this->handle) {
  137. uv_async_send(*this);
  138. }
  139. }
  140. int uv_async_ptr::init(uv_loop_t& loop, uv_async_cb async_cb, void* data)
  141. {
  142. this->allocate(data);
  143. return uv_async_init(&loop, this->handle.get(), async_cb);
  144. }
  145. #endif
  146. template <>
  147. struct uv_handle_deleter<uv_signal_t>
  148. {
  149. void operator()(uv_signal_t* handle) const
  150. {
  151. if (handle) {
  152. uv_signal_stop(handle);
  153. handle_default_delete(handle);
  154. }
  155. }
  156. };
  157. int uv_signal_ptr::init(uv_loop_t& loop, void* data)
  158. {
  159. this->allocate(data);
  160. return uv_signal_init(&loop, this->handle.get());
  161. }
  162. int uv_signal_ptr::start(uv_signal_cb cb, int signum)
  163. {
  164. assert(this->handle);
  165. return uv_signal_start(*this, cb, signum);
  166. }
  167. void uv_signal_ptr::stop()
  168. {
  169. if (this->handle) {
  170. uv_signal_stop(*this);
  171. }
  172. }
  173. int uv_pipe_ptr::init(uv_loop_t& loop, int ipc, void* data)
  174. {
  175. this->allocate(data);
  176. return uv_pipe_init(&loop, *this, ipc);
  177. }
  178. uv_pipe_ptr::operator uv_stream_t*() const
  179. {
  180. return reinterpret_cast<uv_stream_t*>(this->handle.get());
  181. }
  182. int uv_process_ptr::spawn(uv_loop_t& loop, uv_process_options_t const& options,
  183. void* data)
  184. {
  185. this->allocate(data);
  186. return uv_spawn(&loop, *this, &options);
  187. }
  188. int uv_timer_ptr::init(uv_loop_t& loop, void* data)
  189. {
  190. this->allocate(data);
  191. return uv_timer_init(&loop, *this);
  192. }
  193. int uv_timer_ptr::start(uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
  194. {
  195. assert(this->handle);
  196. return uv_timer_start(*this, cb, timeout, repeat);
  197. }
  198. #ifndef CMAKE_BOOTSTRAP
  199. uv_tty_ptr::operator uv_stream_t*() const
  200. {
  201. return reinterpret_cast<uv_stream_t*>(this->handle.get());
  202. }
  203. int uv_tty_ptr::init(uv_loop_t& loop, int fd, int readable, void* data)
  204. {
  205. this->allocate(data);
  206. return uv_tty_init(&loop, *this, fd, readable);
  207. }
  208. #endif
  209. template class uv_handle_ptr_base_<uv_handle_t>;
  210. #define UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(NAME) \
  211. template class uv_handle_ptr_base_<uv_##NAME##_t>; \
  212. template class uv_handle_ptr_<uv_##NAME##_t>;
  213. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(signal)
  214. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(pipe)
  215. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(stream)
  216. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(process)
  217. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(timer)
  218. #ifndef CMAKE_BOOTSTRAP
  219. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(async)
  220. UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(tty)
  221. #endif
  222. }