Guest User

.cc, .win.cc, .h

a guest
Oct 28th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2009-2015 Paul Davis <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18.  
  19. #ifdef PLATFORM_WINDOWS
  20. #define WIN32_LEAN_AND_MEAN
  21. #define NOMINMAX
  22. #include <winsock2.h>
  23. #include <windows.h>
  24. #undef check
  25. #undef read
  26. #undef write
  27. #undef close
  28. #undef seek
  29. #undef flush
  30. #undef interface
  31. #undef min
  32. #undef max
  33. #else
  34. #include <unistd.h>
  35. #endif
  36.  
  37. #include <cstdlib>
  38. #include <cerrno>
  39. #include <cstring>
  40. #include <fcntl.h>
  41.  
  42. #include <glibmm.h>
  43. #include <giomm.h>
  44.  
  45. #include "pbd/crossthread.h"
  46. #include "pbd/error.h"
  47.  
  48. using namespace std;
  49. using namespace PBD;
  50. using namespace Glib;
  51.  
  52. #ifndef PLATFORM_WINDOWS
  53. #include "crossthread.posix.cc"
  54. #else
  55. #include "crossthread.win.cc"
  56. #endif
  57.  
  58. void
  59. CrossThreadChannel::attach (Glib::RefPtr<Glib::MainContext> context)
  60. {
  61. source = Gio::SocketSource::create (recv_socket,
  62. Glib::IO_IN | Glib::IO_ERR | Glib::IO_HUP);
  63. source->connect ([this](const Glib::IOCondition& cond) {
  64. return receive_slot (cond);
  65. });
  66. source->attach (context);
  67. }
  68.  
  69. ______________________________________________________________________________________________________
  70.  
  71. /*
  72. * Copyright (C) 2006-2015 Paul Davis <[email protected]>
  73. * Copyright (C) 2015 Robin Gareus <[email protected]>
  74. *
  75. * This program is free software; you can redistribute it and/or modify
  76. * it under the terms of the GNU General Public License as published by
  77. * the Free Software Foundation; either version 2 of the License, or
  78. * (at your option) any later version.
  79. *
  80. * This program is distributed in the hope that it will be useful,
  81. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  83. * GNU General Public License for more details.
  84. *
  85. * You should have received a copy of the GNU General Public License along
  86. * with this program; if not, write to the Free Software Foundation, Inc.,
  87. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  88. */
  89.  
  90. #pragma once
  91.  
  92. #ifdef PLATFORM_WINDOWS
  93. #include <winsock2.h>
  94. #include <windows.h>
  95. #undef check
  96. #undef read
  97. #undef write
  98. #undef close
  99. #undef seek
  100. #undef flush
  101. #undef interface
  102. #undef min
  103. #undef max
  104. #endif // PLATFORM_WINDOWS
  105.  
  106. #include <glibmm/main.h>
  107. #include <giomm.h>
  108.  
  109. /** A simple abstraction of a mechanism of signalling one thread from another.
  110. * The signaller calls \ref wakeup() to tell the signalled thread to check for
  111. * work to be done.
  112. *
  113. * This implementation provides both selectable() for use in direct
  114. * poll/select-based event loops, and a Glib::IOSource via ios() for use
  115. * in Glib main loop based situations.
  116. */
  117.  
  118. class LIBPBD_API CrossThreadChannel {
  119. public:
  120. /** if @a non_blocking is true, the channel will not cause blocking
  121. * when used in an event loop based on poll/select or the glib main
  122. * loop.
  123. */
  124. CrossThreadChannel(bool non_blocking);
  125. ~CrossThreadChannel();
  126.  
  127. /** Tell the listening thread that is has work to do.
  128. */
  129. void wakeup();
  130.  
  131. /* if the listening thread cares about the precise message
  132. * it is being sent, then \ref deliver() can be used to send
  133. * a single byte message rather than a simple wakeup. These
  134. * two mechanisms should not be used on the same CrossThreadChannel
  135. * because there is no way to know which byte value will be used
  136. * for ::wakeup()
  137. */
  138. int deliver (char msg);
  139.  
  140. /** if using \ref deliver() to wakeup the listening thread, then
  141. * the listener should call \ref receive() to fetch the message
  142. * type from the channel.
  143. *
  144. * wait = true only make sense for non_blocking channels,
  145. * it polls for data to become available.
  146. */
  147. int receive (char& msg, bool wait = false);
  148.  
  149. /** empty the channel of all requests.
  150. * Typically this is done as soon as input
  151. * is noticed on the channel, because the
  152. * handler will look at a separately managed work
  153. * queue. The actual number of queued "wakeups"
  154. * in the channel will not be important.
  155. */
  156. void drain ();
  157.  
  158. void set_receive_handler (sigc::slot<bool(Glib::IOCondition)> s);
  159. void attach (Glib::RefPtr<Glib::MainContext>);
  160.  
  161. private:
  162. friend gboolean cross_thread_channel_call_receive_slot (GIOChannel*, GIOCondition condition, void *data);
  163.  
  164. GIOChannel* receive_channel;
  165. GSource* receive_source;
  166. sigc::slot<bool(Glib::IOCondition)> receive_slot;
  167.  
  168. bool poll_for_request();
  169.  
  170. #ifndef PLATFORM_WINDOWS
  171. int fds[2]; // current implementation uses a pipe/fifo
  172. #else
  173. Glib::RefPtr<Gio::Socket> send_socket;
  174. Glib::RefPtr<Gio::Socket> recv_socket;
  175. Glib::RefPtr<Gio::SocketSource> source;
  176. #endif
  177.  
  178. };
  179.  
  180.  
  181. ________________________________________________________________________________________________________________
  182.  
  183. /*
  184. * Copyright (C) 2014-2016 Paul Davis <[email protected]>
  185. * Copyright (C) 2015 Robin Gareus <[email protected]>
  186. *
  187. * This program is free software; you can redistribute it and/or modify
  188. * it under the terms of the GNU General Public License as published by
  189. * the Free Software Foundation; either version 2 of the License, or
  190. * (at your option) any later version.
  191. *
  192. * This program is distributed in the hope that it will be useful,
  193. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  194. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  195. * GNU General Public License for more details.
  196. *
  197. * You should have received a copy of the GNU General Public License along
  198. * with this program; if not, write to the Free Software Foundation, Inc.,
  199. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  200. */
  201.  
  202. CrossThreadChannel::CrossThreadChannel (bool non_blocking)
  203. : receive_slot ()
  204. {
  205. auto inet = Gio::InetAddress::create("127.0.0.1");
  206. auto family = Gio::SOCKET_FAMILY_IPV4;
  207. auto type = Gio::SOCKET_TYPE_DATAGRAM;
  208. auto proto = Gio::SOCKET_PROTOCOL_UDP;
  209.  
  210. send_socket = Gio::Socket::create(family, type, proto);
  211. recv_socket = Gio::Socket::create(family, type, proto);
  212.  
  213. recv_socket->bind(Gio::InetSocketAddress::create(inet, 0), true);
  214.  
  215. send_socket->set_blocking(!non_blocking);
  216. recv_socket->set_blocking(!non_blocking);
  217. }
  218.  
  219. CrossThreadChannel::~CrossThreadChannel()
  220. {
  221. if (source)
  222. source->destroy();
  223. }
  224.  
  225. void
  226. CrossThreadChannel::wakeup()
  227. {
  228. char c = 0;
  229. deliver(c);
  230. }
  231.  
  232. int
  233. CrossThreadChannel::deliver(char msg)
  234. {
  235. auto addr = recv_socket->get_local_address();
  236. gssize sent = send_socket->send_to(addr, &msg, sizeof(msg));
  237. return (sent < 0) ? -1 : static_cast<int>(sent);
  238. }
  239.  
  240. int
  241. CrossThreadChannel::receive(char& msg, bool /*wait*/)
  242. {
  243. gssize r = recv_socket->receive(&msg, sizeof(msg));
  244. return (r < 0) ? -1 : static_cast<int>(r);
  245. }
  246.  
  247. void
  248. CrossThreadChannel::drain()
  249. {
  250. char buf[512];
  251. while (recv_socket->receive(buf, sizeof(buf)) > 0) {}
  252. }
  253.  
  254.  
  255. ________________________________________________________________________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment