Simplify app::Job implementation

* Remove pointers to base::thread/mutex
* Use std::thread/mutex as values
This commit is contained in:
David Capello 2021-09-30 14:06:25 -03:00
parent d7ddb7feed
commit 89904afa82
2 changed files with 12 additions and 24 deletions

View File

@ -1,4 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -12,10 +13,10 @@
#include "app/app.h" #include "app/app.h"
#include "app/console.h" #include "app/console.h"
#include "app/context.h"
#include "app/i18n/strings.h" #include "app/i18n/strings.h"
#include "base/mutex.h" #include "base/mutex.h"
#include "base/scoped_lock.h" #include "base/scoped_lock.h"
#include "base/thread.h"
#include "fmt/format.h" #include "fmt/format.h"
#include "ui/alert.h" #include "ui/alert.h"
#include "ui/widget.h" #include "ui/widget.h"
@ -36,14 +37,10 @@ int Job::runningJobs()
Job::Job(const char* jobName) Job::Job(const char* jobName)
{ {
m_mutex = NULL;
m_thread = NULL;
m_last_progress = 0.0; m_last_progress = 0.0;
m_done_flag = false; m_done_flag = false;
m_canceled_flag = false; m_canceled_flag = false;
m_mutex = new base::mutex();
if (App::instance()->isGui()) { if (App::instance()->isGui()) {
m_alert_window = ui::Alert::create( m_alert_window = ui::Alert::create(
fmt::format(Strings::alerts_job_working(), jobName)); fmt::format(Strings::alerts_job_working(), jobName));
@ -59,19 +56,15 @@ Job::~Job()
{ {
if (App::instance()->isGui()) { if (App::instance()->isGui()) {
ASSERT(!m_timer->isRunning()); ASSERT(!m_timer->isRunning());
ASSERT(m_thread == NULL);
if (m_alert_window) if (m_alert_window)
m_alert_window->closeWindow(NULL); m_alert_window->closeWindow(NULL);
} }
if (m_mutex)
delete m_mutex;
} }
void Job::startJob() void Job::startJob()
{ {
m_thread = new base::thread(&Job::thread_proc, this); m_thread = std::thread(&Job::thread_proc, this);
++g_runningJobs; ++g_runningJobs;
if (m_alert_window) { if (m_alert_window) {
@ -79,7 +72,7 @@ void Job::startJob()
// The job was canceled by the user? // The job was canceled by the user?
{ {
base::scoped_lock hold(*m_mutex); std::unique_lock<std::mutex> hold(m_mutex);
if (!m_done_flag) if (!m_done_flag)
m_canceled_flag = true; m_canceled_flag = true;
} }
@ -107,10 +100,8 @@ void Job::waitJob()
if (m_timer && m_timer->isRunning()) if (m_timer && m_timer->isRunning())
m_timer->stop(); m_timer->stop();
if (m_thread) { if (m_thread.joinable()) {
m_thread->join(); m_thread.join();
delete m_thread;
m_thread = nullptr;
--g_runningJobs; --g_runningJobs;
} }
@ -128,7 +119,7 @@ bool Job::isCanceled()
void Job::onMonitoringTick() void Job::onMonitoringTick()
{ {
base::scoped_lock hold(*m_mutex); std::unique_lock<std::mutex> hold(m_mutex);
// update progress // update progress
m_alert_window->setProgress(m_last_progress); m_alert_window->setProgress(m_last_progress);
@ -142,7 +133,7 @@ void Job::onMonitoringTick()
void Job::done() void Job::done()
{ {
base::scoped_lock hold(*m_mutex); std::unique_lock<std::mutex> hold(m_mutex);
m_done_flag = true; m_done_flag = true;
} }

View File

@ -14,11 +14,8 @@
#include <atomic> #include <atomic>
#include <exception> #include <exception>
#include <mutex>
namespace base { #include <thread>
class thread;
class mutex;
}
namespace app { namespace app {
@ -62,9 +59,9 @@ namespace app {
static void monitor_proc(void* data); static void monitor_proc(void* data);
static void monitor_free(void* data); static void monitor_free(void* data);
base::thread* m_thread; std::thread m_thread;
std::unique_ptr<ui::Timer> m_timer; std::unique_ptr<ui::Timer> m_timer;
base::mutex* m_mutex; std::mutex m_mutex;
ui::AlertPtr m_alert_window; ui::AlertPtr m_alert_window;
std::atomic<double> m_last_progress; std::atomic<double> m_last_progress;
bool m_done_flag; bool m_done_flag;