| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <jobs/jobmanager.h> |
| 14 | |
| 15 | #include <config.h> |
| 16 | #include <utilities.h> |
| 17 | |
| 18 | #include <support/autorelease.h> |
| 19 | |
| 20 | #include <dialogs/error.h> |
| 21 | |
| 22 | freac::JobManager *freac::JobManager::instance = NIL; |
| 23 | |
| 24 | freac::JobManager::JobManager() |
| 25 | { |
| 26 | exitThread = False; |
| 27 | |
| 28 | /* Enable R/W locking for job lists. |
| 29 | */ |
| 30 | Job::GetScheduledJobs().EnableLocking(); |
| 31 | Job::GetPlannedJobs().EnableLocking(); |
| 32 | Job::GetRunningJobs().EnableLocking(); |
| 33 | |
| 34 | Job::GetAllJobs().EnableLocking(); |
| 35 | |
| 36 | finished.EnableLocking(); |
| 37 | |
| 38 | /* Connect slots. |
| 39 | */ |
| 40 | Job::onFinishJob.Connect(&JobManager::OnFinishJob, this); |
| 41 | |
| 42 | /* Start job manager thread. |
| 43 | */ |
| 44 | managerThread = NonBlocking0<>(&JobManager::ManagerThread, this).Call(); |
| 45 | } |
| 46 | |
| 47 | freac::JobManager::~JobManager() |
| 48 | { |
| 49 | Threads::Access::Set(exitThread, True); |
| 50 | |
| 51 | /* Wait for manager thread to exit. |
| 52 | */ |
| 53 | managerThread->Wait(); |
| 54 | |
| 55 | /* Disconnect slots. |
| 56 | */ |
| 57 | Job::onFinishJob.Disconnect(&JobManager::OnFinishJob, this); |
| 58 | |
| 59 | /* Delete remaining job objects. |
| 60 | */ |
| 61 | const Array<Job *> &all = Job::GetAllJobs(); |
| 62 | |
| 63 | foreachreverse (Job *job, all) Object::DeleteObject(job); |
| 64 | } |
| 65 | |
| 66 | Void freac::JobManager::ManagerThread() |
| 67 | { |
| 68 | BoCA::Config *config = BoCA::Config::Get(); |
| 69 | |
| 70 | while (!Threads::Access::Value(exitThread)) |
| 71 | { |
| 72 | const Array<Job *> &scheduled = Job::GetScheduledJobs(); |
| 73 | const Array<Job *> &planned = Job::GetPlannedJobs(); |
| 74 | |
| 75 | /* Check scheduled jobs; |
| 76 | */ |
| 77 | scheduled.LockForRead(); |
| 78 | |
| 79 | if (scheduled.Length() > 0) |
| 80 | { |
| 81 | AutoRelease autoRelease; |
| 82 | |
| 83 | foreachreverse (Job *job, scheduled) |
| 84 | { |
| 85 | scheduled.Unlock(); |
| 86 | |
| 87 | /* Call the job's precheck method. |
| 88 | */ |
| 89 | if (job->RunPrecheck() != Success()) Object::DeleteObject(job); |
| 90 | |
| 91 | scheduled.LockForRead(); |
| 92 | } |
| 93 | |
| 94 | scheduled.Unlock(); |
| 95 | |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | scheduled.Unlock(); |
| 100 | |
| 101 | /* Check planned jobs. |
| 102 | */ |
| 103 | planned.LockForRead(); |
| 104 | |
| 105 | if (planned.Length() > 0) |
| 106 | { |
| 107 | AutoRelease autoRelease; |
| 108 | |
| 109 | foreach (Job *job, planned) |
| 110 | { |
| 111 | /* Check if the job is ready to run and if so start it. |
| 112 | */ |
| 113 | if (!job->ReadyToRun()) continue; |
| 114 | |
| 115 | NonBlocking0<>(&Job::Run, job).Call(); |
| 116 | |
| 117 | /* Wait until job is removed from planned list. |
| 118 | */ |
| 119 | while (True) |
| 120 | { |
| 121 | if (planned.Get(job->GetHandle()) == NIL) break; |
| 122 | |
| 123 | planned.Unlock(); |
| 124 | |
| 125 | S::System::System::Sleep(0); |
| 126 | |
| 127 | planned.LockForRead(); |
| 128 | } |
| 129 | |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | planned.Unlock(); |
| 135 | |
| 136 | /* Check finished jobs. |
| 137 | */ |
| 138 | finished.LockForRead(); |
| 139 | |
| 140 | if (finished.Length() > 0) |
| 141 | { |
| 142 | AutoRelease autoRelease; |
| 143 | |
| 144 | Bool enableConsole = config->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault); |
| 145 | Bool displayErrors = config->GetIntValue(Config::CategorySettingsID, Config::SettingsDisplayErrorsID, Config::SettingsDisplayErrorsDefault); |
| 146 | |
| 147 | finished.Unlock(); |
| 148 | finished.LockForWrite(); |
| 149 | |
| 150 | foreachreverse (Job *job, finished) |
| 151 | { |
| 152 | finished.Remove(job->GetHandle()); |
| 153 | finished.Unlock(); |
| 154 | |
| 155 | /* Check for errors and display error list if any. |
| 156 | */ |
| 157 | if (job->GetErrors().Length() > 0 && !enableConsole) |
| 158 | { |
| 159 | if (displayErrors) |
| 160 | { |
| 161 | ErrorDialog dialog(job->GetErrors()); |
| 162 | |
| 163 | dialog.ShowDialog(); |
| 164 | } |
| 165 | |
| 166 | finished.LockForWrite(); |
| 167 | |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | /* Check for warnings and keep job object alive if any. |
| 172 | */ |
| 173 | if (job->GetWarnings().Length() > 0 && !enableConsole) |
| 174 | { |
| 175 | finished.LockForWrite(); |
| 176 | |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | Object::DeleteObject(job); |
| 181 | |
| 182 | finished.LockForWrite(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | finished.Unlock(); |
| 187 | |
| 188 | /* Wait 100ms before checking jobs again. |
| 189 | */ |
| 190 | S::System::System::Sleep(100); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | Void freac::JobManager::OnFinishJob(Job *job) |
| 195 | { |
| 196 | /* Add job object to finished list. |
| 197 | */ |
| 198 | finished.Add(job, job->GetHandle()); |
| 199 | } |
| 200 | |
| 201 | Error freac::JobManager::Start() |
| 202 | { |
| 203 | if (instance != NIL) return Error(); |
| 204 | |
| 205 | instance = new JobManager(); |
| 206 | |
| 207 | return Success(); |
| 208 | } |
| 209 | |
| 210 | Error freac::JobManager::Quit() |
| 211 | { |
| 212 | if (instance == NIL) return Error(); |
| 213 | |
| 214 | delete instance; |
| 215 | |
| 216 | instance = NIL; |
| 217 | |
| 218 | return Success(); |
| 219 | } |