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