Merge branch 'master' of git.ardour.org:ardour/ardour
[ardour.git] / libs / pbd / crossthread.cc
1 /*
2     Copyright (C) 2009 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cstdlib>
21 #include <cerrno>
22 #include <cstring>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include "pbd/error.h"
27 #include "pbd/crossthread.h"
28
29 using namespace std;
30 using namespace PBD;
31 using namespace Glib;
32
33 CrossThreadChannel::CrossThreadChannel (bool non_blocking)
34 {
35         _ios = 0;
36         fds[0] = -1;
37         fds[1] = -1;
38
39         if (pipe (fds)) {
40                 error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg;
41                 return;
42         }
43
44         if (non_blocking) {
45                 if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
46                         error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
47                         return;
48                 }
49                 
50                 if (fcntl (fds[1], F_SETFL, O_NONBLOCK)) {
51                         error << "cannot set non-blocking mode for x-thread pipe (write) (%2)" << ::strerror (errno) << ')' << endmsg;
52                         return;
53                 }
54         }
55 }
56
57 CrossThreadChannel::~CrossThreadChannel ()
58 {
59         /* glibmm hack */
60         drop_ios ();
61
62         if (fds[0] >= 0) {
63                 close (fds[0]);
64                 fds[0] = -1;
65         } 
66
67         if (fds[1] >= 0) {
68                 close (fds[1]);
69                 fds[1] = -1;
70         } 
71 }
72
73 void
74 CrossThreadChannel::wakeup ()
75 {
76         char c = 0;
77         (void) ::write (fds[1], &c, 1);
78 }
79
80 RefPtr<IOSource>
81 CrossThreadChannel::ios () 
82 {
83         if (!_ios) {
84                 _ios = new RefPtr<IOSource> (IOSource::create (fds[0], IOCondition(IO_IN|IO_PRI|IO_ERR|IO_HUP|IO_NVAL)));
85         }
86         return *_ios;
87 }
88
89 void
90 CrossThreadChannel::drop_ios ()
91 {
92         delete _ios;
93         _ios = 0;
94 }
95
96 void
97 CrossThreadChannel::drain ()
98 {
99         drain (fds[0]);
100 }
101
102 void
103 CrossThreadChannel::drain (int fd)
104 {
105         /* drain selectable fd */
106         char buf[64];
107         while (::read (fd, buf, sizeof (buf)) > 0) {};
108 }
109
110 int
111 CrossThreadChannel::deliver (char msg)
112 {
113         return ::write (fds[1], &msg, 1);
114 }
115
116 int 
117 CrossThreadChannel::receive (char& msg)
118 {
119         return ::read (fds[0], &msg, 1);
120 }