thread blocking DAG alloc fix

This commit is contained in:
2026-05-15 13:17:24 +02:00
parent 3337ac85ab
commit 5e520d57f6
2 changed files with 25 additions and 18 deletions

View File

@@ -4,6 +4,24 @@
khash_t(balance_sheet_map_m)* sheetMap = NULL;
static pthread_mutex_t g_sheetLock;
static int BalanceSheet_InsertLocked(balance_sheet_entry_t entry) {
if (!sheetMap) {
return -1;
}
key32_t key;
memcpy(key.bytes, entry.address, 32);
int ret = 0;
khiter_t k = kh_put(balance_sheet_map_m, sheetMap, key, &ret);
if (k == kh_end(sheetMap)) {
return -1;
}
kh_value(sheetMap, k) = entry;
return ret;
}
void BalanceSheet_Init() {
sheetMap = kh_init(balance_sheet_map_m);
pthread_mutex_init(&g_sheetLock, NULL);
@@ -13,19 +31,7 @@ int BalanceSheet_Insert(balance_sheet_entry_t entry) {
if (!sheetMap) { return -1; }
pthread_mutex_lock(&g_sheetLock);
// Encapsulate key
key32_t key;
memcpy(key.bytes, entry.address, 32);
int ret;
khiter_t k = kh_put(balance_sheet_map_m, sheetMap, key, &ret);
if (k == kh_end(sheetMap)) {
return -1;
}
kh_value(sheetMap, k) = entry;
int ret = BalanceSheet_InsertLocked(entry);
pthread_mutex_unlock(&g_sheetLock);
return ret;
}
@@ -94,7 +100,7 @@ bool BalanceSheet_LoadFromFile(const char* inPath) {
balance_sheet_entry_t entry;
while (fread(&entry, sizeof(balance_sheet_entry_t), 1, file) == 1) {
if (BalanceSheet_Insert(entry) < 0) {
if (BalanceSheet_InsertLocked(entry) < 0) {
fclose(file);
pthread_mutex_unlock(&g_sheetLock);
return false;

View File

@@ -512,6 +512,10 @@ int main(int argc, char* argv[]) {
signal(SIGINT, handle_sigint);
srand((unsigned int)time(NULL));
// Initialize runtime locks before any thread or helper can touch chain state.
pthread_rwlock_init(&chainLock, NULL);
pthread_mutex_init(&balanceSheetLock, NULL);
BalanceSheet_Init();
blockchain_t* chain = Chain_Create();
if (!chain) {
@@ -531,9 +535,6 @@ int main(int argc, char* argv[]) {
}
uint8_t lastSavedHash[32] = {0};
// Initialize runtime locks before starting modules
pthread_rwlock_init(&chainLock, NULL);
pthread_mutex_init(&balanceSheetLock, NULL);
if (!Chain_LoadFromFile(chain, chainDataDir, &currentSupply, &difficultyTarget, &currentReward, lastSavedHash, false)) {
printf("No existing chain loaded from %s\n", chainDataDir);
}
@@ -595,7 +596,7 @@ int main(int argc, char* argv[]) {
char supplyStr[80];
Uint256ToDecimal(&currentSupply, supplyStr, sizeof(supplyStr));
printf("Current chain has %zu blocks, total supply %s\n", Chain_Size(chain), supplyStr);
printf("Commands: mine <x>, send <address> <amount>, balance [address], connect <ipv4>, flushchain, fullverify, blockdetail <block number>, wipechain, genaddr, exit\n");
printf("Commands: mine <x>, send <address> <amount>, balance [address], connect <ipv4>, sync (requires nodes), flushchain, fullverify, blockdetail <block number>, wipechain, genaddr, exit\n");
char line[1024];
while (true) {