OSDN Git Service

Fixed a bug in AbstractTool class that could cause a severe slow-down on process...
[lamexp/LameXP.git] / src / Tool_Abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Tool_Abstract.h"
24
25 //Internal
26 #include "Global.h"
27 #include "JobObject.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
32
33 //Qt
34 #include <QProcess>
35 #include <QMutex>
36 #include <QMutexLocker>
37 #include <QLibrary>
38 #include <QProcessEnvironment>
39 #include <QDir>
40
41 /*
42  * Job Object
43  */
44 QScopedPointer<JobObject> AbstractTool::s_jobObject;
45 QMutex                    AbstractTool::s_jobObjMtx;
46
47 /*
48  * Process Timer
49  */
50 quint64 AbstractTool::s_startProcessTimer = 0ui64;
51 QMutex  AbstractTool::s_startProcessMutex;
52
53 /*
54  * Const
55  */
56 static const unsigned int START_DELAY = 50U;                                                            //in milliseconds
57 static const quint64 START_DELAY_NANO = quint64(START_DELAY) * 10000ui64;       //in 100-nanosecond intervals
58
59 /*
60  * Constructor
61  */
62 AbstractTool::AbstractTool(void)
63 {
64         QMutexLocker lock(&s_jobObjMtx);
65
66         if(s_jobObject.isNull())
67         {
68                 s_jobObject.reset(new JobObject());
69         }
70
71         m_firstLaunch = true;
72 }
73
74 /*
75  * Destructor
76  */
77 AbstractTool::~AbstractTool(void)
78 {
79 }
80
81 /*
82  * Initialize and launch process object
83  */
84 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
85 {
86         QMutexLocker lock(&s_startProcessMutex);
87
88         while(MUtils::OS::current_file_time() <= s_startProcessTimer)
89         {
90                 lock.unlock();
91                 MUtils::OS::sleep_ms(START_DELAY);
92                 lock.relock();
93         }
94
95         emit messageLogged(commandline2string(program, args) + "\n");
96         MUtils::init_process(process, QFileInfo(program).absolutePath());
97
98         process.start(program, args);
99         
100         if(process.waitForStarted())
101         {
102                 if(s_jobObject)
103                 {
104                         if(!s_jobObject->addProcessToJob(&process))
105                         {
106                                 qWarning("Failed to assign process to job object!");
107                         }
108                 }
109
110                 MUtils::OS::change_process_priority(&process, -1);
111                 
112                 if(m_firstLaunch)
113                 {
114                         emit statusUpdated(0);
115                         m_firstLaunch = false;
116                 }
117                 
118                 s_startProcessTimer = MUtils::OS::current_file_time() + START_DELAY_NANO;
119                 return true;
120         }
121
122         emit messageLogged("Process creation has failed :-(");
123         QString errorMsg= process.errorString().trimmed();
124         if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
125
126         process.kill();
127         process.waitForFinished(-1);
128
129         s_startProcessTimer = MUtils::OS::current_file_time() + START_DELAY_NANO;
130         return false;
131 }
132
133 /*
134  * Convert program arguments to single string
135  */
136 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
137 {
138         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
139         
140         for(int i = 0; i < arguments.count(); i++)
141         {
142                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
143         }
144
145         return commandline;
146 }
147
148