Skip to content

Commit 223d257

Browse files
committed
optimize pthread_cond_timedwait
1 parent 900d52a commit 223d257

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn main() {
6767
fn main() {
6868
_ = open_coroutine::task!(|param| {
6969
assert_eq!(param, "param");
70-
}, "param", 1/*priority*/);
70+
}, "param", 1/*the smaller the value, the higher the priority*/);
7171
}
7272
```
7373

README_ZH.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn main() {
6767
fn main() {
6868
_ = open_coroutine::task!(|param| {
6969
assert_eq!(param, "param");
70-
}, "param", 1);
70+
}, "param", 1/*数值越小,优先级越高*/);
7171
}
7272
```
7373

core/src/syscall/unix/pthread_cond_timedwait.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
6060
lock: *mut pthread_mutex_t,
6161
abstime: *const timespec,
6262
) -> c_int {
63+
fn wait_time(left_time: u64) -> u64 {
64+
if left_time > 10_000_000 {
65+
10_000_000
66+
} else {
67+
left_time
68+
}
69+
}
70+
6371
#[cfg(all(unix, feature = "preemptive"))]
6472
if crate::monitor::Monitor::current().is_some() {
6573
return self.inner.pthread_cond_timedwait(
@@ -87,13 +95,14 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
8795
if 0 == left_time {
8896
return libc::ETIMEDOUT;
8997
}
98+
let next_timeout = now().saturating_add(wait_time(left_time));
9099
let r = self.inner.pthread_cond_timedwait(
91100
fn_ptr,
92101
cond,
93102
lock,
94103
&timespec {
95-
tv_sec: now().saturating_div(1_000_000_000).saturating_add(1).try_into().expect("overflow"),
96-
tv_nsec: 0,
104+
tv_sec: next_timeout.saturating_div(1_000_000_000).try_into().expect("overflow"),
105+
tv_nsec: next_timeout.wrapping_rem(1_000_000_000).try_into().expect("overflow"),
97106
},
98107
);
99108
if libc::ETIMEDOUT != r {
@@ -103,14 +112,10 @@ impl<I: PthreadCondTimedwaitSyscall> PthreadCondTimedwaitSyscall
103112
if 0 == left_time {
104113
return libc::ETIMEDOUT;
105114
}
106-
let wait_time = if left_time > 10_000_000 {
107-
10_000_000
108-
} else {
109-
left_time
110-
};
115+
let wait_time = wait_time(left_time);
111116
if EventLoops::wait_event(Some(Duration::new(
112117
wait_time / 1_000_000_000,
113-
(wait_time % 1_000_000_000) as _,
118+
wait_time.wrapping_rem(1_000_000_000).try_into().expect("overflow"),
114119
)))
115120
.is_err()
116121
{

0 commit comments

Comments
 (0)