]> git.lyx.org Git - lyx.git/blob - src/support/forkedcallqueue.C
support fully the LANGUAGE variable
[lyx.git] / src / support / forkedcallqueue.C
1 /**
2  * \file forkedcallqueue.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein (based on an idea from Angus Leeming)
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "forkedcallqueue.h"
14
15 #include "debug.h"
16
17 #include <boost/bind.hpp>
18 #include <boost/signals/signal2.hpp>
19
20
21 using std::string;
22 using std::endl;
23
24
25 namespace lyx {
26 namespace support {
27
28 ForkedCallQueue & ForkedCallQueue::get()
29 {
30         static ForkedCallQueue singleton;
31         return singleton;
32 }
33
34
35 Forkedcall::SignalTypePtr ForkedCallQueue::add(string const & process)
36 {
37         Forkedcall::SignalTypePtr ptr;
38         ptr.reset(new Forkedcall::SignalType);
39         callQueue_.push(Process(process, ptr));
40         if (!running_) {
41                 startCaller();
42         }
43         return ptr;
44 }
45
46
47 void ForkedCallQueue::callNext()
48 {
49         if (callQueue_.empty())
50                 return;
51         Process pro = callQueue_.front();
52         callQueue_.pop();
53         // Bind our chain caller
54         pro.second->connect(boost::bind(&ForkedCallQueue::callback,
55                                          this, _1, _2));
56         Forkedcall call;
57         // If we fail to fork the process, then emit the signal
58         // to tell the outside world that it failed.
59         if (call.startscript(pro.first, pro.second) > 0) {
60                 pro.second->operator()(0,1);
61         }
62 }
63
64
65 void ForkedCallQueue::callback(pid_t, int)
66 {
67         if(callQueue_.empty()) {
68                 stopCaller();
69         } else {
70                 callNext();
71         }
72 }
73
74 ForkedCallQueue::ForkedCallQueue() : running_(false)
75 {}
76
77
78 void ForkedCallQueue::startCaller()
79 {
80         lyxerr[Debug::GRAPHICS] << "ForkedCallQueue: waking up" << endl;
81         running_ = true ;
82         callNext();
83 }
84
85
86 void ForkedCallQueue::stopCaller()
87 {
88         running_ = false ;
89         lyxerr[Debug::GRAPHICS] << "ForkedCallQueue: I'm going to sleep"
90                                 << endl;
91 }
92
93
94 bool ForkedCallQueue::running() const
95 {
96         return running_ ;
97 }
98
99 } // namespace support
100 } // namespace lyx