diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2014-12-03 20:47:59 -0500 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2014-12-03 20:47:59 -0500 |
commit | f72b87b44841b38f1046619b1a607b6a525a8d25 (patch) | |
tree | 6ba845c16d3ad1b3f6f8987c7c14aea61bf6979a /libs/pbd/crossthread.posix.cc | |
parent | 5c51b302cec5942a37f468903c4183b0a6c50695 (diff) |
new platform specific crossthreadchannel files
Diffstat (limited to 'libs/pbd/crossthread.posix.cc')
-rw-r--r-- | libs/pbd/crossthread.posix.cc | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/libs/pbd/crossthread.posix.cc b/libs/pbd/crossthread.posix.cc new file mode 100644 index 0000000000..44205e680d --- /dev/null +++ b/libs/pbd/crossthread.posix.cc @@ -0,0 +1,68 @@ +CrossThreadChannel::CrossThreadChannel (bool non_blocking) + : receive_channel (0) +{ + fds[0] = -1; + fds[1] = -1; + + if (pipe (fds)) { + error << "cannot create x-thread pipe for read (%2)" << ::strerror (errno) << endmsg; + return; + } + + if (non_blocking) { + if (fcntl (fds[0], F_SETFL, O_NONBLOCK)) { + error << "cannot set non-blocking mode for x-thread pipe (read) (" << ::strerror (errno) << ')' << endmsg; + return; + } + + if (fcntl (fds[1], F_SETFL, O_NONBLOCK)) { + error << "cannot set non-blocking mode for x-thread pipe (write) (%2)" << ::strerror (errno) << ')' << endmsg; + return; + } + } + + receive_channel = g_io_channel_unix_new (fds[0]); +} + +CrossThreadChannel::~CrossThreadChannel () +{ + if (receive_channel) { + g_io_channel_unref (receive_channel); + } + + if (fds[0] >= 0) { + close (fds[0]); + fds[0] = -1; + } + + if (fds[1] >= 0) { + close (fds[1]); + fds[1] = -1; + } +} + +void +CrossThreadChannel::wakeup () +{ + char c = 0; + (void) ::write (fds[1], &c, 1); +} + +void +CrossThreadChannel::drain () +{ + char buf[64]; + while (::read (fds[0], buf, sizeof (buf)) > 0) {}; +} + +int +CrossThreadChannel::deliver (char msg) +{ + return ::write (fds[1], &msg, 1); +} + +int +CrossThreadChannel::receive (char& msg) +{ + return ::read (fds[0], &msg, 1); +} |