From 295490fe8366e2d96046e1d14a582526cde956ef Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 16 Dec 2023 23:55:49 +0800 Subject: Develop PlatformSubProcessBase. --- src/common/SubProcess.cpp | 56 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) (limited to 'src') 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 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 -- cgit v1.2.3