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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::errno::*;
use crate::synch::semaphore::Semaphore;
use alloc::boxed::Box;
fn __sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 {
if sem.is_null() {
return -EINVAL;
}
let boxed_semaphore = Box::new(Semaphore::new(value as isize));
unsafe {
*sem = Box::into_raw(boxed_semaphore);
}
0
}
#[no_mangle]
pub extern "C" fn sys_sem_init(sem: *mut *mut Semaphore, value: u32) -> i32 {
kernel_function!(__sys_sem_init(sem, value))
}
fn __sys_sem_destroy(sem: *mut Semaphore) -> i32 {
if sem.is_null() {
return -EINVAL;
}
unsafe {
Box::from_raw(sem);
}
0
}
#[no_mangle]
pub extern "C" fn sys_sem_destroy(sem: *mut Semaphore) -> i32 {
kernel_function!(__sys_sem_destroy(sem))
}
fn __sys_sem_post(sem: *const Semaphore) -> i32 {
if sem.is_null() {
return -EINVAL;
}
let semaphore = unsafe { &*sem };
semaphore.release();
0
}
#[no_mangle]
pub extern "C" fn sys_sem_post(sem: *const Semaphore) -> i32 {
kernel_function!(__sys_sem_post(sem))
}
fn __sys_sem_trywait(sem: *const Semaphore) -> i32 {
if sem.is_null() {
return -EINVAL;
}
let semaphore = unsafe { &*sem };
if semaphore.try_acquire() {
0
} else {
-ECANCELED
}
}
#[no_mangle]
pub extern "C" fn sys_sem_trywait(sem: *const Semaphore) -> i32 {
kernel_function!(__sys_sem_trywait(sem))
}
fn __sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
if sem.is_null() {
return -EINVAL;
}
let delay = if ms > 0 { Some(u64::from(ms)) } else { None };
let semaphore = unsafe { &*sem };
if semaphore.acquire(delay) {
0
} else {
-ETIME
}
}
#[no_mangle]
pub extern "C" fn sys_sem_timedwait(sem: *const Semaphore, ms: u32) -> i32 {
kernel_function!(__sys_sem_timedwait(sem, ms))
}
fn __sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 {
sys_sem_timedwait(sem, ms)
}
#[no_mangle]
pub extern "C" fn sys_sem_cancelablewait(sem: *const Semaphore, ms: u32) -> i32 {
kernel_function!(__sys_sem_cancelablewait(sem, ms))
}