Refactor CPU mutex code
This commit is contained in:
+1
-1
@@ -42,7 +42,7 @@ void exception_trigger(rv32_cpu_t* cpu, uint32_t scause, uint32_t tval)
|
||||
cpu->pc = cpu->csr[CSR_STVEC] & 0xFFFFFFFC;
|
||||
|
||||
// Unlock cpu mutex, cpu_loop will lock it just after
|
||||
pthread_mutex_unlock(&cpu0_mutex);
|
||||
pthread_mutex_unlock(&cpu->mutex);
|
||||
|
||||
// cpu loop (attribute noreturn should erase previous stack)
|
||||
cpu_loop(cpu);
|
||||
|
||||
+2
-2
@@ -33,7 +33,7 @@ void interrupt_trigger(rv32_cpu_t* cpu, uint32_t scause)
|
||||
// but we know it is not in the middle of an instruction
|
||||
// (we got the mutex) ; this way we can just change
|
||||
// registers to set interrupt handler execution
|
||||
pthread_mutex_lock(&cpu0_mutex);
|
||||
pthread_mutex_lock(&cpu->mutex);
|
||||
|
||||
// Set xCAUSE with interrupt bit set
|
||||
cpu->csr[CSR_SCAUSE] = 0x80000000 | scause;
|
||||
@@ -67,7 +67,7 @@ void interrupt_trigger(rv32_cpu_t* cpu, uint32_t scause)
|
||||
break;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&cpu0_mutex);
|
||||
pthread_mutex_unlock(&cpu->mutex);
|
||||
}
|
||||
|
||||
void interrupt_timer_thread()
|
||||
|
||||
+5
-6
@@ -11,7 +11,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
rv32_cpu_t* cpu0;
|
||||
pthread_mutex_t cpu0_mutex;
|
||||
|
||||
typedef union RAW_INSTRUCTION
|
||||
{
|
||||
@@ -43,7 +42,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);
|
||||
pthread_cond_init(&cpu0->sim_condition, 0);
|
||||
cpu0->regs.zero = 0;
|
||||
cpu0->privilege_mode = MACHINE;
|
||||
@@ -716,14 +715,14 @@ __attribute__((noreturn)) void cpu_loop(rv32_cpu_t* cpu)
|
||||
while(1)
|
||||
{
|
||||
// Aquire CPU mutex
|
||||
pthread_mutex_lock(&cpu0_mutex);
|
||||
pthread_mutex_lock(&cpu->mutex);
|
||||
|
||||
// No simulation ticks left : wakeup people waiting on sim end
|
||||
if(!cpu->sim_ticks_left)
|
||||
pthread_cond_signal(&cpu0->sim_condition);
|
||||
pthread_cond_signal(&cpu->sim_condition);
|
||||
// Then, wait for simulation state to change until we get more ticks to simulate
|
||||
while(!cpu->sim_ticks_left)
|
||||
pthread_cond_wait(&cpu0->sim_condition, &cpu0_mutex);
|
||||
pthread_cond_wait(&cpu->sim_condition, &cpu->mutex);
|
||||
|
||||
// Fetch
|
||||
raw_instruction_t raw_instruction;
|
||||
@@ -758,7 +757,7 @@ __attribute__((noreturn)) void cpu_loop(rv32_cpu_t* cpu)
|
||||
cpu->sim_ticks_left--;
|
||||
|
||||
// Let go of cpu mutex
|
||||
pthread_mutex_unlock(&cpu0_mutex);
|
||||
pthread_mutex_unlock(&cpu->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -205,13 +205,14 @@ typedef struct RV32_CPU
|
||||
ssize_t sim_ticks_left; // -1 : simulate forever
|
||||
size_t sim_ticks_done;
|
||||
pthread_cond_t sim_condition;
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
} rv32_cpu_t;
|
||||
|
||||
uint32_t csr_read(struct RV32_CPU* cpu, uint32_t csr);
|
||||
void csr_write(struct RV32_CPU* cpu, uint32_t csr, uint32_t value);
|
||||
|
||||
extern rv32_cpu_t* cpu0;
|
||||
extern pthread_mutex_t cpu0_mutex;
|
||||
void cpu_init();
|
||||
__attribute__((noreturn)) void cpu_loop(rv32_cpu_t* cpu);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user