36 #include <nsThreadUtils.h>
37 #include <nsComponentManagerUtils.h>
48 extern PRLogModuleInfo* gMetadataLog;
49 #define TRACE(args) PR_LOG(gMetadataLog, PR_LOG_DEBUG, args)
50 #define LOG(args) PR_LOG(gMetadataLog, PR_LOG_WARN, args)
63 mJobManager(aManager),
65 mShouldShutdown(PR_FALSE),
69 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - ctor",
this));
70 NS_ASSERTION(NS_IsMainThread(),
71 "sbBackgroundThreadMetadataProcessor called off the main thread");
77 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - dtor",
this));
82 nsAutoMonitor::DestroyMonitor(
mMonitor);
89 NS_ASSERTION(NS_IsMainThread(),
90 "sbBackgroundThreadMetadataProcessor called off the main thread!");
91 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - Start",
this));
95 mMonitor = nsAutoMonitor::NewMonitor(
96 "sbBackgroundThreadMetadataProcessor::mMonitor");
97 NS_ENSURE_TRUE(
mMonitor, NS_ERROR_OUT_OF_MEMORY);
104 rv = NS_NewThread(getter_AddRefs(
mThread),
this);
105 NS_ENSURE_SUCCESS(rv, rv);
108 rv = monitor.Notify();
109 NS_ASSERTION(NS_SUCCEEDED(rv),
110 "sbBackgroundThreadMetadataProcessor::Start monitor notify failed");
117 NS_ASSERTION(NS_IsMainThread(),
118 "sbBackgroundThreadMetadataProcessor called off the main thread");
119 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - Stop",
this));
127 rv = monitor.Notify();
128 NS_ASSERTION(NS_SUCCEEDED(rv),
129 "sbBackgroundThreadMetadataProcessor::Start monitor notify failed");
145 NS_IMETHODIMP sbBackgroundThreadMetadataProcessor::Run()
147 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - Thread Starting",
154 nsRefPtr<sbMetadataJobItem> item;
161 rv =
mJobManager->GetQueuedJobItem(PR_FALSE, getter_AddRefs(item));
166 if (rv == NS_ERROR_NOT_AVAILABLE) {
167 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - Thread waiting",
170 NS_ASSERTION(NS_SUCCEEDED(rv),
171 "sbBackgroundThreadMetadataProcessor::Run monitor wait failed");
174 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run encountered "
175 " an error while getting a job item.");
181 NS_ENSURE_SUCCESS(rv, rv);
184 nsRefPtr<sbMetadataJob> job;
185 rv = item->GetOwningJob(getter_AddRefs(job));
186 NS_ENSURE_SUCCESS(rv, rv);
189 nsCOMPtr<sbIMetadataHandler>
handler;
190 rv = item->GetHandler(getter_AddRefs(handler));
192 if (!NS_SUCCEEDED(rv)) {
193 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
194 " to get an sbIMetadataHandler.");
199 rv = item->GetJobType(&jobType);
200 if (!NS_SUCCEEDED(rv)) {
201 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
202 " to determine job type.");
207 PRBool skipItem = PR_FALSE;
210 PRBool jobItemIsBlocked;
211 rv =
mJobManager->GetJobItemIsBlocked(item, &jobItemIsBlocked);
213 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
214 " to determine if job item is blocked.");
220 if (!jobItemIsBlocked)
224 rv = job->SetBlocked(PR_TRUE);
226 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
227 " to set job blocked.");
233 PR_Sleep(PR_MillisecondsToInterval(20));
239 rv = job->SetBlocked(PR_FALSE);
241 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
242 " to set job not blocked.");
246 PRBool async = PR_FALSE;
247 PRInt32 operationRetVal;
249 rv = handler->Write(&operationRetVal);
251 rv = handler->Read(&operationRetVal);
257 if (operationRetVal == -1) {
263 if (NS_SUCCEEDED(rv)) {
264 rv = item->SetProcessingStarted(PR_TRUE);
266 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
267 " to set item processing started.");
270 PRBool handlerCompleted = PR_FALSE;
271 rv = handler->GetCompleted(&handlerCompleted);
272 if (!NS_SUCCEEDED(rv)) {
273 NS_ERROR(
"sbBackgroundThreadMetadataProcessor::Run unable "
274 " to determine check handler completed.");
284 if (async && !handlerCompleted) {
285 NS_WARNING(
"Attempting to run an async sbIMetadataHandler on " \
286 "a background thread! This may not work correctly!");
292 for (handler->GetCompleted(&handlerCompleted);
294 handler->GetCompleted(&handlerCompleted), counter++) {
297 PRBool
event = PR_FALSE;
300 event && eventCount < 10;
301 mThread->ProcessNextEvent(PR_FALSE, &
event), eventCount++) {
302 PR_Sleep(PR_MillisecondsToInterval(0));
305 PR_Sleep(PR_MillisecondsToInterval(20));
310 item->SetProcessed(PR_TRUE);
312 TRACE((
"sbBackgroundThreadMetadataProcessor - item processed"));
319 TRACE((
"sbBackgroundThreadMetadataProcessor[0x%.8x] - Thread Finished",
this));