Remove invalid error message
[ardour.git] / libs / pbd / crossthread.posix.cc
1 /*
2  * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <poll.h>
21
22 CrossThreadChannel::CrossThreadChannel (bool non_blocking)
23         : receive_channel (0)
24         , receive_source (0)
25 {
26         fds[0] = -1;
27         fds[1] = -1;
28
29         if (pipe (fds)) {
30                 error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg;
31                 return;
32         }
33
34         if (non_blocking) {
35                 if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
36                         error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
37                         return;
38                 }
39
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;
42                         return;
43                 }
44         }
45
46         receive_channel = g_io_channel_unix_new (fds[0]);
47 }
48
49 CrossThreadChannel::~CrossThreadChannel ()
50 {
51         if (receive_source) {
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.
55                 */
56                 g_source_destroy (receive_source);
57         }
58
59         if (receive_channel) {
60                 g_io_channel_unref (receive_channel);
61                 receive_channel = 0;
62         }
63
64         if (fds[0] >= 0) {
65                 close (fds[0]);
66                 fds[0] = -1;
67         }
68
69         if (fds[1] >= 0) {
70                 close (fds[1]);
71                 fds[1] = -1;
72         }
73 }
74
75 void
76 CrossThreadChannel::wakeup ()
77 {
78         char c = 0;
79         (void) ::write (fds[1], &c, 1);
80 }
81
82 void
83 CrossThreadChannel::drain ()
84 {
85         char buf[64];
86         while (::read (fds[0], buf, sizeof (buf)) > 0) {};
87 }
88
89 int
90 CrossThreadChannel::deliver (char msg)
91 {
92         return ::write (fds[1], &msg, 1);
93 }
94
95 bool
96 CrossThreadChannel::poll_for_request()
97 {
98         struct pollfd pfd[1];
99         pfd[0].fd = fds[0];
100         pfd[0].events = POLLIN|POLLERR|POLLHUP;
101         while(true) {
102                 if (poll (pfd, 1, -1) < 0) {
103                         if (errno == EINTR) {
104                                 continue;
105                         }
106                         break;
107                 }
108                 if (pfd[0].revents & ~POLLIN) {
109                         break;
110                 }
111
112                 if (pfd[0].revents & POLLIN) {
113                         return true;
114                 }
115         }
116         return false;
117 }
118
119 int
120 CrossThreadChannel::receive (char& msg, bool wait)
121 {
122         if (wait) {
123                 if (!poll_for_request ()) {
124                         return -1;
125                 }
126         }
127         return ::read (fds[0], &msg, 1);
128 }