rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / glibmm2 / glib / glibmm / dispatcher.h
1 // -*- c++ -*-
2 #ifndef _GLIBMM_DISPATCHER_H
3 #define _GLIBMM_DISPATCHER_H
4
5 /* $Id: dispatcher.h 370 2007-01-20 10:53:28Z daniel $ */
6
7 /* Copyright 2002 The gtkmm Development Team
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the Free
21  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <sigc++/sigc++.h>
25 #include <glibmm/main.h>
26
27 namespace Glib
28 {
29
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 class DispatchNotifier;
32 #endif
33
34 /** Signal class for inter-thread communication.
35  * @ingroup Threads
36  * Glib::Dispatcher works similar to sigc::signal<void>.  But unlike normal
37  * signals, the notification happens asynchronously through a pipe.  This is
38  * a simple and efficient way of communicating between threads, and especially
39  * useful in a thread model with a single GUI thread.
40  *
41  * No mutex locking is involved, apart from the operating system's internal
42  * I/O locking.  That implies some usage rules:
43  *
44  * @li Only one thread may connect to the signal and receive notification, but
45  * multiple senders are allowed even without locking.
46  * @li The GLib main loop must run in the receiving thread (this will be the
47  * GUI thread usually).
48  * @li The Dispatcher object must be instantiated by the receiver thread.
49  * @li The Dispatcher object should be instantiated before creating any of the
50  * sender threads, if you want to avoid extra locking.
51  *
52  * Notes about performance:
53  *
54  * @li After instantiation, Glib::Dispatcher will never lock any mutexes on its
55  * own.  The interaction with the GLib main loop might involve locking on the
56  * @em receiver side.  The @em sender side, however, is guaranteed not to lock,
57  * except for internal locking in the <tt>%write()</tt> system call.
58  * @li All Dispatcher instances of a receiver thread share the same pipe.  That
59  * is, if you use Glib::Dispatcher only to notify the GUI thread, only one pipe
60  * is created no matter how many Dispatcher objects you have.
61  *
62  * Using Glib::Dispatcher on Windows:
63  *
64  * Glib::Dispatcher also works on win32-based systems.  Unfortunately though,
65  * the implementation cannot use a pipe on win32 and therefore does have to
66  * lock a mutex on emission, too.  However, the impact on performance is
67  * likely minor and the notification still happens asynchronously.  Apart
68  * from the additional lock the behavior matches the Unix implementation.
69  */
70 class Dispatcher
71 {
72 public:
73   /** Create new Dispatcher instance using the default main context.
74    * @throw Glib::FileError
75    */
76   Dispatcher();
77   
78   /** Create new Dispatcher instance using an arbitrary main context.
79    * @throw Glib::FileError
80    */
81   explicit Dispatcher(const Glib::RefPtr<MainContext>& context);
82   ~Dispatcher();
83
84   void emit();
85   void operator()();
86
87   sigc::connection connect(const sigc::slot<void>& slot);
88
89 private:
90   sigc::signal<void> signal_;
91   DispatchNotifier*   notifier_;
92
93   // noncopyable
94   Dispatcher(const Dispatcher&);
95   Dispatcher& operator=(const Dispatcher&);
96
97 #ifndef DOXYGEN_SHOULD_SKIP_THIS
98   friend class Glib::DispatchNotifier;
99 #endif
100 };
101
102 /*! A Glib::Dispatcher example.
103  * @example thread/dispatcher.cc
104  */
105
106 } // namespace Glib
107
108 #endif /* _GLIBMM_DISPATCHER_H */