]> git.lyx.org Git - lyx.git/blob - src/support/forkedcontr.h
introduce namespace lyx::support
[lyx.git] / src / support / forkedcontr.h
1 // -*- C++ -*-
2 /**
3  * \file forkedcontr.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup Nielsen
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS
11  *
12  * A class for the control of child processes launched using
13  * fork() and execvp().
14  */
15
16 #ifndef FORKEDCONTR_H
17 #define FORKEDCONTR_H
18
19 #include "LString.h"
20
21 #include <boost/signals/signal0.hpp>
22 #include <boost/signals/trackable.hpp>
23
24 #include <sys/types.h> // needed for pid_t
25
26 #include <list>
27 #include <vector>
28
29 class Timeout;
30
31 namespace lyx {
32 namespace support {
33
34 class ForkedProcess;
35
36 class ForkedcallsController : public boost::signals::trackable {
37 public:
38         /// We need this to avoid warnings.
39         ForkedcallsController();
40         /** This d-tor should really be private, but making it public
41          *   allows egcs 1.1 to compile the class.
42          */
43         ~ForkedcallsController();
44
45         /// Get hold of the only controller that can exist inside the process.
46         static ForkedcallsController & get();
47
48         /// Add a new child process to the list of controlled processes.
49         void addCall(ForkedProcess const &);
50
51         /** This method is connected to the timer. Every XX ms it is called
52          *  so that we can check on the status of the children. Those that
53          *  are found to have finished are removed from the list and their
54          *  callback function is passed the final return state.
55          */
56         void timer();
57
58         /// Return a vector of the pids of all the controlled processes.
59         std::vector<pid_t> const getPIDs() const;
60
61         /// Get the command string of the process.
62         string const getCommand(pid_t) const;
63
64         /** Kill this process prematurely and remove it from the list.
65          *  The process is killed within tolerance secs.
66          *  See forkedcall.[Ch] for details.
67          */
68         void kill(pid_t, int tolerance = 5);
69
70         /// Signal emitted when the list of current child processes changes.
71         boost::signal0<void> childrenChanged;
72
73 private:
74         ///
75         ForkedcallsController(ForkedcallsController const &);
76
77         /// The child processes
78         typedef std::list<ForkedProcess *> ListType;
79         ///
80         ListType forkedCalls;
81
82         /** The timer. Enables us to check the status of the children
83          *  every XX ms and to invoke a callback on completion.
84          */
85         Timeout * timeout_;
86 };
87
88 } // namespace support
89 } // namespace lyx
90
91 #endif // FORKEDCONTR_H