]> git.lyx.org Git - lyx.git/blob - src/support/forkedcontr.h
* lyxfunctional.h: delete compare_memfun and helper classes
[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 <boost/shared_ptr.hpp>
20 #include <csignal>
21 //#include <sys/types.h> // needed for pid_t
22 #include <list>
23 #include <vector>
24
25 namespace lyx {
26 namespace support {
27
28 class ForkedProcess;
29
30 class ForkedcallsController {
31 public:
32         /// Get hold of the only controller that can exist inside the process.
33         static ForkedcallsController & get();
34
35         /// Are there any completed child processes to be cleaned-up after?
36         bool processesCompleted() const { return current_child != -1; }
37
38         /** Those child processes that are found to have finished are removed
39          *  from the list and their callback function is passed the final
40          *  return state.
41          */
42         void handleCompletedProcesses();
43
44         /// Add a new child process to the list of controlled processes.
45         void addCall(ForkedProcess const &);
46
47         /** Kill this process prematurely and remove it from the list.
48          *  The process is killed within tolerance secs.
49          *  See forkedcall.[Ch] for details.
50          */
51         void kill(pid_t, int tolerance = 5);
52
53         struct Data {
54                 Data() : pid(0), status(0) {}
55                 pid_t pid;
56                 int status;
57         };
58
59         /** These data are used by the SIGCHLD handler to populate a list
60          *  of child processes that have completed and been reaped.
61          *  The associated signals are then emitted within the main LyX
62          *  event loop.
63          */
64         std::vector<Data> reaped_children;
65         sig_atomic_t current_child;
66
67 private:
68         ForkedcallsController();
69         ForkedcallsController(ForkedcallsController const &);
70         ~ForkedcallsController();
71
72         typedef boost::shared_ptr<ForkedProcess> ForkedProcessPtr;
73         typedef std::list<ForkedProcessPtr> ListType;
74         typedef ListType::iterator iterator;
75
76         iterator find_pid(pid_t);
77
78         /// The child processes
79         ListType forkedCalls;
80
81         /// Used to block SIGCHLD signals.
82         sigset_t newMask, oldMask;
83 };
84
85 } // namespace support
86 } // namespace lyx
87
88 #endif // FORKEDCONTR_H