-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.c
More file actions
36 lines (29 loc) · 709 Bytes
/
lock.c
File metadata and controls
36 lines (29 loc) · 709 Bytes
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
//
// lock.c -- Defines functions and structures for the locking mechanism.
// Written for JamesM's kernel development tutorials.
//
#include "lock.h"
#include "common.h"
#include "thread.h"
#include "monitor.h"
#include "scheduler.h"
extern thread_t *current_thread;
static uint32_t atomic_test_and_set (volatile spinlock_t *lock) {
register spinlock_t value = SPINLOCK_UNLOCKED;
asm volatile("lock \
xchgl %0, %1"
: "=q" (value), "=m" (*lock)
: "0" (value));
return value;
}
void spinlock_lock (spinlock_t *lock)
{
while (atomic_test_and_set (lock) == SPINLOCK_LOCKED);
/*{
sleep();
}*/
}
void spinlock_unlock (spinlock_t *lock)
{
*lock = 0;
}