-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.c
37 lines (31 loc) · 915 Bytes
/
lock.c
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
#include "lock.h"
#include "util.h"
#include "threads.h"
#include "io.h"
#include "interrupt.h"
void lock(struct spinlock *lock)
{
const uint16_t ticket = __sync_fetch_and_add(&lock->users, 1);
while (lock->ticket != ticket) {
barrier();
yield();
}
__sync_synchronize(); /* we don't use cmpxchg explicitly */
}
void unlock(struct spinlock *lock)
{
__sync_synchronize();
__sync_add_and_fetch(&lock->ticket, 1);
}
volatile int critical_section_depth = 0;
void start_critical_section() {
//printf("critical section start %d, current thread: %d \n", critical_section_depth, get_current_thread());
interrupt_off();
__sync_fetch_and_add(&critical_section_depth, 1);
}
void end_critical_section() {
//printf("critical section end %d\n", critical_section_depth);
if (__sync_fetch_and_add(&critical_section_depth, -1) == 1) {
interrupt_on();
}
}