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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::thread;
use std::sync::mpsc::{channel, sync_channel, Sender, SyncSender};
use crate::EventType::{*};
use std::sync::{Arc, Mutex, Condvar};

pub type Runnable = Box<dyn Send + Sync + FnOnce() -> ()>;
pub type Callable<T> = Box<dyn Send + Sync + FnOnce() -> T>;

enum EventType {
  Execute(Runnable),
  //ExecuteWait(Callable<T>),
  ExecuteInner((Sender<EventType>, Runnable)),
  Quit,
}

pub struct ExecutorService {
  dispatcher: SyncSender<EventType>,
}

impl ExecutorService {
  pub fn execute(&mut self, fun: Runnable) {
    self.dispatcher.send(Execute(fun)).unwrap();
  }

  //TODO
  //pub fn submit<T>(&mut self, fun: Callable<T>) {
  //  self.dispatcher.send(Execute(fun)).unwrap();
  //}
}

impl Drop for ExecutorService {
  fn drop(&mut self) {
    self.dispatcher.send(EventType::Quit).unwrap();
  }
}


pub struct Executors;

impl Executors {
  pub fn new_fixed_thread_pool(thread_count: u32) -> ExecutorService {
    let mut guarded_count= thread_count;
    if guarded_count > 80 {
      guarded_count = 80;
    }

    let available = Arc::new(Mutex::new(vec![]));

    let (sender, receiver) = sync_channel::<EventType>(1);

    let pair = Arc::new(Condvar::new());

    for i in 0..guarded_count {
      let (s, r) = channel::<EventType>();

      if let Ok(mut lock) = available.lock() {
        lock.push(s);
      }

      let vec_clone = available.clone();
      let cv_clone = pair.clone();
      thread::Builder::new()
        .name(format!("Thread-{:}", i)).spawn(move || {
        loop {
          //println!("{:} Waiting for job", thread::current().name().unwrap());
          let fun = r.recv().unwrap();
          //println!("{:} Received func", thread::current().name().unwrap());
          match fun {
            ExecuteInner((sender, fun)) => {
              fun();
              //println!("{:} Send result", thread::current().name().unwrap());
              if let Ok(mut lock) = vec_clone.lock() {
                lock.push(sender);
                //println!("Notify waiters for finish");
                cv_clone.notify_all();
              }
            }
            Quit => {
              println!("{:} Received exit", thread::current().name().unwrap());
              break;
            }
            _ => {}
          }
        }
        //println!("{:} Loop done", thread::current().name().unwrap())
      }).unwrap();
    }

    thread::Builder::new()
      .name("Dispatcher".into())
      .spawn(move || {
        loop {
          match receiver.recv().unwrap() {
            Execute(func) => {
              if let Ok(lock) = available.lock() {
                if lock.is_empty() {
                  let _ = pair.wait(lock);
                }
              };

              if let Ok(mut lock) = available.lock() {
                //println!("Available: {:}", lock.len());
                let the_sender = lock.pop().unwrap();
                //println!("Available: {:}", lock.len());
                the_sender.send(ExecuteInner((the_sender.clone(), func))).unwrap();
              };
            }
            Quit => {
              //println!("Dispatcher received Quit");
              if let Ok(lock) = available.lock() {
                for x in &*lock {
                  println!("AV Send quit");
                  let _ = x.send(EventType::Quit);
                }
              }

              break;
            }
            _ => {}
          }
        }
        println!("Dispatcher exit");
      }).unwrap();

    ExecutorService {
      dispatcher: sender
    }
  }
}


#[cfg(test)]
mod tests {
  use std::time::Duration;
  use super::*;
  use std::thread::sleep;
  use std::thread;
  use std::sync::mpsc::{channel, sync_channel, Sender, SyncSender};
  use crate::EventType::{*};
  use std::sync::{Arc, Mutex, Condvar};

  #[test]
  fn it_works() {
    let max = 100;
    let mut executor_service = Executors::new_fixed_thread_pool(10);

    let (sender, receiver) = sync_channel(max);
    for i in 0..max {
      let moved_i = i;

      let sender2 = sender.clone();

      executor_service.execute(Box::new(move || {
        sleep(Duration::from_millis(10));
        println!("Hello from {:} {:}", thread::current().name().unwrap(), moved_i);
        sender2.send(1);
      }));
    }

    let mut latch_count = max;

    loop {
      let _ = &receiver.recv().unwrap();
      latch_count -= 1;

      if latch_count == 0 {
        break; //all threads are done
      }
    }

    println!("Done!");
  }
}