-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticketlock.c
82 lines (60 loc) · 1.3 KB
/
ticketlock.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Mutual exclusion spin locks.
#include "types.h"
#include "defs.h"
#include "param.h"
#include "x86.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "spinlock.h"
#include "ticketlock.h"
void
initlock_t(struct ticketlock *lk)
{
lk->proc = 0;
lk->ticket = 0;
lk->turn = 0;
lk->locked=0;
}
// Acquire the lock.
void
acquire_t(struct ticketlock *lk)
{
//uint ticket;
//pushcli(); // disable interrupts to avoid deadlock.
// pushcli();
if(holding_t(lk))
panic("acquire");
//ticket = fetch_and_add(&lk->ticket, 1);
myproc()->ticket = fetch_and_add(&lk->ticket, 1);
//while(lk->turn != myproc()->ticket);
while(xchg(&lk->locked, 1) != 0);
lk->proc=myproc();
// Record info about lock acquisition for debugging.
//lk->cpu = mycpu();
lk->proc = myproc();
getcallerpcs(&lk, lk->pcs);
// getcallerpcs(&lk, lk->pcs);
}
// Release the lock.
void
release_t(struct ticketlock *lk)
{
if(!holding_t(lk))
panic("release");
lk->pcs[0] = 0;
lk->proc = 0;
lk->cpu = 0;
lk->turn++; //fetch_and_add(&lk->turn, 1);
lk->locked=0;
//wakeup(lk);
//resetpriority();
//popcli();
}
// Check whether this cpu is holding the lock.
int
holding_t(struct ticketlock *lock)
{
return (lock->proc == myproc());
//return (lock->proc == myproc());
}