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