Skip to content

Commit 4918ed8

Browse files
committed
code polish
1 parent 561b7e5 commit 4918ed8

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

core/src/co_pool/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ impl<'p> CoroutinePool<'p> {
197197
assert_eq!(PoolState::Running, self.stopping()?);
198198
_ = self.try_timed_schedule_task(dur)?;
199199
assert_eq!(PoolState::Stopping, self.stopped()?);
200-
Ok(())
201200
}
202-
PoolState::Stopping => Err(Error::new(ErrorKind::Other, "should never happens")),
203-
PoolState::Stopped => Ok(()),
201+
PoolState::Stopping => {
202+
_ = self.try_timed_schedule_task(dur)?;
203+
assert_eq!(PoolState::Stopping, self.stopped()?);
204+
}
205+
PoolState::Stopped => {}
204206
}
207+
Ok(())
205208
}
206209

207210
/// Submit a new task to this pool.

core/src/net/event_loop.rs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -399,49 +399,62 @@ impl<'e> EventLoop<'e> {
399399
match self.state() {
400400
PoolState::Running => {
401401
assert_eq!(PoolState::Running, self.stopping()?);
402-
let timeout_time = crate::common::get_timeout_time(wait_time);
403-
loop {
404-
let left_time = timeout_time.saturating_sub(crate::common::now());
405-
if 0 == left_time {
406-
return Err(Error::new(ErrorKind::TimedOut, "stop timeout !"));
407-
}
408-
self.wait_event(Some(Duration::from_nanos(left_time).min(SLICE)))?;
409-
if self.is_empty() && self.get_running_size() == 0 {
410-
assert_eq!(PoolState::Stopping, self.stopped()?);
411-
return Ok(());
412-
}
413-
}
402+
self.do_stop_sync(wait_time)
414403
}
415-
PoolState::Stopping => Err(Error::new(ErrorKind::Other, "should never happens")),
404+
PoolState::Stopping => self.do_stop_sync(wait_time),
416405
PoolState::Stopped => Ok(()),
417406
}
418407
}
419408

409+
fn do_stop_sync(&mut self, wait_time: Duration) -> std::io::Result<()> {
410+
let timeout_time = crate::common::get_timeout_time(wait_time);
411+
loop {
412+
let left_time = timeout_time.saturating_sub(crate::common::now());
413+
if 0 == left_time {
414+
return Err(Error::new(ErrorKind::TimedOut, "stop timeout !"));
415+
}
416+
self.wait_event(Some(Duration::from_nanos(left_time).min(SLICE)))?;
417+
if self.is_empty() && self.get_running_size() == 0 {
418+
assert_eq!(PoolState::Stopping, self.stopped()?);
419+
return Ok(());
420+
}
421+
}
422+
}
423+
420424
pub(super) fn stop(&self, wait_time: Duration) -> std::io::Result<()> {
421425
match self.state() {
422426
PoolState::Running => {
423427
if BeanFactory::remove_bean::<JoinHandle<()>>(&self.get_thread_name()).is_some() {
424428
assert_eq!(PoolState::Running, self.stopping()?);
425-
//开启了单独的线程
426-
let (lock, cvar) = &*self.stop;
427-
let result = cvar
428-
.wait_timeout_while(
429-
lock.lock().expect("lock failed"),
430-
wait_time,
431-
|&mut pending| pending,
432-
)
433-
.expect("lock failed");
434-
if result.1.timed_out() {
435-
return Err(Error::new(ErrorKind::TimedOut, "stop timeout !"));
436-
}
437-
assert_eq!(PoolState::Stopping, self.stopped()?);
429+
return self.do_stop(wait_time);
438430
}
439-
Ok(())
431+
Err(Error::new(
432+
ErrorKind::Unsupported,
433+
"use EventLoop::stop_sync instead !",
434+
))
440435
}
441-
PoolState::Stopping => Err(Error::new(ErrorKind::Other, "should never happens")),
436+
PoolState::Stopping => self.do_stop(wait_time),
442437
PoolState::Stopped => Ok(()),
443438
}
444439
}
440+
441+
fn do_stop(&self, wait_time: Duration) -> std::io::Result<()> {
442+
//开启了单独的线程
443+
let (lock, cvar) = &*self.stop;
444+
let result = cvar
445+
.wait_timeout_while(
446+
lock.lock().expect("lock failed"),
447+
wait_time,
448+
|&mut pending| pending,
449+
)
450+
.expect("lock failed");
451+
if result.1.timed_out() {
452+
return Err(Error::new(ErrorKind::TimedOut, "stop timeout !"));
453+
}
454+
assert_eq!(PoolState::Stopping, self.stopped()?);
455+
assert!(BeanFactory::remove_bean::<Self>(self.name()).is_some());
456+
Ok(())
457+
}
445458
}
446459

447460
impl_current_for!(EVENT_LOOP, EventLoop<'e>);

0 commit comments

Comments
 (0)