Make CPU and memory thread-safe

This commit is contained in:
2023-10-08 16:50:57 +02:00
parent c878dee7e0
commit f2c573bfc6
5 changed files with 29 additions and 11 deletions

View File

@@ -9,7 +9,7 @@
#include <stdio.h>
rv32_cpu_t* cpu0;
pthread_mutex_t* cpu0_mutex;
pthread_mutex_t cpu0_mutex;
typedef union RAW_INSTRUCTION
{
@@ -41,7 +41,7 @@ static void cpu_print_instruction(instruction_t* instruction);
void cpu_init()
{
cpu0 = calloc(1, sizeof(rv32_cpu_t));
pthread_mutex_init(cpu0_mutex, 0);
pthread_mutex_init(&cpu0_mutex, 0);
cpu0->regs.zero = 0;
}
@@ -647,8 +647,9 @@ void cpu_loop(rv32_cpu_t* cpu)
{
while(1)
{
// Aquire CPU mutex
pthread_mutex_lock(cpu0_mutex);
// Aquire CPU and memory mutex
pthread_mutex_lock(&cpu0_mutex);
pthread_mutex_lock(&memory_mutex);
// Fetch
raw_instruction_t raw_instruction;
@@ -674,8 +675,9 @@ void cpu_loop(rv32_cpu_t* cpu)
cpu->pc += 4;
// Let go of cpu mutex
pthread_mutex_unlock(cpu0_mutex);
// Let go of cpu and memory mutex
pthread_mutex_unlock(&cpu0_mutex);
pthread_mutex_unlock(&memory_mutex);
}
}

View File

@@ -189,7 +189,7 @@ typedef struct RV32_CPU
} rv32_cpu_t;
extern rv32_cpu_t* cpu0;
extern pthread_mutex_t* cpu0_mutex;
extern pthread_mutex_t cpu0_mutex;
void cpu_init();
void cpu_loop(rv32_cpu_t* cpu);