Add a trash pool for invalidation requests.
[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                 gint                    _valid;
72                 InvalidationRecord*     invalidation;
73                 boost::function<void()> the_slot;
74
75                 BaseRequestObject() : _valid (0), invalidation (0) {}
76
77                 void validate () { g_atomic_int_set (&_valid, 1); }
78                 void invalidate () { g_atomic_int_set (&_valid, 0); }
79                 bool valid () { return g_atomic_int_get (&_valid) == 1; }
80         };
81
82         virtual void call_slot (InvalidationRecord*, const boost::function<void()>&) = 0;
83         virtual Glib::Threads::Mutex& slot_invalidation_mutex() = 0;
84
85         std::string event_loop_name() const { return _name; }
86
87         static EventLoop* get_event_loop_for_thread();
88         static void set_event_loop_for_thread (EventLoop* ui);
89
90         struct ThreadBufferMapping {
91                 pthread_t emitting_thread;
92                 std::string target_thread_name;
93                 void* request_buffer;
94         };
95
96         static std::vector<ThreadBufferMapping> get_request_buffers_for_target_thread (const std::string&);
97
98         static void register_request_buffer_factory (const std::string& target_thread_name, void* (*factory) (uint32_t));
99         static void pre_register (const std::string& emitting_thread_name, uint32_t num_requests);
100         static void remove_request_buffer_from_map (void* ptr);
101
102         std::list<InvalidationRecord*> trash;
103
104 private:
105         static Glib::Threads::Private<EventLoop> thread_event_loop;
106         std::string _name;
107
108         typedef std::map<std::string,ThreadBufferMapping> ThreadRequestBufferList;
109         static ThreadRequestBufferList thread_buffer_requests;
110         static Glib::Threads::RWLock   thread_buffer_requests_lock;
111
112         struct RequestBufferSupplier {
113
114                 /* @param name : name of object/entity that will/may accept
115                  * requests from other threads, via a request buffer.
116                  */
117                 std::string name;
118
119                 /* @param factory : a function that can be called (with an
120                  * argument specifying the @param number_of_requests) to create and
121                  * return a request buffer for communicating with @param name)
122                  */
123                 void* (*factory)(uint32_t nunber_of_requests);
124         };
125         typedef std::vector<RequestBufferSupplier> RequestBufferSuppliers;
126         static RequestBufferSuppliers request_buffer_suppliers;
127 };
128
129 }
130
131 #define MISSING_INVALIDATOR 0 // used to mark places where we fail to provide an invalidator
132
133 #endif /* __pbd_event_loop_h__ */