fix up wscript/build issues in exportvis after merge with master
[ardour.git] / libs / pbd / pbd / crossthread.h
1 /*
2     Copyright (C) 2000-2007 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 #ifndef __pbd__crossthread_h__
21 #define __pbd__crossthread_h__
22
23 #ifdef check
24 #undef check
25 #endif
26
27 #include <glibmm/main.h>
28
29 #include "pbd/libpbd_visibility.h"
30
31 /** A simple abstraction of a mechanism of signalling one thread from another.
32  * The signaller calls ::wakeup() to tell the signalled thread to check for
33  * work to be done. 
34  *
35  * This implementation provides both ::selectable() for use in direct
36  * poll/select-based event loops, and a Glib::IOSource via ::ios() for use
37  * in Glib main loop based situations. 
38  */
39
40 class LIBPBD_API CrossThreadChannel { 
41   public:
42         /** if @a non_blocking is true, the channel will not cause blocking
43          * when used in an event loop based on poll/select or the glib main
44          * loop.
45          */
46         CrossThreadChannel(bool non_blocking);
47         ~CrossThreadChannel();
48         
49         /** Tell the listening thread that is has work to do.
50          */
51         void wakeup();
52         
53         /* if the listening thread cares about the precise message
54          * it is being sent, then ::deliver() can be used to send
55          * a single byte message rather than a simple wakeup. These
56          * two mechanisms should not be used on the same CrossThreadChannel
57          * because there is no way to know which byte value will be used
58          * for ::wakeup()
59          */
60         int deliver (char msg);
61
62         /** if using ::deliver() to wakeup the listening thread, then
63          * the listener should call ::receive() to fetch the message
64          * type from the channel.
65          */
66         int receive (char& msg);
67
68         /** empty the channel of all requests.
69          * Typically this is done as soon as input 
70          * is noticed on the channel, because the
71          * handler will look at a separately managed work
72          * queue. The actual number of queued "wakeups"
73          * in the channel will not be important.
74          */
75         void drain ();
76         static void drain (int fd);
77
78         /** File descriptor that can be used with poll/select to
79          * detect when wakeup() has been called on this channel.
80          * It be marked as readable/input-ready when this condition
81          * is true. It has already been marked non-blocking.
82          */
83         int selectable() const { return fds[0]; }
84
85         /* glibmm 2.22 and earlier has a terrifying bug that will
86            cause crashes whenever a Source is removed from
87            a MainContext (including the destruction of the MainContext),
88            because the Source is destroyed "out from under the nose of" 
89            the RefPtr. I (Paul) have fixed this (https://bugzilla.gnome.org/show_bug.cgi?id=561885)
90            but in the meantime, we need a hack to get around the issue.
91         */
92         Glib::RefPtr<Glib::IOSource> ios();
93         void drop_ios ();
94
95         /** returns true if the CrossThreadChannel was
96          * correctly constructed.
97          */
98         bool ok() const { return fds[0] >= 0 && fds[1] >= 0; }
99
100   private:
101         Glib::RefPtr<Glib::IOSource>* _ios; // lazily constructed
102         int fds[2]; // current implementation uses a pipe/fifo
103 };
104
105 #endif /* __pbd__crossthread_h__ */