Skip to content
This repository
Browse code

[musicdb] fix: restructure the filecountreader so it can be killed an…

…d kill the thread before modifying shared variables
  • Loading branch information...
commit 85aabfabfae15092fc08a8077153b8ee07bcdb8a 1 parent cb65532
night199uk authored April 15, 2013
110  xbmc/music/infoscanner/MusicInfoScanner.cpp
@@ -58,7 +58,7 @@ using namespace MUSIC_INFO;
58 58
 using namespace XFILE;
59 59
 using namespace MUSIC_GRABBER;
60 60
 
61  
-CMusicInfoScanner::CMusicInfoScanner() : CThread("MusicInfoScanner")
  61
+CMusicInfoScanner::CMusicInfoScanner() : CThread("MusicInfoScanner"), m_fileCountReader(this, "MusicFileCounter")
62 62
 {
63 63
   m_bRunning = false;
64 64
   m_showDialog = false;
@@ -104,9 +104,8 @@ void CMusicInfoScanner::Process()
104 104
 
105 105
       // Create the thread to count all files to be scanned
106 106
       SetPriority( GetMinPriority() );
107  
-      CThread fileCountReader(this, "MusicFileCounter");
108 107
       if (m_handle)
109  
-        fileCountReader.Create();
  108
+        m_fileCountReader.Create();
110 109
 
111 110
       // Database operations should not be canceled
112 111
       // using Interupt() while scanning as it could
@@ -151,7 +150,7 @@ void CMusicInfoScanner::Process()
151 150
         }
152 151
       }
153 152
 
154  
-      fileCountReader.StopThread();
  153
+      m_fileCountReader.StopThread();
155 154
 
156 155
       m_musicDatabase.EmptyCache();
157 156
 
@@ -219,6 +218,8 @@ void CMusicInfoScanner::Process()
219 218
 
220 219
 void CMusicInfoScanner::Start(const CStdString& strDirectory, int flags)
221 220
 {
  221
+  m_fileCountReader.StopThread();
  222
+  StopThread();
222 223
   m_pathsToScan.clear();
223 224
   m_albumsScanned.clear();
224 225
   m_artistsScanned.clear();
@@ -235,7 +236,6 @@ void CMusicInfoScanner::Start(const CStdString& strDirectory, int flags)
235 236
     m_pathsToScan.insert(strDirectory);
236 237
   m_pathsToCount = m_pathsToScan;
237 238
   m_scanType = 0;
238  
-  StopThread();
239 239
   Create();
240 240
   m_bRunning = true;
241 241
 }
@@ -244,6 +244,8 @@ void CMusicInfoScanner::FetchAlbumInfo(const CStdString& strDirectory,
244 244
                                        bool refresh)
