I've been messing around with a custom boot loader for my Raspberry Pi 3B. I've found that, if I use a static variable in a function, all cores share the same value for that static variable.
Here's the code I'm running on each core. It's based on something found online:
void kernel_main(unsigned int processor_index)
{
static unsigned int current_processor_index = 0;
if (processor_index == 0) {
uart_init();
}
while (processor_index != current_processor_index);
uart_send_string("Hi from processor ");
uart_send(processor_index + '0');
uart_send_string("!\r\n");
current_processor_index++;
if (processor_index == 0) {
// if current_processor_index == 4 then all processors send message
while (current_processor_index != 4)
;
for (;;) {
uart_send(uart_recv());
}
}
}
Each core hangs until the static current_processor_index
takes the value of the processor_index
. Since the RPi3 has 4 cores, the output is:
Hi from processor 0!
Hi from processor 1!
Hi from processor 2!
Hi from processor 3!
Why does this work? I'm struggling to find answers online. Thanks!