]> git.lyx.org Git - lyx.git/blob - src/support/forkedcallqueue.C
Make lyx2lyx output the new external inset format.
[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 using std::endl;
21 using std::queue;
22
23 ForkedCallQueue & ForkedCallQueue::get()
24 {
25         static ForkedCallQueue singleton;
26         return singleton;
27 }
28
29
30 Forkedcall::SignalTypePtr ForkedCallQueue::add(string const & process)
31 {
32         Forkedcall::SignalTypePtr ptr;
33         ptr.reset(new Forkedcall::SignalType);
34         callQueue_.push(Process(process, ptr));
35         if (!running_) {
36                 startCaller();
37         }
38         return ptr;
39 }
40
41
42 void ForkedCallQueue::callNext()
43 {
44         if (callQueue_.empty())
45                 return;
46         Process pro = callQueue_.front();
47         callQueue_.pop();
48         // Bind our chain caller
49         pro.second->connect(boost::bind(&ForkedCallQueue::callback,
50                                          this, _1, _2));
51         Forkedcall call;
52         // If we fail to fork the process, then emit the signal
53         // to tell the outside world that it failed.
54         if (call.startscript(pro.first, pro.second) > 0) {
55                 pro.second->operator()(0,1);
56         }
57 }
58
59
60 void ForkedCallQueue::callback(pid_t, int)
61 {
62         if(callQueue_.empty()) {
63                 stopCaller();
64         } else {
65                 callNext();
66         }
67 }
68
69 ForkedCallQueue::ForkedCallQueue() : running_(false)
70 {}
71
72
73 void ForkedCallQueue::startCaller()
74 {
75         lyxerr[Debug::GRAPHICS] << "ForkedCallQueue: waking up" << endl;
76         running_ = true ;
77         callNext();
78 }
79
80
81 void ForkedCallQueue::stopCaller()
82 {
83         running_ = false ;
84         lyxerr[Debug::GRAPHICS] << "ForkedCallQueue: I'm going to sleep"
85                                 << endl;
86 }
87
88
89 bool ForkedCallQueue::running() const
90 {
91         return running_ ;
92 }