Fixed windows deadlocks, performance is shit tho

This commit is contained in:
ImBenji
2025-12-11 17:40:35 +00:00
parent fc7297e0f8
commit 0836935888
2 changed files with 26 additions and 5 deletions

View File

@@ -107,13 +107,13 @@ void SweepstoreConcurrency::spawnTicket(SweepstoreFileHandle* _file,
SweepstoreWorkerTicketSnapshot snapshot = ticket.snapshot(); SweepstoreWorkerTicketSnapshot snapshot = ticket.snapshot();
int identifier = snapshot.identifier; uint32_t identifier = snapshot.identifier;
bool identifier_unassigned = identifier == 0; bool identifier_unassigned = identifier == 0;
bool stale_heartbeat = millisecondsSinceEpoch32() - snapshot.workerHeartbeat > STALE_HEARTBEAT_THRESHOLD_MS; bool stale_heartbeat = millisecondsSinceEpoch32() - snapshot.workerHeartbeat > STALE_HEARTBEAT_THRESHOLD_MS;
bool is_free = snapshot.state == SweepstoreTicketState::FREE; bool is_free = snapshot.state == SweepstoreTicketState::FREE;
if (identifier_unassigned && stale_heartbeat && is_free) { if ((identifier_unassigned && is_free) || stale_heartbeat) {
SWEEPSTORE_TIME_SCOPE("Claim Ticket"); SWEEPSTORE_TIME_SCOPE("Claim Ticket");
snapshot.identifier = newIdentifier; snapshot.identifier = newIdentifier;
snapshot.workerHeartbeat = millisecondsSinceEpoch32(); snapshot.workerHeartbeat = millisecondsSinceEpoch32();
@@ -218,7 +218,7 @@ void SweepstoreConcurrency::initialiseMaster(std::string filePath) {
SWEEPSTORE_TIME_FUNCTION(); SWEEPSTORE_TIME_FUNCTION();
auto log = [&](const std::string &message) { auto log = [&](const std::string &message) {
debugPrint("\033[38;5;33m[Concurrency Master]:\033[0m " + message); debugPrint(("\033[38;5;33m[Concurrency Master]:\033[0m " + message).c_str());
}; };
SweepstoreFileHandle file(filePath, std::ios::binary | std::ios::in | std::ios::out); SweepstoreFileHandle file(filePath, std::ios::binary | std::ios::in | std::ios::out);
@@ -252,9 +252,27 @@ void SweepstoreConcurrency::initialiseMaster(std::string filePath) {
ticket.write(cleanSnapshot); ticket.write(cleanSnapshot);
log("Reset ticket " + std::to_string(i) + "."); log("Reset ticket " + std::to_string(i) + ".");
} }
}
// Handle stale tickets continue;
uint32_t currentTime = millisecondsSinceEpoch32();
// Cleanup any stale tickets
uint32_t currentTime = millisecondsSinceEpoch32();
for (uint32_t i = 0; i < concurrentWorkers; i++) {
SweepstoreWorkerTicket ticket(i, file);
SweepstoreWorkerTicketSnapshot snapshot = ticket.snapshot();
if (snapshot.state != SweepstoreTicketState::FREE) {
continue;
}
bool stale_heartbeat = currentTime - snapshot.workerHeartbeat > STALE_HEARTBEAT_THRESHOLD_MS;
if (stale_heartbeat) {
SweepstoreWorkerTicketSnapshot cleanSnapshot = SweepstoreWorkerTicketSnapshot();
ticket.write(cleanSnapshot);
log("Stale heartbeat " + std::to_string(i) + ".");
}
} }
preciseSleep(std::chrono::milliseconds(1)); preciseSleep(std::chrono::milliseconds(1));

View File

@@ -84,6 +84,9 @@ private:
if (it->second == Mode::Shared && mode == Mode::Exclusive) { if (it->second == Mode::Shared && mode == Mode::Exclusive) {
releaseInternal(); // Release the old shared lock releaseInternal(); // Release the old shared lock
activeLocks.erase(it); activeLocks.erase(it);
// Small delay to allow OS to process the unlock before re-locking
// This prevents deadlock when multiple threads upgrade simultaneously
Sleep(1);
} else { } else {
// Already hold compatible or same lock // Already hold compatible or same lock
locked = true; locked = true;