Skip to content

Instantly share code, notes, and snippets.

@nico-abram
Created October 22, 2019 23:47
Show Gist options
  • Save nico-abram/aeedec4f3073db99211e88b5255fc7fe to your computer and use it in GitHub Desktop.
Save nico-abram/aeedec4f3073db99211e88b5255fc7fe to your computer and use it in GitHub Desktop.
int onePercent = std::max(static_cast<int>(scores.size() / 100 * 5), 1);
int scoreindex = 0;
// Add this include
#include <cstdint>
mutex songVectorPtrMutex;
vector<std::uintptr_t> currentlyLockedSongs;
// This is meant to ensure mutual exclusion for a song
class SongLock
{
public:
mutex& songVectorPtrMutex; // This mutex guards the vector
vector<std::uintptr_t>& currentlyLockedSongs; // Vector of currently locked songs
std::uintptr_t song; // The song for this lock
SongLock(vector<std::uintptr_t>& vec, mutex& mut, std::uintptr_t k)
: currentlyLockedSongs(vec)
, songVectorPtrMutex(mut)
, song(k)
{
bool active = true;
{
lock_guard<mutex> lk(songVectorPtrMutex);
active = find(currentlyLockedSongs.begin(), currentlyLockedSongs.end(), song) !=
currentlyLockedSongs.end();
if (!active)
currentlyLockedSongs.emplace_back(song);
}
while (active) {
// TODO: Try to make this wake up from the destructor (CondVar's maybe)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
{
lock_guard<mutex> lk(songVectorPtrMutex);
active =
find(currentlyLockedSongs.begin(), currentlyLockedSongs.end(), song) !=
currentlyLockedSongs.end();
if (!active)
currentlyLockedSongs.emplace_back(song);
}
}
}
~SongLock()
{
lock_guard<mutex> lk(songVectorPtrMutex);
currentlyLockedSongs.erase(
find(currentlyLockedSongs.begin(), currentlyLockedSongs.end(), song));
}
};
function<void(std::pair<vectorIt<HighScore*>, vectorIt<HighScore*>>,
ThreadData*)>
callback =
[&stepsMutex, &currentSteps](
std::pair<vectorIt<HighScore*>, vectorIt<HighScore*>> workload,
ThreadData* data) {
auto pair =
static_cast<std::pair<int, LoadingWindow*>*>(data->data);
auto onePercent = pair->first;
auto ld = pair->second;
int scoreIndex = 0;
int lastUpdate = 0;
for (auto it = workload.first; it != workload.second; it++) {
auto hs = *it;
if (ld && scoreIndex % onePercent == 0) {
data->_progress += scoreIndex - lastUpdate;
lastUpdate = scoreIndex;
data->setUpdated(true);
}
++scoreIndex;
if (hs->GetSSRCalcVersion() == GetCalcVersion())
continue;
string ck = hs->GetChartKey();
Steps* steps = SONGMAN->GetStepsByChartkey(ck);
if (!steps)
continue;
SongLock lk(currentlyLockedSongs, songVectorPtrMutex, steps.m_pSong);
if (!steps->IsRecalcValid()) {
hs->ResetSkillsets();
continue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment