Skip to content

Commit 7e9c960

Browse files
committed
code polish
1 parent 561b7e5 commit 7e9c960

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
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: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -399,48 +399,63 @@ 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+
self.do_stop(wait_time)?;
430+
} else {
431+
return Err(Error::new(
432+
ErrorKind::Unsupported,
433+
"use EventLoop::stop_sync instead !",
434+
));
438435
}
439-
Ok(())
440436
}
441-
PoolState::Stopping => Err(Error::new(ErrorKind::Other, "should never happens")),
442-
PoolState::Stopped => Ok(()),
437+
PoolState::Stopping => self.do_stop(wait_time)?,
438+
PoolState::Stopped => {}
443439
}
440+
Ok(())
441+
}
442+
443+
fn do_stop(&self, wait_time: Duration) -> std::io::Result<()> {
444+
//开启了单独的线程
445+
let (lock, cvar) = &*self.stop;
446+
let result = cvar
447+
.wait_timeout_while(
448+
lock.lock().expect("lock failed"),
449+
wait_time,
450+
|&mut pending| pending,
451+
)
452+
.expect("lock failed");
453+
if result.1.timed_out() {
454+
return Err(Error::new(ErrorKind::TimedOut, "stop timeout !"));
455+
}
456+
assert_eq!(PoolState::Stopping, self.stopped()?);
457+
assert!(BeanFactory::remove_bean::<Self>(self.name()).is_some());
458+
Ok(())
444459
}
445460
}
446461

0 commit comments

Comments
 (0)