forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.cpp
More file actions
154 lines (125 loc) · 2.76 KB
/
job.cpp
File metadata and controls
154 lines (125 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Aseprite
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/job.h"
#include "app/app.h"
#include "app/console.h"
#include "base/mutex.h"
#include "base/scoped_lock.h"
#include "base/thread.h"
#include "ui/alert.h"
#include "ui/widget.h"
#include "ui/window.h"
#include <atomic>
static const int kMonitoringPeriod = 100;
static std::atomic<int> g_runningJobs(0);
namespace app {
// static
int Job::runningJobs()
{
return g_runningJobs;
}
Job::Job(const char* jobName)
{
m_mutex = NULL;
m_thread = NULL;
m_last_progress = 0.0;
m_done_flag = false;
m_canceled_flag = false;
m_mutex = new base::mutex();
if (App::instance()->isGui()) {
m_alert_window = ui::Alert::create("%s<<Working...||&Cancel", jobName);
m_alert_window->addProgress();
m_timer.reset(new ui::Timer(kMonitoringPeriod, m_alert_window.get()));
m_timer->Tick.connect(&Job::onMonitoringTick, this);
m_timer->start();
}
}
Job::~Job()
{
if (App::instance()->isGui()) {
ASSERT(!m_timer->isRunning());
ASSERT(m_thread == NULL);
if (m_alert_window)
m_alert_window->closeWindow(NULL);
}
if (m_mutex)
delete m_mutex;
}
void Job::startJob()
{
m_thread = new base::thread(&Job::thread_proc, this);
++g_runningJobs;
if (m_alert_window) {
m_alert_window->openWindowInForeground();
// The job was canceled by the user?
{
base::scoped_lock hold(*m_mutex);
if (!m_done_flag)
m_canceled_flag = true;
}
// In case of error, take the "cancel" path (i.e. it's like the
// user canceled the operation).
if (m_error) {
m_canceled_flag = true;
try {
std::rethrow_exception(m_error);
}
catch (const std::exception& ex) {
Console::showException(ex);
}
}
}
}
void Job::waitJob()
{
if (m_timer && m_timer->isRunning())
m_timer->stop();
if (m_thread) {
m_thread->join();
delete m_thread;
m_thread = nullptr;
--g_runningJobs;
}
}
void Job::jobProgress(double f)
{
m_last_progress = f;
}
bool Job::isCanceled()
{
return m_canceled_flag;
}
void Job::onMonitoringTick()
{
base::scoped_lock hold(*m_mutex);
// update progress
m_alert_window->setProgress(m_last_progress);
// is job done? we can close the monitor
if (m_done_flag || m_canceled_flag) {
m_timer->stop();
m_alert_window->closeWindow(NULL);
}
}
void Job::done()
{
base::scoped_lock hold(*m_mutex);
m_done_flag = true;
}
// Called to start the worker thread.
void Job::thread_proc(Job* self)
{
try {
self->onJob();
}
catch (...) {
self->m_error = std::current_exception();
}
self->done();
}
} // namespace app