summaryrefslogtreecommitdiff
path: root/spinlock.c
blob: 911ecf820bdbbd2f71fe3923f82b7d2a5969cb02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "types.h"
#include "defs.h"
#include "x86.h"

#define LOCK_FREE -1

uint32_t kernel_lock = LOCK_FREE;

// lock = LOCK_FREE if free, else = cpu_id of owner CPU
void 
acquire_spinlock(uint32_t* lock)
{
  int cpu_id = cpu();

  if (*lock == cpu_id)
    return;
  while ( cmpxchg(LOCK_FREE, cpu_id, lock) != cpu_id ) { ; }
  // cprintf ("acquired: %d\n", cpu_id);
}

void
release_spinlock(uint32_t* lock)
{
  int cpu_id = cpu();
  // cprintf ("release: %d\n", cpu_id);
  if (*lock != cpu_id)
    panic("release_spinlock: releasing a lock that i don't own\n");
  *lock = LOCK_FREE;
}

void
release_grant_spinlock(uint32_t* lock, int c)
{
  int cpu_id = cpu();
  // cprintf ("release_grant: %d -> %d\n", cpu_id, c);
  if (*lock != cpu_id)
    panic("release_spinlock: releasing a lock that i don't own\n");
  *lock = c;
}