-
Notifications
You must be signed in to change notification settings - Fork 199
/
mutex.asm
69 lines (60 loc) · 1.61 KB
/
mutex.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
;asmttpd - Web server for Linux written in amd64 assembly.
;Copyright (C) 2014 nemasu <[email protected]>
;
;This file is part of asmttpd.
;
;asmttpd is free software: you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation, either version 2 of the License, or
;(at your option) any later version.
;
;asmttpd is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.
mutex_leave:
stackpush
mov rax, 1
mov rdi, 0
lock cmpxchg [queue_futex], rdi ; 1 -> 0 case
cmp rax, 1
je mutex_leave_end
mov QWORD [queue_futex], 0
mov rdi, 1
call sys_futex_wake
mutex_leave_end:
stackpop
ret
mutex_enter:
stackpush
mov rax, 0
mov rdi, 1
lock cmpxchg [queue_futex], rdi
cmp rax, 0
je mutex_enter_end
mutex_enter_begin:
cmp rax, 2
je mutex_enter_wait
mov rax, 1
mov rdi, 2
lock cmpxchg [queue_futex], rdi
cmp rax, 1
je mutex_enter_wait
;Both tries ( c == 2 || cmpxchg( 1, 2 ) ) failed
mutex_enter_cont:
mov rax, 0
mov rdi, 2
lock cmpxchg [queue_futex], rdi
cmp rax, 0
jne mutex_enter_begin
jmp mutex_enter_end
mutex_enter_wait:
mov rdi, 2
call sys_futex_wait
jmp mutex_enter_cont
mutex_enter_end:
stackpop
ret