fix gcc4.4 compile warning
[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 ()
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 (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
45                 error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
46                 return;
47         }
48
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;
52         }
53 }
54
55 CrossThreadChannel::~CrossThreadChannel ()
56 {
57         /* glibmm hack */
58         drop_ios ();
59
60         if (fds[0] >= 0) {
61                 close (fds[0]);
62                 fds[0] = -1;
63         } 
64
65         if (fds[1] >= 0) {
66                 close (fds[1]);
67                 fds[1] = -1;
68         } 
69 }
70
71 void
72 CrossThreadChannel::wakeup ()
73 {
74         char c = 0;
75         (void) ::write (fds[1], &c, 1);
76 }
77
78 RefPtr<IOSource>
79 CrossThreadChannel::ios () 
80 {
81         if (!_ios) {
82                 _ios = new RefPtr<IOSource> (IOSource::create (fds[0], IOCondition(IO_IN|IO_PRI|IO_ERR|IO_HUP|IO_NVAL)));
83         }
84         return *_ios;
85 }
86
87 void
88 CrossThreadChannel::drop_ios ()
89 {
90         delete _ios;
91         _ios = 0;
92 }
93
94 void
95 CrossThreadChannel::drain ()
96 {
97         drain (fds[0]);
98 }
99
100 void
101 CrossThreadChannel::drain (int fd)
102 {
103         /* drain selectable fd */
104         char buf[64];
105         while (::read (fd, buf, sizeof (buf)) > 0);
106 }
107
108 int
109 CrossThreadChannel::deliver (char msg)
110 {
111         return ::write (fds[1], &msg, 1);
112 }
113
114 int 
115 CrossThreadChannel::receive (char& msg)
116 {
117         return ::read (fds[0], &msg, 1);
118 }