2 * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 CrossThreadChannel::CrossThreadChannel (bool non_blocking)
30 error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg;
35 if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
36 error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
40 if (fcntl (fds[1], F_SETFL, O_NONBLOCK)) {
41 error << "cannot set non-blocking mode for x-thread pipe (write) (%2)" << ::strerror (errno) << ')' << endmsg;
46 receive_channel = g_io_channel_unix_new (fds[0]);
49 CrossThreadChannel::~CrossThreadChannel ()
52 /* this disconnects it from any main context it was attached in
53 in ::attach(), this prevent its callback from being invoked
54 after the destructor has finished.
56 g_source_destroy (receive_source);
59 if (receive_channel) {
60 g_io_channel_unref (receive_channel);
76 CrossThreadChannel::wakeup ()
79 (void) ::write (fds[1], &c, 1);
83 CrossThreadChannel::drain ()
86 while (::read (fds[0], buf, sizeof (buf)) > 0) {};
90 CrossThreadChannel::deliver (char msg)
92 return ::write (fds[1], &msg, 1);
96 CrossThreadChannel::poll_for_request()
100 pfd[0].events = POLLIN|POLLERR|POLLHUP;
102 if (poll (pfd, 1, -1) < 0) {
103 if (errno == EINTR) {
108 if (pfd[0].revents & ~POLLIN) {
112 if (pfd[0].revents & POLLIN) {
120 CrossThreadChannel::receive (char& msg, bool wait)
123 if (!poll_for_request ()) {
127 return ::read (fds[0], &msg, 1);