Fix mysterious crashes such as #7049
[ardour.git] / libs / pbd / pbd / event_loop.h
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 #ifndef __pbd_event_loop_h__
21 #define __pbd_event_loop_h__
22
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <boost/function.hpp>
27 #include <boost/bind.hpp> /* we don't need this here, but anything calling call_slot() probably will, so this is convenient */
28 #include <stdint.h>
29 #include <pthread.h>
30 #include <glibmm/threads.h>
31
32 #include "pbd/libpbd_visibility.h"
33
34 namespace PBD
35 {
36
37 /** An EventLoop is as basic abstraction designed to be used with any "user
38  * interface" (not necessarily graphical) that needs to wait on
39  * events/requests and dispatch/process them as they arrive.
40  *
41  * This is a very basic class that doesn't by itself provide an actual
42  * event loop or thread. See BaseUI for the "real" object to be used
43  * when something like this is needed (it inherits from EventLoop).
44  */
45
46 class LIBPBD_API EventLoop
47 {
48   public:
49         EventLoop (std::string const&);
50         virtual ~EventLoop() {}
51
52         enum RequestType {
53                 range_guarantee = ~0
54         };
55
56         struct BaseRequestObject;
57
58         struct InvalidationRecord {
59             std::list<BaseRequestObject*> requests;
60             PBD::EventLoop* event_loop;
61             const char* file;
62             int line;
63
64             InvalidationRecord() : event_loop (0) {}
65         };
66
67         static void* invalidate_request (void* data);
68
69         struct BaseRequestObject {
70             RequestType             type;
71             bool                    valid;
72             InvalidationRecord*     invalidation;
73             boost::function<void()> the_slot;
74
75             BaseRequestObject() : valid (true), invalidation (0) {}
76         };
77
78         virtual void call_slot (InvalidationRecord*, const boost::function<void()>&) = 0;
79         virtual Glib::Threads::Mutex& slot_invalidation_mutex() = 0;
80
81         std::string event_loop_name() const { return _name; }
82
83         static EventLoop* get_event_loop_for_thread();
84         static void set_event_loop_for_thread (EventLoop* ui);
85
86         struct ThreadBufferMapping {
87                 pthread_t emitting_thread;
88                 std::string target_thread_name;
89                 void* request_buffer;
90         };
91
92         static std::vector<ThreadBufferMapping> get_request_buffers_for_target_thread (const std::string&);
93
94         static void register_request_buffer_factory (const std::string& target_thread_name, void* (*factory) (uint32_t));
95         static void pre_register (const std::string& emitting_thread_name, uint32_t num_requests);
96         static void remove_request_buffer_from_map (void* ptr);
97
98   private:
99         static Glib::Threads::Private<EventLoop> thread_event_loop;
100         std::string _name;
101
102         typedef std::map<std::string,ThreadBufferMapping> ThreadRequestBufferList;
103         static ThreadRequestBufferList thread_buffer_requests;
104         static Glib::Threads::RWLock   thread_buffer_requests_lock;
105
106         struct RequestBufferSupplier {
107
108                 /* @param name : name of object/entity that will/may accept
109                    requests from other threads, via a request buffer.
110                 */
111                 std::string name;
112
113                 /* @param factory : a function that can be called (with an
114                    argument specifying the @param number_of_requests) to create and
115                    return a request buffer for communicating with @param name)
116                 */
117                 void* (*factory)(uint32_t nunber_of_requests);
118         };
119         typedef std::vector<RequestBufferSupplier> RequestBufferSuppliers;
120         static RequestBufferSuppliers request_buffer_suppliers;
121 };
122
123 }
124
125 #define MISSING_INVALIDATOR 0 // used to mark places where we fail to provide an invalidator
126
127 #endif /* __pbd_event_loop_h__ */