SMP barrier is reusable
[akaros.git] / kern / atomic.c
1 #include <kern/atomic.h>
2 #include <kern/apic.h>
3
4 // byte per cpu, as mentioned below
5 void init_barrier_all(barrier_t* cpu_barrier)
6 {
7         extern uint8_t num_cpus;
8         uint8_t i;
9         cpu_barrier->ready = 0;
10         for(i = 0; i < num_cpus; i++)
11                 cpu_barrier->cpu_array[i] = 1;
12 }
13
14 // primitive barrier function.  all cores call this.
15 // consider changing this to use bits and lock bit ops.
16 // currently uses a byte per core, and assumes it was 
17 // initialized by a core such that num_cpus entries
18 // are all 1
19 void barrier_all(barrier_t* cpu_barrier)
20 {
21         extern uint8_t num_cpus;
22         uint8_t i;
23         uint8_t local_ready = cpu_barrier->ready;
24
25         cpu_barrier->cpu_array[lapic_get_id()] = 0;
26         if (lapic_get_id())
27                 while(cpu_barrier->ready == local_ready)
28                         cpu_relax();
29         else {
30                 for(i = 0; i < num_cpus; i++) {
31                         while(cpu_barrier->cpu_array[i]) 
32                                 cpu_relax();
33                         cpu_barrier->cpu_array[i] = 1;
34                 }
35                 // if we need to wmb(), it'll be here
36                 cpu_barrier->ready++;
37         }
38 }