Guest User

crossthread 3 diffs

a guest
Oct 28th, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.42 KB | None | 0 0
  1. diff --git a/libs/pbd/crossthread.cc b/libs/pbd/crossthread.cc
  2. index bf21d2befc..84e42e5320 100644
  3. --- a/libs/pbd/crossthread.cc
  4. +++ b/libs/pbd/crossthread.cc
  5. @@ -16,20 +16,20 @@
  6. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  7. */
  8.  
  9. +#ifndef PLATFORM_WINDOWS
  10. +#include <unistd.h>
  11. +#endif
  12.  
  13. #include <cstdlib>
  14. #include <cerrno>
  15. #include <cstring>
  16. #include <fcntl.h>
  17. -#include <unistd.h>
  18.  
  19. -#ifdef PLATFORM_WINDOWS
  20. -#include <winsock2.h>
  21. -#include <ws2tcpip.h>
  22. -#endif
  23. +#include <glibmm.h>
  24. +#include <giomm.h>
  25.  
  26. -#include "pbd/error.h"
  27. #include "pbd/crossthread.h"
  28. +#include "pbd/error.h"
  29.  
  30. using namespace std;
  31. using namespace PBD;
  32. @@ -41,28 +41,14 @@ using namespace Glib;
  33. #include "crossthread.win.cc"
  34. #endif
  35.  
  36. -#ifndef G_SOURCE_FUNC
  37. -#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
  38. -#endif
  39. -
  40. -gboolean
  41. -cross_thread_channel_call_receive_slot (GIOChannel*, GIOCondition condition, void *data)
  42. -{
  43. - CrossThreadChannel* ctc = static_cast<CrossThreadChannel*>(data);
  44. - return ctc->receive_slot (Glib::IOCondition (condition));
  45. -}
  46. -
  47. -void
  48. -CrossThreadChannel::set_receive_handler (sigc::slot<bool,Glib::IOCondition> s)
  49. -{
  50. - receive_slot = s;
  51. -}
  52. -
  53. void
  54. CrossThreadChannel::attach (Glib::RefPtr<Glib::MainContext> context)
  55. {
  56. - receive_source = g_io_create_watch (receive_channel, GIOCondition(G_IO_IN | G_IO_ERR | G_IO_HUP));
  57. -
  58. - g_source_set_callback (receive_source, G_SOURCE_FUNC(cross_thread_channel_call_receive_slot), this, NULL);
  59. - g_source_attach (receive_source, context->gobj());
  60. + source = Gio::SocketSource::create (recv_socket,
  61. + static_cast<Glib::IOCondition>(G_IO_IN | G_IO_ERR | G_IO_HUP));
  62. + CrossThreadChannel* self = this;
  63. + source->connect ([self](Glib::IOCondition cond) -> bool {
  64. + return self->receive_slot (cond);
  65. + });
  66. + source->attach (context);
  67.  
  68.  
  69. diff --git a/libs/pbd/crossthread.win.cc b/libs/pbd/crossthread.win.cc
  70. index 1d0758c911..f117e1ec80 100644
  71. --- a/libs/pbd/crossthread.win.cc
  72. +++ b/libs/pbd/crossthread.win.cc
  73. @@ -18,187 +18,53 @@
  74. */
  75.  
  76. CrossThreadChannel::CrossThreadChannel (bool non_blocking)
  77. - : receive_channel (0)
  78. - , receive_source (0)
  79. - , receive_slot ()
  80. - , send_socket()
  81. - , receive_socket()
  82. - , recv_address()
  83. + : receive_slot ()
  84. {
  85. - struct sockaddr_in send_address;
  86. + auto inet = Gio::InetAddress::create("127.0.0.1");
  87. + Gio::SocketFamily family = static_cast<Gio::SocketFamily>(G_SOCKET_FAMILY_IPV4);
  88. + Gio::Socket::Type type = static_cast<Gio::Socket::Type>(G_SOCKET_TYPE_DATAGRAM);
  89. + Gio::Socket::Protocol proto = static_cast<Gio::Socket::Protocol>(G_SOCKET_PROTOCOL_UDP);
  90.  
  91. - // Create Send Socket
  92. - send_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  93. - send_address.sin_family = AF_INET;
  94. - send_address.sin_addr.s_addr = inet_addr("127.0.0.1");
  95. - send_address.sin_port = htons(0);
  96. - int status = ::bind(send_socket, (SOCKADDR*)&send_address,
  97. - sizeof(send_address));
  98. + send_socket = Gio::Socket::create(family, type, proto);
  99. + recv_socket = Gio::Socket::create(family, type, proto);
  100.  
  101. - if (status != 0) {
  102. - std::cerr << "CrossThreadChannel::CrossThreadChannel() Send socket binding failed with error: " << WSAGetLastError() << std::endl;
  103. - return;
  104. - }
  105. + recv_socket->bind(Gio::InetSocketAddress::create(inet, 0), true);
  106.  
  107. - // make the socket non-blockable if required
  108. - u_long mode = (u_long)non_blocking;
  109. - int otp_result = 0;
  110. -
  111. - otp_result = ioctlsocket(send_socket, FIONBIO, &mode);
  112. - if (otp_result != NO_ERROR) {
  113. - std::cerr << "CrossThreadChannel::CrossThreadChannel() Send socket cannot be set to non blocking mode with error: " << WSAGetLastError() << std::endl;
  114. - }
  115. -
  116. - // Create Receive Socket, this socket will be set to unblockable mode by IO channel
  117. - receive_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  118. - recv_address.sin_family = AF_INET;
  119. - recv_address.sin_addr.s_addr = inet_addr("127.0.0.1");
  120. - recv_address.sin_port = htons(0);
  121. - status = ::bind(receive_socket, (SOCKADDR*)&recv_address,
  122. - sizeof(recv_address));
  123. -
  124. - if (status != 0) {
  125. - std::cerr << "CrossThreadChannel::CrossThreadChannel() Receive socket binding failed with error: " << WSAGetLastError() << std::endl;
  126. - return;
  127. - }
  128. -
  129. - // recieve socket will be made non-blocking by GSource which will use it
  130. -
  131. - // get assigned port number for Receive Socket
  132. - int recv_addr_len = sizeof(recv_address);
  133. - status = getsockname(receive_socket, (SOCKADDR*)&recv_address, &recv_addr_len);
  134. -
  135. - if (status != 0) {
  136. - std::cerr << "CrossThreadChannel::CrossThreadChannel() Setting receive socket address to local failed with error: " << WSAGetLastError() << std::endl;
  137. - return;
  138. - }
  139. -
  140. - // construct IOChannel
  141. - receive_channel = g_io_channel_win32_new_socket((gint)receive_socket);
  142. -
  143. - // set binary data type
  144. - GIOStatus g_status = g_io_channel_set_encoding (receive_channel, NULL, NULL);
  145. - if (G_IO_STATUS_NORMAL != g_status ) {
  146. - std::cerr << "CrossThreadChannel::CrossThreadChannel() Cannot set flag for IOChannel. " << g_status << std::endl;
  147. - return;
  148. - }
  149. -
  150. - // disable channel buffering
  151. - g_io_channel_set_buffered (receive_channel, false);
  152. -}
  153. -
  154. -CrossThreadChannel::~CrossThreadChannel ()
  155. -{
  156. - if (receive_source) {
  157. - /* this disconnects it from any main context it was attached in
  158. - in ::attach(), this prevent its callback from being invoked
  159. - after the destructor has finished.
  160. - */
  161. - g_source_destroy (receive_source);
  162. - }
  163. -
  164. - /* glibmm hack */
  165. -
  166. - if (receive_channel) {
  167. - g_io_channel_unref (receive_channel);
  168. - }
  169. -
  170. - closesocket(send_socket);
  171. - closesocket(receive_socket);
  172. + send_socket->set_blocking(!non_blocking);
  173. + recv_socket->set_blocking(!non_blocking);
  174. }
  175.  
  176. -void
  177. -CrossThreadChannel::wakeup ()
  178. +CrossThreadChannel::~CrossThreadChannel()
  179. {
  180. - char c = 0;
  181. -
  182. - // write one byte to wake up a thread which is listening our IOS
  183. - sendto(send_socket, &c, sizeof(c), 0, (SOCKADDR*)&recv_address, sizeof(recv_address) );
  184. + if (source)
  185. + source->destroy();
  186. }
  187.  
  188. void
  189. -CrossThreadChannel::drain ()
  190. +CrossThreadChannel::wakeup()
  191. {
  192. - /* flush the buffer - empty the channel from all requests */
  193. - GError *g_error = 0;
  194. - gchar buffer[512];
  195. - gsize read = 0;
  196. -
  197. - while (1) {
  198. - GIOStatus g_status = g_io_channel_read_chars (receive_channel, buffer, sizeof(buffer), &read, &g_error);
  199. -
  200. - if (G_IO_STATUS_AGAIN == g_status) {
  201. - break;
  202. - }
  203. -
  204. - if (G_IO_STATUS_NORMAL != g_status) {
  205. - std::cerr << "CrossThreadChannel::CrossThreadChannel() Cannot drain from read buffer! " << g_status << std::endl;
  206. -
  207. - if (g_error) {
  208. - std::cerr << "Error is Domain: " << g_error->domain << " Code: " << g_error->code << std::endl;
  209. - g_clear_error(&g_error);
  210. - } else {
  211. - std::cerr << "No error provided\n";
  212. - }
  213. - break;
  214. - }
  215. - }
  216. + char c = 0;
  217. + deliver(c);
  218. }
  219.  
  220. -
  221. int
  222. -CrossThreadChannel::deliver (char msg)
  223. +CrossThreadChannel::deliver(char msg)
  224. {
  225. -
  226. - // write one particular byte to wake up the thread which is listening our IOS
  227. - int status = sendto(send_socket, &msg, sizeof(msg), 0, (SOCKADDR*)&recv_address, sizeof(recv_address) );
  228. -
  229. - if (SOCKET_ERROR == status) {
  230. - return -1;
  231. - }
  232. -
  233. - return status;
  234. + auto addr = recv_socket->get_local_address();
  235. + gssize sent = send_socket->send_to(addr, &msg, sizeof(msg));
  236. + return (sent < 0) ? -1 : static_cast<int>(sent);
  237. }
  238.  
  239. -bool
  240. -CrossThreadChannel::poll_for_request()
  241. +int
  242. +CrossThreadChannel::receive(char& msg, bool /*wait*/)
  243. {
  244. - // windows before Vista has no poll
  245. - while(true) {
  246. - fd_set rfds;
  247. - FD_ZERO(&rfds);
  248. - FD_SET(receive_socket, &rfds);
  249. - if ((select(receive_socket+1, &rfds, NULL, NULL, NULL)) < 0) {
  250. - if (errno == EINTR) {
  251. - continue;
  252. - }
  253. - break;
  254. - }
  255. - if(FD_ISSET(receive_socket, &rfds)) {
  256. - return true;
  257. - }
  258. - }
  259. - return false;
  260. + gssize r = recv_socket->receive(&msg, sizeof(msg));
  261. + return (r < 0) ? -1 : static_cast<int>(r);
  262. }
  263.  
  264. -int
  265. -CrossThreadChannel::receive (char& msg, bool wait)
  266. +void
  267. +CrossThreadChannel::drain()
  268. {
  269. - gsize read = 0;
  270. - GError *g_error = 0;
  271. -
  272. - if (wait) {
  273. - if (!poll_for_request ()) {
  274. - return -1;
  275. - }
  276. - }
  277. -
  278. - // fetch the message from the channel.
  279. - GIOStatus g_status = g_io_channel_read_chars (receive_channel, &msg, sizeof(msg), &read, &g_error);
  280. -
  281. - if (G_IO_STATUS_NORMAL != g_status) {
  282. - read = -1;
  283. - }
  284. -
  285. - return read;
  286. + char buf[512];
  287. + while (recv_socket->receive(buf, sizeof(buf)) > 0) {}
  288. }
  289.  
  290.  
  291.  
  292. diff --git a/libs/pbd/pbd/crossthread.h b/libs/pbd/pbd/crossthread.h
  293. index 08f61a5b58..2c3b8457ab 100644
  294. --- a/libs/pbd/pbd/crossthread.h
  295. +++ b/libs/pbd/pbd/crossthread.h
  296. @@ -19,18 +19,26 @@
  297.  
  298. #pragma once
  299.  
  300. -#ifdef check
  301. -#undef check
  302. -#endif
  303. -
  304. -#include <glibmm/main.h>
  305. -
  306. #include "pbd/libpbd_visibility.h"
  307.  
  308. +#include <gio/gio.h>
  309. +
  310. #ifdef PLATFORM_WINDOWS
  311. +#include <winsock2.h>
  312. #include <windows.h>
  313. +#undef check
  314. +#undef read
  315. +#undef write
  316. +#undef close
  317. +#undef seek
  318. +#undef flush
  319. +#undef interface
  320. +#undef min
  321. +#undef max
  322. #endif // PLATFORM_WINDOWS
  323.  
  324. +#include <glibmm/main.h>
  325. +#include <giomm.h>
  326.  
  327. /** A simple abstraction of a mechanism of signalling one thread from another.
  328. * The signaller calls \ref wakeup() to tell the signalled thread to check for
  329. @@ -81,7 +89,7 @@ public:
  330. */
  331. void drain ();
  332.  
  333. - void set_receive_handler (sigc::slot<bool,Glib::IOCondition> s);
  334. + void set_receive_handler (sigc::slot<bool(Glib::IOCondition)> s);
  335. void attach (Glib::RefPtr<Glib::MainContext>);
  336.  
  337. private:
  338. @@ -89,17 +97,16 @@ private:
  339.  
  340. GIOChannel* receive_channel;
  341. GSource* receive_source;
  342. - sigc::slot<bool,Glib::IOCondition> receive_slot;
  343. + sigc::slot<bool(Glib::IOCondition)> receive_slot;
  344.  
  345. bool poll_for_request();
  346.  
  347. #ifndef PLATFORM_WINDOWS
  348. int fds[2]; // current implementation uses a pipe/fifo
  349. #else
  350. -
  351. - SOCKET send_socket;
  352. - SOCKET receive_socket;
  353. - struct sockaddr_in recv_address;
  354. + Glib::RefPtr<Gio::Socket> send_socket;
  355. + Glib::RefPtr<Gio::Socket> recv_socket;
  356. + Glib::RefPtr<Gio::SocketSource> source;
  357. #endif
  358.  
  359. };
  360.  
Advertisement
Add Comment
Please, Sign In to add comment