From 052bdf4ec7945a2b7b9b2262906ad00cdca42dc1 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Sun, 3 Nov 2013 18:35:17 +0100 Subject: [PATCH] Added JobObject class. --- src/job_object.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/job_object.h | 38 ++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/job_object.cpp create mode 100644 src/job_object.h diff --git a/src/job_object.cpp b/src/job_object.cpp new file mode 100644 index 0000000..dfaa286 --- /dev/null +++ b/src/job_object.cpp @@ -0,0 +1,113 @@ +/////////////////////////////////////////////////////////////////////////////// +// Simple x264 Launcher +// Copyright (C) 2004-2013 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version, but always including the *additional* +// restrictions defined in the "License.txt" file. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#include "job_object.h" + +#include "global.h" + +#include + +//Windows includes +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include + +JobObject::JobObject(void) +: + m_hJobObject(NULL) +{ + HANDLE jobObject = CreateJobObject(NULL, NULL); + if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE)) + { + JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo; + memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); + memset(&jobExtendedLimitInfo.BasicLimitInformation, 0, sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION)); + jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION; + if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION))) + { + m_hJobObject = jobObject; + } + else + { + qWarning("Failed to set job object information!"); + CloseHandle(jobObject); + } + } + else + { + qWarning("Failed to create the job object!"); + } +} + +JobObject::~JobObject(void) +{ + if(m_hJobObject) + { + CloseHandle(m_hJobObject); + m_hJobObject = NULL; + } +} + +bool JobObject::addProcessToJob(const QProcess *proc) +{ + if(m_hJobObject) + { + if(AssignProcessToJobObject(m_hJobObject, proc->pid()->hProcess)) + { + return true; + } + else + { + qWarning("Failed to assign process to job object!"); + return false; + } + } + else + { + qWarning("Cannot assign process to job: No job bject available!"); + return false; + } +} + +bool JobObject::terminateJob(unsigned int exitCode) +{ + if(m_hJobObject) + { + if(TerminateJobObject(m_hJobObject, exitCode)) + { + return true; + } + else + { + qWarning("Failed to terminate job object!"); + return false; + } + } + else + { + qWarning("Cannot assign process to job: No job bject available!"); + return false; + } +} diff --git a/src/job_object.h b/src/job_object.h new file mode 100644 index 0000000..d2a7659 --- /dev/null +++ b/src/job_object.h @@ -0,0 +1,38 @@ +/////////////////////////////////////////////////////////////////////////////// +// LameXP - Audio Encoder Front-End +// Copyright (C) 2004-2013 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version, but always including the *additional* +// restrictions defined in the "License.txt" file. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#pragma once + +class QProcess; + +class JobObject +{ +public: + JobObject(void); + ~JobObject(void); + + bool addProcessToJob(const QProcess *proc); + bool terminateJob(unsigned int exitCode = -1); + +private: + void *m_hJobObject; +}; -- 2.11.0