Skip to content

Commit

Permalink
edit pool
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Feb 22, 2022
1 parent d875dba commit 0b66bb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[package]
name = "mco"
version = "0.1.42"
version = "0.1.43"
edition = "2018"
authors = ["[email protected]", "Xudong Huang <[email protected]>"]
license = "MIT/Apache-2.0"
Expand Down
32 changes: 14 additions & 18 deletions src/std/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::RefCell;
use std::mem::take;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::sync::mpsc::RecvError;
use std::sync::mpsc::{RecvError, SendError};
use crate::coroutine::spawn;
use crate::std::errors::Error;
use crate::std::sync::{Receiver, Sender};
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Pool {
}

pub fn run(&self) {
let mut current = Arc::new(AtomicI32::new(self.worker_num));
let mut current = Arc::new(chan!(self.worker_num as usize));
loop {
match self.idle.1.recv() {
Ok(mut task) => {
Expand All @@ -82,22 +82,18 @@ impl Pool {
break;
}
Some(task) => {
let n = current.load(Ordering::SeqCst);
if n != 0 {
current.store(n - 1, Ordering::SeqCst);
let c = current.clone();
spawn(move || {
defer!(||{
let n = c.load(Ordering::SeqCst);
c.store(n+1,Ordering::SeqCst);
});
let r = task.execute();
if r.is_err() {
log::error!("task run fail:{}",r.err().unwrap());
}
});
} else {
self.idle.0.send(Some(task));
match current.0.send(()) {
Ok(_) => {
let rv = current.1.clone();
spawn(move || {
defer!(move ||{ rv.try_recv(); });
let r = task.execute();
if r.is_err() {
log::error!("task run fail:{}",r.err().unwrap());
}
});
}
Err(_) => {}
}
}
}
Expand Down

0 comments on commit 0b66bb3

Please sign in to comment.