cosmetics

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21864 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2007-11-29 19:56:25 +00:00
parent 1aafa6ff77
commit 0ef0cbbf1a
5 changed files with 84 additions and 130 deletions

View File

@ -79,7 +79,6 @@ using std::string;
namespace lyx { namespace lyx {
using support::FileName; using support::FileName;
using support::ForkedCallsController;
/// return the LyX mouse button state from Qt's /// return the LyX mouse button state from Qt's
@ -458,8 +457,7 @@ void GuiWorkArea::toggleCursor()
// Use this opportunity to deal with any child processes that // Use this opportunity to deal with any child processes that
// have finished but are waiting to communicate this fact // have finished but are waiting to communicate this fact
// to the rest of LyX. // to the rest of LyX.
ForkedCallsController & fcc = ForkedCallsController::get(); support::ForkedCallsController::handleCompletedProcesses();
fcc.handleCompletedProcesses();
} }

View File

@ -34,7 +34,6 @@ using support::addExtension;
using support::changeExtension; using support::changeExtension;
using support::FileName; using support::FileName;
using support::ForkedCall; using support::ForkedCall;
using support::ForkedCallQueue;
using support::getExtension; using support::getExtension;
using support::libScriptSearch; using support::libScriptSearch;
using support::onlyPath; using support::onlyPath;
@ -193,9 +192,8 @@ void Converter::Impl::startConversion()
return; return;
} }
ForkedCall::SignalTypePtr ForkedCall::SignalTypePtr ptr =
ptr = ForkedCallQueue::get().add(script_command_); support::ForkedCallQueue::add(script_command_);
ptr->connect(boost::bind(&Impl::converted, this, _1, _2)); ptr->connect(boost::bind(&Impl::converted, this, _1, _2));
} }

View File

