patches from lincoln to speed up the regionlist and provide region removal (causes...
[ardour2.git] / libs / pbd / crossthread.cc
blob12ca19b945097d3cb2e1390da8bd916aa606b432
1 /*
2 Copyright (C) 2009 Paul Davis
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <cstdlib>
21 #include <cerrno>
22 #include <cstring>
23 #include <fcntl.h>
24 #include <unistd.h>
26 #include "pbd/error.h"
27 #include "pbd/crossthread.h"
29 using namespace std;
30 using namespace PBD;
31 using namespace Glib;
33 CrossThreadChannel::CrossThreadChannel ()
35 _ios = 0;
36 fds[0] = -1;
37 fds[1] = -1;
39 if (pipe (fds)) {
40 error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg;
41 return;
44 if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
45 error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
46 return;
49 if (fcntl (fds[1], F_SETFL, O_NONBLOCK)) {
50 error << "cannot set non-blocking mode for x-thread pipe (write) (%2)" << ::strerror (errno) << ')' << endmsg;
51 return;
55 CrossThreadChannel::~CrossThreadChannel ()
57 /* glibmm hack */
58 drop_ios ();
60 if (fds[0] >= 0) {
61 close (fds[0]);
62 fds[0] = -1;
65 if (fds[1] >= 0) {
66 close (fds[1]);
67 fds[1] = -1;
71 void
72 CrossThreadChannel::wakeup ()
74 char c = 0;
75 (void) ::write (fds[1], &c, 1);
78 RefPtr<IOSource>
79 CrossThreadChannel::ios ()
81 if (!_ios) {
82 _ios = new RefPtr<IOSource> (IOSource::create (fds[0], IOCondition(IO_IN|IO_PRI|IO_ERR|IO_HUP|IO_NVAL)));
84 return *_ios;
87 void
88 CrossThreadChannel::drop_ios ()
90 delete _ios;
91 _ios = 0;
94 void
95 CrossThreadChannel::drain ()
97 drain (fds[0]);
100 void
101 CrossThreadChannel::drain (int fd)
103 /* drain selectable fd */
104 char buf[64];
105 while (::read (fd, buf, sizeof (buf)) > 0);
109 CrossThreadChannel::deliver (char msg)
111 return ::write (fds[1], &msg, 1);
114 int
115 CrossThreadChannel::receive (char& msg)
117 return ::read (fds[0], &msg, 1);