-
Notifications
You must be signed in to change notification settings - Fork 47
/
mutex16.asm
83 lines (68 loc) · 874 Bytes
/
mutex16.asm
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
83
USE16
macro qlock16 trg,del = -1
{
push ds
push di
push ecx
MOV DI,DATA16
MOV DS,DI
MOV DI,trg
dec byte [ds:di]
pop ecx
pop di
pop ds
}
macro qunlock16 trg
{
push ds
push di
MOV DI,DATA16
MOV DS,DI
MOV DI,trg
cmp byte [ds:di],0xFF
jz .unlk
inc byte [ds:di]
.unlk:
pop di
pop ds
}
qwait16:
; ax = target mutex in data16
push ds
push di
MOV DI,DATA16
MOV DS,DI
MOV DI,ax
.Loop1:
CMP byte [ds:di],0xff
JZ .OutLoop1
pause
JMP .Loop1
.OutLoop1:
pop di
pop ds
retf
qwaitlock16:
; ax = target mutex in data16
push bx
push ds
push di
MOV DI,DATA16
MOV DS,DI
MOV DI,ax
.Loop1:
CMP byte [ds:di],0xff
JZ .OutLoop1
pause
JMP .Loop1
.OutLoop1:
; Lock is free, can we grab it?
mov bl,0xfe
MOV AL,0xFF
LOCK CMPXCHG [DS:DI],bl
JNZ .Loop1 ; Write failed
.OutLoop2: ; Lock Acquired
pop di
pop ds
pop bx
retf