245 245
 {
246 246
   m_albumsToScan.clear();
  247
+  m_fileCountReader.StopThread();
  248
+  StopThread();
247 249
   m_albumsScanned.clear();
248 250
 
249 251
   CFileItemList items;
@@ -285,7 +287,6 @@ void CMusicInfoScanner::FetchAlbumInfo(const CStdString& strDirectory,
285 287
   m_musicDatabase.Close();
286 288
 
287 289
   m_scanType = 1;
288  
-  StopThread();
289 290
   Create();
290 291
   m_bRunning = true;
291 292
 }
@@ -294,6 +295,8 @@ void CMusicInfoScanner::FetchArtistInfo(const CStdString& strDirectory,
294 295
                                         bool refresh)
295 296
 {
296 297
   m_artistsToScan.clear();
  298
+  m_fileCountReader.StopThread();
  299
+  StopThread();
297 300
   m_artistsScanned.clear();
298 301
   CFileItemList items;
299 302
 
@@ -334,7 +337,6 @@ void CMusicInfoScanner::FetchArtistInfo(const CStdString& strDirectory,
334 337
   m_musicDatabase.Close();
335 338
 
336 339
   m_scanType = 2;
337  
-  StopThread();
338 340
   Create();
339 341
   m_bRunning = true;
340 342
 }
@@ -853,53 +855,6 @@ void CMusicInfoScanner::FindArtForAlbums(VECALBUMS &albums, const CStdString &pa
853 855
   }
854 856
 }
855 857
 
856  
-// This function is run by another thread
857  
-void CMusicInfoScanner::Run()
858  
-{
859  
-  int count = 0;
860  
-  while (!m_bStop && m_pathsToCount.size())
861  
-    count+=CountFilesRecursively(*m_pathsToCount.begin());
862  
-  m_itemCount = count;
863  
-}
864  
-
865  
-// Recurse through all folders we scan and count files
866  
-int CMusicInfoScanner::CountFilesRecursively(const CStdString& strPath)
867  
-{
868  
-  // load subfolder
869  
-  CFileItemList items;
870  
-//  CLog::Log(LOGDEBUG, __FUNCTION__" - processing dir: %s", strPath.c_str());
871  
-  CDirectory::GetDirectory(strPath, items, g_advancedSettings.m_musicExtensions, DIR_FLAG_NO_FILE_DIRS);
872  
-
873  
-  if (m_bStop)
874  
-    return 0;
875  
-
876  
-  // true for recursive counting
877  
-  int count = CountFiles(items, true);
878  
-
879  
-  // remove this path from the list we're processing
880  
-  set<CStdString>::iterator it = m_pathsToCount.find(strPath);
881  
-  if (it != m_pathsToCount.end())
882  
-    m_pathsToCount.erase(it);
883  
-
884  
-//  CLog::Log(LOGDEBUG, __FUNCTION__" - finished processing dir: %s", strPath.c_str());
885  
-  return count;
886  
-}
887  
-
888  
-int CMusicInfoScanner::CountFiles(const CFileItemList &items, bool recursive)
889  
-{
890  
-  int count = 0;
891  
-  for (int i=0; i<items.Size(); ++i)
892  
-  {
893  
-    const CFileItemPtr pItem=items[i];
894  
-
895  
-    if (recursive && pItem->m_bIsFolder)
896  
-      count+=CountFilesRecursively(pItem->GetPath());
897  
-    else if (pItem->IsAudio() && !pItem->IsPlayList() && !pItem->IsNFO())
898  
-      count++;
899  
-  }
900  
-  return count;
901  
-}
902  
-
903 858
 int CMusicInfoScanner::GetPathHash(const CFileItemList &items, CStdString &hash)
904 859
 {
905 860
   // Create a hash based on the filenames, filesize and filedate.  Also count the number of files
@@ -1359,3 +1314,50 @@ map<string, string> CMusicInfoScanner::GetArtistArtwork(long id, const CArtist *
1359 1314
 
1360 1315
   return artwork;
1361 1316
 }
  1317
+
  1318
+// This function is the Run() function of the IRunnable
  1319
+// CFileCountReader and runs in a separate thread.
  1320
+void CMusicInfoScanner::Run()
  1321
+{
  1322
+  int count = 0;
  1323
+  while (!m_bStop && m_pathsToCount.size())
  1324
+  {
  1325
+    count+=CountFilesRecursively(*m_pathsToCount.begin());
  1326
+  }
  1327
+  m_itemCount = count;
  1328
+}
  1329
+
  1330
+// Recurse through all folders we scan and count files
  1331
+int CMusicInfoScanner::CountFilesRecursively(const CStdString& strPath)
  1332
+{
  1333
+  // load subfolder
  1334
+  CFileItemList items;
  1335
+  CDirectory::GetDirectory(strPath, items, g_advancedSettings.m_musicExtensions, DIR_FLAG_NO_FILE_DIRS);
  1336
+
  1337
+  if (m_bStop)
  1338
+    return 0;
  1339
+
  1340
+  int count = CountFiles(items, true);
  1341
+
  1342
+  // true for recursive counting
  1343
+  set<CStdString>::iterator it = m_pathsToCount.find(strPath);
  1344
+  if (it != m_pathsToCount.end())
  1345
+    m_pathsToCount.erase(it);
  1346
+
  1347
+  return count;
  1348
+}
  1349
+
  1350
+int CMusicInfoScanner::CountFiles(const CFileItemList &items, bool recursive)
  1351
+{
  1352
+  int count = 0;
  1353
+  for (int i=0; i<items.Size(); ++i)
  1354
+  {
  1355
+    const CFileItemPtr pItem=items[i];
  1356
+    
  1357
+    if (recursive && pItem->m_bIsFolder)
  1358
+      count+=CountFilesRecursively(pItem->GetPath());
  1359
+    else if (pItem->IsAudio() && !pItem->IsPlayList() && !pItem->IsNFO())
  1360
+      count++;
  1361
+  }
  1362
+  return count;
  1363
+}
1  xbmc/music/infoscanner/MusicInfoScanner.h
@@ -145,5 +145,6 @@ class CMusicInfoScanner : CThread, public IRunnable
145 145
   std::vector<long> m_artistsScanned;
146 146
   std::vector<long> m_albumsScanned;
147 147
   int m_flags;
  148
+  CThread m_fileCountReader;
148 149
 };
149 150
 }

0 notes on commit 85aabfa

Please sign in to comment.
Something went wrong with that request. Please try again.