3 CrossThreadChannel::CrossThreadChannel (bool non_blocking)
11 error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg;
16 if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
17 error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
21 if (fcntl (fds[1], F_SETFL, O_NONBLOCK)) {
22 error << "cannot set non-blocking mode for x-thread pipe (write) (%2)" << ::strerror (errno) << ')' << endmsg;
27 receive_channel = g_io_channel_unix_new (fds[0]);
30 CrossThreadChannel::~CrossThreadChannel ()
33 /* this disconnects it from any main context it was attached in
34 in ::attach(), this prevent its callback from being invoked
35 after the destructor has finished.
37 g_source_destroy (receive_source);
40 if (receive_channel) {
41 g_io_channel_unref (receive_channel);
57 CrossThreadChannel::wakeup ()
60 (void) ::write (fds[1], &c, 1);
64 CrossThreadChannel::drain ()
67 while (::read (fds[0], buf, sizeof (buf)) > 0) {};
71 CrossThreadChannel::deliver (char msg)
73 return ::write (fds[1], &msg, 1);
77 CrossThreadChannel::poll_for_request()
81 pfd[0].events = POLLIN|POLLERR|POLLHUP;
83 if (poll (pfd, 1, -1) < 0) {
89 if (pfd[0].revents & ~POLLIN) {
93 if (pfd[0].revents & POLLIN) {
101 CrossThreadChannel::receive (char& msg, bool wait)
104 if (!poll_for_request ()) {
108 return ::read (fds[0], &msg, 1);