fix Window->Common move for show-mixer
[ardour.git] / libs / pbd / crossthread.posix.cc
1 #include <poll.h>
2
3 CrossThreadChannel::CrossThreadChannel (bool non_blocking)
4         : receive_channel (0)
5         , receive_source (0)
6 {
7         fds[0] = -1;
8         fds[1] = -1;
9
10         if (pipe (fds)) {
11                 error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg;
12                 return;
13         }
14
15         if (non_blocking) {
16                 if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) {
17                         error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg;
18                         return;
19                 }
20
21                 if (fcntl (fds[1], F_SETFL, O_NONBLOCK)) {
22                         error << "cannot set non-blocking mode for x-thread pipe (write) (%2)" << ::strerror (errno) << ')' << endmsg;
23                         return;
24                 }
25         }
26
27         receive_channel = g_io_channel_unix_new (fds[0]);
28 }
29
30 CrossThreadChannel::~CrossThreadChannel ()
31 {
32         if (receive_source) {
33                 /* this disconnects it from any main context it was attached in
34                    in ::attach(), this prevent its callback from being invoked
35                    after the destructor has finished.
36                 */
37                 g_source_destroy (receive_source);
38         }
39
40         if (receive_channel) {
41                 g_io_channel_unref (receive_channel);
42                 receive_channel = 0;
43         }
44
45         if (fds[0] >= 0) {
46                 close (fds[0]);
47                 fds[0] = -1;
48         }
49
50         if (fds[1] >= 0) {
51                 close (fds[1]);
52                 fds[1] = -1;
53         }
54 }
55
56 void
57 CrossThreadChannel::wakeup ()
58 {
59         char c = 0;
60         (void) ::write (fds[1], &c, 1);
61 }
62
63 void
64 CrossThreadChannel::drain ()
65 {
66         char buf[64];
67         while (::read (fds[0], buf, sizeof (buf)) > 0) {};
68 }
69
70 int
71 CrossThreadChannel::deliver (char msg)
72 {
73         return ::write (fds[1], &msg, 1);
74 }
75
76 bool
77 CrossThreadChannel::poll_for_request()
78 {
79         struct pollfd pfd[1];
80         pfd[0].fd = fds[0];
81         pfd[0].events = POLLIN|POLLERR|POLLHUP;
82         while(true) {
83                 if (poll (pfd, 1, -1) < 0) {
84                         if (errno == EINTR) {
85                                 continue;
86                         }
87                         break;
88                 }
89                 if (pfd[0].revents & ~POLLIN) {
90                         break;
91                 }
92
93                 if (pfd[0].revents & POLLIN) {
94                         return true;
95                 }
96         }
97         return false;
98 }
99
100 int
101 CrossThreadChannel::receive (char& msg, bool wait)
102 {
103         if (wait) {
104                 if (!poll_for_request ()) {
105                         return -1;
106                 }
107         }
108         return ::read (fds[0], &msg, 1);
109 }