queuingtask/
lib.rs

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
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use std::sync::PoisonError;
use std::sync::mpsc;
use std::sync::mpsc::Sender;
use std::thread::JoinHandle;
use std::collections::HashMap;
use std::num::Wrapping;

#[derive(Clone, Copy, Eq, PartialOrd, PartialEq, Debug)]
pub enum Notify {
	Started(u32),
	Go,
	Terminated(u32),
}
pub struct ThreadQueue {
	sender_map:Arc<Mutex<HashMap<u32,Sender<Notify>>>>,
	sender:Option<Sender<Notify>>,
	current_id:Arc<Mutex<u32>>,
	last_id:Arc<Mutex<u32>>,
}
impl ThreadQueue {
	pub fn new() -> ThreadQueue {
		ThreadQueue {
			sender_map:Arc::new(Mutex::new(HashMap::new())),
			sender:None,
			current_id:Arc::new(Mutex::new(0)),
			last_id:Arc::new(Mutex::new(0)),
		}
	}

	pub fn submit<F,T>(&mut self,f:F) ->
		Result<JoinHandle<T>,PoisonError<MutexGuard<HashMap<u32,Sender<Notify>>>>>
		where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static {

		if self.sender_map.lock()?.len() == 0 {
			let (ss,sr) = mpsc::channel();
			let current_id = self.current_id.clone();
			let last_id = self.last_id.clone();
			let sender_map = self.sender_map.clone();

			std::thread::spawn(move || {
				while *last_id.lock().unwrap() == *current_id.lock().unwrap() {
					std::thread::sleep(std::time::Duration::from_millis(1));
				}
				while sender_map.lock().unwrap().len() > 0 {
					match sr.recv().unwrap() {
						Notify::Started(id) => {
							match current_id.lock() {
								Ok(ref mut current_id) if id == **current_id => {
									sender_map.lock()
										.unwrap().get(&id)
										.unwrap().send(Notify::Go).unwrap();
								},
								Ok(_) => (),
								Err(ref e) => {
									panic!(format!("{:?}",e));
								}
							}
						},
						Notify::Terminated(id) => {
							match current_id.lock() {
								Ok(ref mut current_id) if id == **current_id => {
									match sender_map.lock() {
										Ok(mut map) => {
											map.remove(&id);
											let id = Wrapping(id) + Wrapping(1);
											let id = id.0;
											**current_id = id;
											match map.get(&id) {
												Some(ref sender) => {
													sender.send(Notify::Go).unwrap();
												},
												None => ()
											}
										},
										Err(ref e) => {
											panic!(format!("{:?}",e));
										}
									};
								},
								Ok(_) => (),
								Err(ref e) => {
									panic!(format!("{:?}",e));
								}
							}
						},
						_ => (),
					}
				}
			});
			self.sender = Some(ss)
		}

		match self.last_id.lock() {
			Ok(ref mut last_id) => {
				let (cs,cr) = mpsc::channel();

				self.sender_map.lock()?.insert(**last_id, cs.clone());

				let ss = match self.sender {
					Some(ref ss) => ss.clone(),
					None => panic!("Sender is not initialized."),
				};

				let id = **last_id;
				let next_id = Wrapping(**last_id) + Wrapping(1);

				**last_id = next_id.0;

				let r = std::thread::spawn(move || {
					ss.send(Notify::Started(id)).unwrap();
					cr.recv().unwrap();

					let r = f();

					ss.send(Notify::Terminated(id)).unwrap();
					r
				});
				Ok(r)
			},
			Err(ref e) => {
				panic!(format!("{:?}",e));
			}
		}
	}
}