13
13
#include " Etterna/FileTypes/XmlFileUtil.h"
14
14
#include " arch/LoadingWindow/LoadingWindow.h"
15
15
#include " RageUtil/Misc/RageThreads.h"
16
+ #include < cstdint>
16
17
17
18
ScoreManager* SCOREMAN = NULL ;
18
19
@@ -226,7 +227,8 @@ ScoresForChart::SetTopScores()
226
227
eligiblescores.emplace_back (hs);
227
228
}
228
229
229
- // if there aren't 2 noccpbs in top scores we might as well use old cc scores -mina
230
+ // if there aren't 2 noccpbs in top scores we might as well use old cc
231
+ // scores -mina
230
232
if (eligiblescores.size () < 2 ) {
231
233
FOREACHM (int , ScoresAtRate, ScoresByRate, i)
232
234
{
@@ -237,7 +239,7 @@ ScoresForChart::SetTopScores()
237
239
eligiblescores.emplace_back (hs);
238
240
}
239
241
}
240
-
242
+
241
243
if (eligiblescores.empty ())
242
244
return ;
243
245
@@ -351,50 +353,56 @@ ScoreManager::RecalculateSSRs(LoadingWindow* ld, const string& profileID)
351
353
}
352
354
int onePercent = std::max (static_cast <int >(scores.size () / 100 * 5 ), 1 );
353
355
int scoreindex = 0 ;
354
- mutex stepsMutex;
355
- vector<string> currentSteps;
356
- class StepsLock
356
+
357
+ mutex songVectorPtrMutex;
358
+ vector<std::uintptr_t > currentlyLockedSongs;
359
+ // This is meant to ensure mutual exclusion for a song
360
+ class SongLock
357
361
{
358
362
public:
359
- mutex& stepsMutex;
360
- vector<string>& currentSteps;
361
- string& ck;
362
- StepsLock (vector<string>& vec, mutex& mut, string& k)
363
- : currentSteps(vec)
364
- , stepsMutex(mut)
365
- , ck(k)
363
+ mutex& songVectorPtrMutex; // This mutex guards the vector
364
+ vector<std::uintptr_t >&
365
+ currentlyLockedSongs; // Vector of currently locked songs
366
+ std::uintptr_t song; // The song for this lock
367
+ SongLock (vector<std::uintptr_t >& vec, mutex& mut, std::uintptr_t k)
368
+ : currentlyLockedSongs(vec)
369
+ , songVectorPtrMutex(mut)
370
+ , song(k)
366
371
{
367
372
bool active = true ;
368
373
{
369
- lock_guard<mutex> lk (stepsMutex);
370
- active = find (currentSteps.begin (), currentSteps.end (), ck) !=
371
- currentSteps.end ();
374
+ lock_guard<mutex> lk (songVectorPtrMutex);
375
+ active = find (currentlyLockedSongs.begin (),
376
+ currentlyLockedSongs.end (),
377
+ song) != currentlyLockedSongs.end ();
372
378
if (!active)
373
- currentSteps .emplace_back (ck );
379
+ currentlyLockedSongs .emplace_back (song );
374
380
}
375
381
while (active) {
382
+ // TODO: Try to make this wake up from the destructor (CondVar's
383
+ // maybe)
376
384
std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
377
385
{
378
- lock_guard<mutex> lk (stepsMutex );
379
- active =
380
- find (currentSteps. begin (), currentSteps. end (), ck) !=
381
- currentSteps .end ();
386
+ lock_guard<mutex> lk (songVectorPtrMutex );
387
+ active = find (currentlyLockedSongs. begin (),
388
+ currentlyLockedSongs. end (),
389
+ song) != currentlyLockedSongs .end ();
382
390
if (!active)
383
- currentSteps .emplace_back (ck );
391
+ currentlyLockedSongs .emplace_back (song );
384
392
}
385
393
}
386
394
}
387
- ~StepsLock ()
395
+ ~SongLock ()
388
396
{
389
- lock_guard<mutex> lk (stepsMutex );
390
- currentSteps .erase (
391
- find (currentSteps .begin (), currentSteps .end (), ck ));
397
+ lock_guard<mutex> lk (songVectorPtrMutex );
398
+ currentlyLockedSongs .erase ( find (
399
+ currentlyLockedSongs .begin (), currentlyLockedSongs .end (), song ));
392
400
}
393
401
};
394
402
function<void (std::pair<vectorIt<HighScore*>, vectorIt<HighScore*>>,
395
403
ThreadData*)>
396
404
callback =
397
- [&stepsMutex , ¤tSteps ](
405
+ [&songVectorPtrMutex , ¤tlyLockedSongs ](
398
406
std::pair<vectorIt<HighScore*>, vectorIt<HighScore*>> workload,
399
407
ThreadData* data) {
400
408
auto pair =
@@ -414,12 +422,15 @@ ScoreManager::RecalculateSSRs(LoadingWindow* ld, const string& profileID)
414
422
if (hs->GetSSRCalcVersion () == GetCalcVersion ())
415
423
continue ;
416
424
string ck = hs->GetChartKey ();
417
- StepsLock lk (currentSteps, stepsMutex, ck);
418
425
Steps* steps = SONGMAN->GetStepsByChartkey (ck);
419
426
420
427
if (!steps)
421
428
continue ;
422
429
430
+ SongLock lk (currentlyLockedSongs,
431
+ songVectorPtrMutex,
432
+ reinterpret_cast <std::uintptr_t >(steps->m_pSong ));
433
+
423
434
if (!steps->IsRecalcValid ()) {
424
435
hs->ResetSkillsets ();
425
436
continue ;
@@ -716,7 +727,8 @@ ScoresAtRate::LoadFromNode(const XNode* node,
716
727
SCOREMAN->RegisterScore (&scores.find (sk)->second );
717
728
SCOREMAN->AddToKeyedIndex (&scores.find (sk)->second );
718
729
SCOREMAN->RegisterScoreInProfile (&scores.find (sk)->second , profileID);
719
- if (scores[sk].GetSSRCalcVersion () != GetCalcVersion () && SONGMAN->IsChartLoaded (ck))
730
+ if (scores[sk].GetSSRCalcVersion () != GetCalcVersion () &&
731
+ SONGMAN->IsChartLoaded (ck))
720
732
SCOREMAN->scorestorecalc .emplace_back (&scores[sk]);
721
733
}
722
734
}
0 commit comments