97f19e1fe5d66d2c317241aee17e4a6c88270022
[ardour.git] / libs / pbd / pbd / abstract_ui.cc
1 #include <unistd.h>
2
3 #include <pbd/abstract_ui.h>
4 #include <pbd/pthread_utils.h>
5 #include <pbd/failed_constructor.h>
6
7 #include "i18n.h"
8
9 using namespace std;
10
11 template <typename RequestObject>
12 AbstractUI<RequestObject>::AbstractUI (string name, bool with_signal_pipes)
13         : BaseUI (name, with_signal_pipes)
14 {
15         if (pthread_key_create (&thread_request_buffer_key, 0)) {
16                 cerr << _("cannot create thread request buffer key") << endl;
17                 throw failed_constructor();
18         }
19
20         PBD::ThreadCreated.connect (mem_fun (*this, &AbstractUI<RequestObject>::register_thread));
21         PBD::ThreadCreatedWithRequestSize.connect (mem_fun (*this, &AbstractUI<RequestObject>::register_thread_with_request_count));
22 }
23
24 template <typename RequestObject> void
25 AbstractUI<RequestObject>::register_thread (pthread_t thread_id, string name)
26 {
27         register_thread_with_request_count (thread_id, name, 256);
28 }
29
30 template <typename RequestObject> void
31 AbstractUI<RequestObject>::register_thread_with_request_count (pthread_t thread_id, string thread_name, uint32_t num_requests)
32 {
33         RequestBuffer* b = new RequestBuffer (num_requests);
34
35         {
36         Glib::Mutex::Lock lm (request_buffer_map_lock);
37                 request_buffers[thread_id] = b;
38         }
39
40         pthread_setspecific (thread_request_buffer_key, b);
41 }
42
43 template <typename RequestObject> RequestObject*
44 AbstractUI<RequestObject>::get_request (RequestType rt)
45 {
46         RequestBuffer* rbuf = static_cast<RequestBuffer*>(pthread_getspecific (thread_request_buffer_key));
47         
48         if (rbuf == 0) {
49                 /* Cannot happen, but if it does we can't use the error reporting mechanism */
50                 cerr << _("programming error: ")
51                      << string_compose ("no %1-UI request buffer found for thread %2", name(), pthread_name())
52                      << endl;
53                 abort ();
54         }
55         
56         RequestBufferVector vec;
57         vec.buf[0] = 0;
58         vec.buf[1] = 0;
59         
60         rbuf->get_write_vector (&vec);
61
62         if (vec.len[0] == 0) {
63                 if (vec.len[1] == 0) {
64                         cerr << string_compose ("no space in %1-UI request buffer for thread %2", name(), pthread_name())
65                              << endl;
66                         return 0;
67                 } else {
68                         vec.buf[1]->type = rt;
69                         return vec.buf[1];
70                 }
71         } else {
72                 vec.buf[0]->type = rt;
73                 return vec.buf[0];
74         }
75 }
76
77 template <typename RequestObject> void
78 AbstractUI<RequestObject>::handle_ui_requests ()
79 {
80         RequestBufferMapIterator i;
81
82         request_buffer_map_lock.lock ();
83
84         for (i = request_buffers.begin(); i != request_buffers.end(); ++i) {
85
86                 RequestBufferVector vec;
87
88                 while (true) {
89
90                         /* we must process requests 1 by 1 because
91                            the request may run a recursive main
92                            event loop that will itself call
93                            handle_ui_requests. when we return
94                            from the request handler, we cannot
95                            expect that the state of queued requests
96                            is even remotely consistent with
97                            the condition before we called it.
98                         */
99
100                         i->second->get_read_vector (&vec);
101
102                         if (vec.len[0] == 0) {
103                                 break;
104                         } else {
105                                 request_buffer_map_lock.unlock ();
106                                 do_request (vec.buf[0]);
107                                 request_buffer_map_lock.lock ();
108                                 i->second->increment_read_ptr (1);
109                         } 
110                 }
111         }
112
113         request_buffer_map_lock.unlock ();
114 }
115
116 template <typename RequestObject> void
117 AbstractUI<RequestObject>::send_request (RequestObject *req)
118 {
119         if (base_instance() == 0) {
120                 return; /* XXX is this the right thing to do ? */
121         }
122         
123         if (caller_is_ui_thread()) {
124                 // cerr << "GUI thread sent request " << req << " type = " << req->type << endl;
125                 do_request (req);
126         } else {        
127                 RequestBuffer* rbuf = static_cast<RequestBuffer*> (pthread_getspecific (thread_request_buffer_key));
128
129                 if (rbuf == 0) {
130                         /* can't use the error system to report this, because this
131                            thread isn't registered!
132                         */
133                         cerr << _("programming error: ")
134                              << string_compose ("AbstractUI::send_request() called from %1 (%2), but no request buffer exists for that thread", name(), pthread_name())
135                              << endl;
136                         abort ();
137                 }
138                 
139                 // cerr << "thread " << pthread_self() << " sent request " << req << " type = " << req->type << endl;
140
141                 rbuf->increment_write_ptr (1);
142
143                 if (signal_pipe[1] >= 0) {
144                         const char c = 0;
145                         write (signal_pipe[1], &c, 1);
146                 }
147         }
148 }
149