@ -383,7 +383,7 @@ InProgress::InProgress(string const & filename_base,
void InProgress::stop() const void InProgress::stop() const
{ {
if (pid) if (pid)
lyx::support::ForkedCallsController::get().kill(pid, 0); lyx::support::ForkedCallsController::kill(pid, 0);
if (!metrics_file.empty()) if (!metrics_file.empty())
metrics_file.removeFile(); metrics_file.removeFile();
@ -421,9 +421,8 @@ PreviewLoader::Impl::~Impl()
InProgressProcesses::iterator ipit = in_progress_.begin(); InProgressProcesses::iterator ipit = in_progress_.begin();
InProgressProcesses::iterator ipend = in_progress_.end(); InProgressProcesses::iterator ipend = in_progress_.end();
for (; ipit != ipend; ++ipit) { for (; ipit != ipend; ++ipit)
ipit->second.stop(); ipit->second.stop();
}
} }
@ -437,7 +436,7 @@ PreviewLoader::Impl::preview(string const & latex_snippet) const
namespace { namespace {
class FindSnippet : public std::unary_function<InProgressProcess, bool> { class FindSnippet {
public: public:
FindSnippet(string const & s) : snippet_(s) {} FindSnippet(string const & s) : snippet_(s) {}
bool operator()(InProgressProcess const & process) const bool operator()(InProgressProcess const & process) const

View File

@ -23,9 +23,11 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <vector>
#include <cerrno> #include <cerrno>
#include <queue>
#include <sstream> #include <sstream>
#include <utility>
#include <vector>
#ifdef _WIN32 #ifdef _WIN32
# define SIGHUP 1 # define SIGHUP 1
@ -145,8 +147,7 @@ int ForkedProcess::run(Starttype type)
break; break;
case DontWait: { case DontWait: {
// Integrate into the Controller // Integrate into the Controller
ForkedCallsController & contr = ForkedCallsController::get(); ForkedCallsController::addCall(*this);
contr.addCall(*this);
break; break;
} }
} }
@ -399,15 +400,31 @@ int ForkedCall::generateChild()
// //
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
namespace ForkedCallQueue {
ForkedCallQueue & ForkedCallQueue::get() /// A process in the queue
{ typedef std::pair<std::string, ForkedCall::SignalTypePtr> Process;
static ForkedCallQueue singleton; /** Add a process to the queue. Processes are forked sequentially
return singleton; * only one is running at a time.
} * Connect to the returned signal and you'll be informed when
* the process has ended.
*/
ForkedCall::SignalTypePtr add(std::string const & process);
/// in-progress queue
static std::queue<Process> callQueue_;
ForkedCall::SignalTypePtr ForkedCallQueue::add(string const & process) /// flag whether queue is running
static bool running_ = 0;
///
void startCaller();
///
void stopCaller();
///
void callback(pid_t, int);
ForkedCall::SignalTypePtr add(string const & process)
{ {
ForkedCall::SignalTypePtr ptr; ForkedCall::SignalTypePtr ptr;
ptr.reset(new ForkedCall::SignalType); ptr.reset(new ForkedCall::SignalType);
@ -418,15 +435,14 @@ ForkedCall::SignalTypePtr ForkedCallQueue::add(string const & process)
} }
void ForkedCallQueue::callNext() void callNext()
{ {
if (callQueue_.empty()) if (callQueue_.empty())
return; return;
Process pro = callQueue_.front(); Process pro = callQueue_.front();
callQueue_.pop(); callQueue_.pop();
// Bind our chain caller // Bind our chain caller
pro.second->connect(boost::bind(&ForkedCallQueue::callback, pro.second->connect(boost::bind(&ForkedCallQueue::callback, _1, _2));
this, _1, _2));
ForkedCall call; ForkedCall call;
// If we fail to fork the process, then emit the signal // If we fail to fork the process, then emit the signal
// to tell the outside world that it failed. // to tell the outside world that it failed.
@ -435,7 +451,7 @@ void ForkedCallQueue::callNext()
} }
void ForkedCallQueue::callback(pid_t, int) void callback(pid_t, int)
{ {
if (callQueue_.empty()) if (callQueue_.empty())
stopCaller(); stopCaller();
@ -444,12 +460,7 @@ void ForkedCallQueue::callback(pid_t, int)
} }
ForkedCallQueue::ForkedCallQueue() void startCaller()
: running_(false)
{}
void ForkedCallQueue::startCaller()
{ {
LYXERR(Debug::GRAPHICS, "ForkedCallQueue: waking up"); LYXERR(Debug::GRAPHICS, "ForkedCallQueue: waking up");
running_ = true ; running_ = true ;
@ -457,18 +468,21 @@ void ForkedCallQueue::startCaller()
} }
void ForkedCallQueue::stopCaller() void stopCaller()
{ {
running_ = false ; running_ = false ;
LYXERR(Debug::GRAPHICS, "ForkedCallQueue: I'm going to sleep"); LYXERR(Debug::GRAPHICS, "ForkedCallQueue: I'm going to sleep");
} }
bool ForkedCallQueue::running() const bool running()
{ {
return running_ ; return running_;
} }
} // namespace ForkedCallsQueue
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// //
@ -504,33 +518,33 @@ string const getChildErrorMessage()
#endif #endif
// Ensure, that only one controller exists inside process namespace ForkedCallsController {
ForkedCallsController & ForkedCallsController::get()
typedef boost::shared_ptr<ForkedProcess> ForkedProcessPtr;
typedef std::list<ForkedProcessPtr> ListType;
typedef ListType::iterator iterator;
/// The child processes
static ListType forkedCalls;
iterator find_pid(pid_t pid)
{ {
static ForkedCallsController singleton; return find_if(forkedCalls.begin(), forkedCalls.end(),
return singleton; bind(equal_to<pid_t>(),
bind(&ForkedCall::pid, _1),
pid));
} }
ForkedCallsController::ForkedCallsController() void addCall(ForkedProcess const & newcall)
{}
// open question: should we stop childs here?
// Asger says no: I like to have my xdvi open after closing LyX. Maybe
// I want to print or something.
ForkedCallsController::~ForkedCallsController()
{}
void ForkedCallsController::addCall(ForkedProcess const & newcall)
{ {
forkedCalls.push_back(newcall.clone()); forkedCalls.push_back(newcall.clone());
} }
// Check the list of dead children and emit any associated signals. // Check the list of dead children and emit any associated signals.
void ForkedCallsController::handleCompletedProcesses() void handleCompletedProcesses()
{ {
ListType::iterator it = forkedCalls.begin(); ListType::iterator it = forkedCalls.begin();
ListType::iterator end = forkedCalls.end(); ListType::iterator end = forkedCalls.end();
@ -624,18 +638,9 @@ void ForkedCallsController::handleCompletedProcesses()
} }
ForkedCallsController::iterator ForkedCallsController::find_pid(pid_t pid)
{
return find_if(forkedCalls.begin(), forkedCalls.end(),
bind(equal_to<pid_t>(),
bind(&ForkedCall::pid, _1),
pid));
}
// Kill the process prematurely and remove it from the list // Kill the process prematurely and remove it from the list
// within tolerance secs // within tolerance secs
void ForkedCallsController::kill(pid_t pid, int tolerance) void kill(pid_t pid, int tolerance)
{ {
ListType::iterator it = find_pid(pid); ListType::iterator it = find_pid(pid);
if (it == forkedCalls.end()) if (it == forkedCalls.end())
@ -645,5 +650,7 @@ void ForkedCallsController::kill(pid_t pid, int tolerance)
forkedCalls.erase(it); forkedCalls.erase(it);
} }
} // namespace ForkedCallsController
} // namespace support } // namespace support
} // namespace lyx } // namespace lyx

View File

@ -21,12 +21,7 @@
# include <sys/types.h> # include <sys/types.h>
#endif #endif
#include <list>
#include <queue>
#include <string> #include <string>
#include <utility>
#include <vector>
namespace lyx { namespace lyx {
namespace support { namespace support {
@ -172,87 +167,44 @@ private:
/** /**
* This class implements a queue of forked processes. In order not to * This interfaces a queue of forked processes. In order not to
* hose the system with multiple processes running simultaneously, you can * hose the system with multiple processes running simultaneously, you can
* request the addition of your process to this queue and it will be * request the addition of your process to this queue and it will be
* executed when its turn comes. * executed when its turn comes.
* *
*/ */
class ForkedCallQueue { namespace ForkedCallQueue {
public:
/// A process in the queue
typedef std::pair<std::string, ForkedCall::SignalTypePtr> Process;
/** Add a process to the queue. Processes are forked sequentially
* only one is running at a time.
* Connect to the returned signal and you'll be informed when
* the process has ended.
*/
ForkedCall::SignalTypePtr add(std::string const & process);
/// Query whether the queue is running a forked process now.
bool running() const;
/// Get the and only instance of the class
static ForkedCallQueue & get();
private: ForkedCall::SignalTypePtr add(std::string const & process);
/** this class is a singleton class... use /// Query whether the queue is running a forked process now.
* ForkedCallQueue::get() instead bool running();
*/
ForkedCallQueue(); }
/// in-progress queue
std::queue<Process> callQueue_;
///
bool running_;
///
void callNext();
///
void startCaller();
///
void stopCaller();
///
void callback(pid_t, int);
};
/** /**
* A class for the control of child processes launched using * Control of child processes launched using fork() and execvp().
* fork() and execvp().
*/ */
class ForkedCallsController { namespace ForkedCallsController {
public:
/// Get hold of the only controller that can exist inside the process.
static ForkedCallsController & get();
/// Add a new child process to the list of controlled processes. /// Add a new child process to the list of controlled processes.
void addCall(ForkedProcess const &); void addCall(ForkedProcess const &);
/** Those child processes that are found to have finished are removed /** Those child processes that are found to have finished are removed
* from the list and their callback function is passed the final * from the list and their callback function is passed the final
* return state. * return state.
*/ */
void handleCompletedProcesses(); void handleCompletedProcesses();
/** Kill this process prematurely and remove it from the list. /** Kill this process prematurely and remove it from the list.
* The process is killed within tolerance secs. * The process is killed within tolerance secs.
* See forkedcall.[Ch] for details. * See forkedcall.[Ch] for details.
*/ */
void kill(pid_t, int tolerance = 5); void kill(pid_t, int tolerance = 5);
private: } // namespace ForkedCallsController
ForkedCallsController();
ForkedCallsController(ForkedCallsController const &);
~ForkedCallsController();
typedef boost::shared_ptr<ForkedProcess> ForkedProcessPtr;
typedef std::list<ForkedProcessPtr> ListType;
typedef ListType::iterator iterator;
iterator find_pid(pid_t);
/// The child processes
ListType forkedCalls;
};
#if defined(_WIN32) #if defined(_WIN32)