aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/SubProcess.cpp56
1 files changed, 48 insertions, 8 deletions
diff --git a/src/common/SubProcess.cpp b/src/common/SubProcess.cpp
index f2bcf919..d69fb973 100644
--- a/src/common/SubProcess.cpp
+++ b/src/common/SubProcess.cpp
@@ -11,16 +11,12 @@ SubProcessException::~SubProcessException() {}
PlatformSubProcessBase::PlatformSubProcessBase(
const PlatformSubProcessStartInfo& start_info)
- : dispose_(DisposeKind::None),
+ : auto_delete_(false),
start_info_(start_info),
data_lock_(data_mutex_, std::defer_lock),
process_lock_(process_mutex_, std::defer_lock) {}
-PlatformSubProcessBase::~PlatformSubProcessBase() {
- auto data_lock_guard = CreateDataLockGuard();
- if (status_ )
- switch (dispose_) { DisposeKind::Join: }
-}
+PlatformSubProcessBase::~PlatformSubProcessBase() {}
void PlatformSubProcessBase::Start() {
auto data_lock_guard = CreateDataLockGuard();
@@ -40,18 +36,62 @@ void PlatformSubProcessBase::Start() {
auto data_lock_guard = CreateDataLockGuard();
status_ = PlatformSubProcessStatus::Exited;
});
+
+ process_thread_.detach();
} catch (const std::exception& e) {
- status_ = PlatformSubProcessStatus::FailToStart;
+ status_ = PlatformSubProcessStatus::FailedToStart;
}
}
void PlatformSubProcessBase::Wait(
std::optional<std::chrono::milliseconds> wait_time) {
+ auto status = GetStatus();
+
+ if (status == PlatformSubProcessStatus::Prepare) {
+ throw SubProcessException(
+ u"The process does not start. Can't wait for it.");
+ }
+
+ if (status == PlatformSubProcessStatus::FailedToStart) {
+ throw SubProcessException(
+ u"The process failed to start. Can't wait for it.");
+ }
+
+ if (status == PlatformSubProcessStatus::Exited) {
+ return;
+ }
+
if (wait_time) {
- process_lock_.try_lock_for(*wait_time);
+ auto locked = process_lock_.try_lock_for(*wait_time);
+ if (locked) {
+ process_lock_.unlock();
+ }
} else {
process_lock_.lock();
+ process_lock_.unlock();
}
}
+void PlatformSubProcessBase::Kill() {
+ auto status = GetStatus();
+
+ if (status == PlatformSubProcessStatus::Prepare) {
+ throw SubProcessException(u"The process does not start. Can't kill it.");
+ }
+
+ if (status == PlatformSubProcessStatus::FailedToStart) {
+ throw SubProcessException(u"The process failed to start. Can't kill it.");
+ }
+
+ if (status == PlatformSubProcessStatus::Exited) {
+ return;
+ }
+
+ PlatformKillProcess();
+}
+
+PlatformSubProcessStatus PlatformSubProcessBase::GetStatus() {
+ auto data_lock_guard = CreateDataLockGuard();
+ return status_;
+}
} // namespace cru