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
use crate::linux::*;
use core::mem;
use sgx_oc::linux::ocall;
use sgx_oc::linux::ocall::OCallError;
use sgx_trts::trts::is_within_enclave;
#[no_mangle]
pub unsafe extern "C" fn sched_yield() -> c_int {
if ocall::sched_yield().is_ok() {
0
} else {
-1
}
}
#[no_mangle]
pub unsafe extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
if rqtp.is_null() || !is_within_enclave(rqtp as *const u8, mem::size_of::<timespec>()) {
set_errno(EINVAL);
return -1;
}
if !rmtp.is_null() && !is_within_enclave(rmtp as *const u8, mem::size_of::<timespec>()) {
set_errno(EINVAL);
return -1;
}
let mut req = *rqtp;
if let Err(err) = ocall::nanosleep(&mut req) {
match err {
OCallError::OsError(e) if e == EINTR => {
if !rmtp.is_null() {
*rmtp = req;
}
}
_ => (),
}
-1
} else {
0
}
}
#[no_mangle]
pub unsafe extern "C" fn sleep(seconds: c_uint) -> c_uint {
let mut req = timespec {
tv_sec: seconds as time_t,
tv_nsec: 0,
};
if ocall::nanosleep(&mut req).is_ok() {
0
} else {
req.tv_sec as c_uint
}
}
const MICROS_PER_SEC: c_uint = 1_000_000;
const NANOS_PER_MICRO: c_uint = 1_000;
#[no_mangle]
pub unsafe extern "C" fn usleep(useconds: c_uint) -> c_int {
let mut req = timespec {
tv_sec: (useconds / MICROS_PER_SEC) as time_t,
tv_nsec: ((useconds % MICROS_PER_SEC) * NANOS_PER_MICRO) as c_long,
};
if ocall::nanosleep(&mut req).is_ok() {
0
} else {
-1
}
}