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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! A set of utilities to abstract tasks execution.
//!
//! Tasks can be executed in the same thread, multiple threads (pool) or you can
//! control when and in which thread the tasks are consumed.
//!
#![feature(fnbox)]
#![warn(missing_docs)]

extern crate eventual;

use self::eventual::Complete;
pub use self::eventual::{Future, Async};

use std::boxed::FnBox;
use std::cmp::{Ord, Ordering};
use std::collections::binary_heap::BinaryHeap;
use std::marker::PhantomData;
use std::sync::{atomic, Arc, Condvar, Mutex};
use std::thread::{JoinHandle, spawn};

#[macro_export]
macro_rules! trivial_ordering {
	($t:ident by $cmp:expr) => {
		impl Eq for $t {}
		impl PartialEq for $t {
			fn eq(&self, other: &$t) -> bool {
				self.cmp(other) == Ordering::Equal
			}
		}
		impl Ord for $t {
			fn cmp(&self, other: &$t) -> Ordering {
				$cmp(self, other)
			}
		}
		impl PartialOrd for $t {
			fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
				Some(self.cmp(other))
			}
		}
	}
}

/// Some work to be done in a thread pool
pub trait Task: Send + 'static {
	/// Task result
	type Result: Send + 'static;
	/// Task error
	type Error: Send + 'static;

	/// Actual computation to be done
	fn call(self) -> Result<Self::Result, Self::Error>;
}

/// Abstraction over task execution
pub trait Executor<T : Task> {
	/// Returns number of elements waiting in queue
	fn queued(&self) -> usize;
	/// Add new task to be executed. Lower value in `priority` means faster execution. Returns a `Future` result.
	fn execute_with_priority(&self, task: T, priority: usize) -> Future<T::Result, T::Error>;
	/// Add new task to be executed with default priority `std::usize::MAX / 2`
	fn execute(&self, task: T) -> Future<T::Result, T::Error> {
		self.execute_with_priority(task, (!0 as usize) / 2)
	}

	/// Clear any pending tasks
	fn clear(&self);
}

/// PriorityQueue of tasks waiting for execution
pub trait TaskQueue<T: Task>: Send + 'static {
	/// Add new element to this queue
	fn push(&mut self, elem: TaskQueueElement<T>);
	/// Try to get `Task` that should be executed next.
	/// Returns `None` if no more work is waiting in queue.
	fn try_next(&mut self) -> Option<TaskQueueElement<T>>;
	/// Returns the size of the queue
	fn len(&self) -> usize;
	/// Removes all elements from the queue
	fn clear(&mut self);
	/// Returns true if there are no items in queue
	fn is_empty(&self) -> bool {
		self.len() == 0
	}
}

/// Single element inside a `TaskQueue`. Represents
pub struct TaskQueueElement<T: Task> {
	/// Task
	task: T,
	/// Priority
	priority: usize,
	/// Task fulfillment or rejection handler
	complete: Complete<T::Result, T::Error>
}

/// Type for tasks that are just closures
pub struct ClosureTask<R, E>
	where R: Send + 'static,
		  E: Send + 'static {
	closure: Box<FnBox() -> Result<R, E> + Send>,
}

// Implementations
/// Utility structure to create possible executors.
pub struct Executors;
impl Executors {

	/// Creates new `Task` from a closure.
	///
	/// Essential to have type correctness without implementing `Task` trait on ones own.
	pub fn task<R, E, F>(closure: F) -> ClosureTask<R, E>
		where F: FnBox() -> Result<R, E> + Send + 'static,
			  R: Send + 'static,
			  E: Send + 'static {
		ClosureTask {
			closure: Box::new(closure)
		}
	}

	/// Create executor that runs tasks synchronously.
	pub fn same_thread() -> SameThreadExecutor {
		SameThreadExecutor
	}

	/// Create executor that uses thread pool and priority queue to run tasks
	pub fn thread_pool<T>(threads: usize) -> ThreadPoolExecutor<T, BinaryHeap<TaskQueueElement<T>>>
		where T: Task {
			let queue = BinaryHeap::new();
			ThreadPoolExecutor::new(threads, queue)
		}

	/// Create executor with manual control over when the tasks are executed
	pub fn manual<T>() -> ManualExecutor<T, BinaryHeap<TaskQueueElement<T>>>
		where T: Task {
			let queue = BinaryHeap::new();
			ManualExecutor::new(queue)
		}

}

/// Executor with thread pool
pub struct ThreadPoolExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {

	wait: Arc<Condvar>,
	finished: Arc<atomic::AtomicBool>,
	queue: Arc<Mutex<Queue>>,
	threads: Vec<JoinHandle<()>>,
	_task: PhantomData<T>
}

impl<T, Queue> ThreadPoolExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {

	/// Creates new `ThreadPoolExecutor` with specified number of threads in pool and custom `Queue` implementation.
	fn new(threads_num: usize, queue: Queue) -> ThreadPoolExecutor<T, Queue> {
		assert!(threads_num > 0);

		let wait = Arc::new(Condvar::new());
		let finished = Arc::new(atomic::AtomicBool::new(false));
		let queue = Arc::new(Mutex::new(queue));
		let mut threads = vec![];

		for _i in 0..threads_num {
			let wait = wait.clone();
			let finished = finished.clone();
			let queue = queue.clone();
			threads.push(spawn(move || ThreadPoolExecutor::worker(wait, finished, queue)));
		}

		ThreadPoolExecutor {
			wait: wait,
			finished: finished,
			queue: queue,
			threads: threads,
			_task: PhantomData
		}
	}

	fn worker(wait: Arc<Condvar>, finished: Arc<atomic::AtomicBool>, queue: Arc<Mutex<Queue>>) {
		loop {
			// Initial check if not finished
			if finished.load(atomic::Ordering::Relaxed) {
				return;
			}

			let mut queue = queue.lock().unwrap();
			// Do work if any
			if let Some(work) = queue.try_next() {
				let result = work.consume();
				match result {
					(complete, Ok(res)) => complete.complete(res),
					(complete, Err(err)) => complete.fail(err)
				}
			}

			// Thread might be finished by the time we get here
			if finished.load(atomic::Ordering::Relaxed) {
				return;
			}

			// Wait for some more work
			let _ = wait.wait(queue).unwrap();
		}
	}
}

impl<T, Queue> Drop for ThreadPoolExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {
	fn drop(&mut self) {
		self.finished.store(true, atomic::Ordering::Relaxed);
		self.wait.notify_all();

		for thread in self.threads.drain(..) {
			thread.join().unwrap();
		}
	}
}

impl<T, Queue> Executor<T> for ThreadPoolExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {

	fn queued(&self) -> usize {
		let q = self.queue.lock().unwrap();
		q.len()
	}

	fn clear(&self) {
		let mut q = self.queue.lock().unwrap();
		q.clear();
	}

	fn execute_with_priority(&self, task: T, priority: usize) -> Future<T::Result, T::Error> {
		let (complete, future) = Future::pair();
		let mut q = self.queue.lock().unwrap();
		q.push(TaskQueueElement {
			task: task,
			priority: priority,
			complete: complete,
		});
		self.wait.notify_one();
		future
	}
}

/// Synchronous executor implementation
pub struct SameThreadExecutor;
impl<T: Task> Executor<T> for SameThreadExecutor {

	fn queued(&self) -> usize {
		// Everything is run synchronously
		0
	}

	fn clear(&self) {
		// There is no queue
	}

	fn execute_with_priority(&self, task: T, _priority: usize) -> Future<T::Result, T::Error> {
		task.call()
			.map(Future::of)
			.unwrap_or_else(Future::error)
	}
}

/// Manual executor. You can add tasks to it but you need to manually specify when the tasks should be invoked.
pub struct ManualExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {
	queue: Mutex<Queue>,
	_task: PhantomData<T>
}

impl<T, Queue> ManualExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {

	/// Returns new `ManualExecutor` with custom `Queue` implementation
	fn new(queue: Queue) -> ManualExecutor<T, Queue> {
		ManualExecutor {
			queue: Mutex::new(queue),
			_task: PhantomData
		}
	}

	/// Execute specified number of tasks from queue.
	///
	/// Panics when requested to consume more than there is in queue.
	pub fn consume(&self, amount: usize) {
		let mut queue = self.queue.lock().unwrap();
		for _i in 0..amount {
			let work = queue.try_next().expect("Not enough items to consume.");
			let result = work.consume();
			match result {
				(complete, Ok(res)) => complete.complete(res),
				(complete, Err(err)) => complete.fail(err)
			}
		}
	}
}
impl<T, Queue> Executor<T> for ManualExecutor<T, Queue>
	where T: Task, Queue: TaskQueue<T> {

	fn queued(&self) -> usize {
		let q = self.queue.lock().unwrap();
		q.len()
	}

	fn clear(&self) {
		let mut q = self.queue.lock().unwrap();
		q.clear();
	}

	fn execute_with_priority(&self, task: T, priority: usize) -> Future<T::Result, T::Error> {
		let (complete, future) = Future::pair();
		let mut q = self.queue.lock().unwrap();
		q.push(TaskQueueElement {
			task: task,
			priority: priority,
			complete: complete,
		});
		future
	}
}

// ClosureTask
impl<R, E> Task for ClosureTask<R, E>
	where R: Send + 'static,
		  E: Send + 'static {
	type Result = R;
	type Error = E;

	fn call(self) -> Result<Self::Result, Self::Error> {
		(self.closure)()
	}
}

// TaskQueueElement ordering
// TODO [todr] This implementation is only valid for structures that allows duplicated elements.
// T1=T2 if T1.priority = T2.priority
impl<T: Task> TaskQueueElement<T> {
	fn consume(self) -> (Complete<T::Result, T::Error>, Result<T::Result, T::Error>) {
		(self.complete, self.task.call())
	}
}
impl<T: Task> Ord for TaskQueueElement<T> {
	fn cmp(&self, other: &TaskQueueElement<T>) -> Ordering {
		self.priority.cmp(&other.priority)
	}
}
impl<T: Task> PartialOrd for TaskQueueElement<T> {
	fn partial_cmp(&self, other: &TaskQueueElement<T>) -> Option<Ordering> {
		self.priority.partial_cmp(&other.priority)
	}
}
impl<T: Task> Eq for TaskQueueElement<T> {}
impl<T: Task> PartialEq for TaskQueueElement<T> {
	fn eq(&self, other: &TaskQueueElement<T>) -> bool {
		self.priority.eq(&other.priority)
	}
}

// TaskQueue implementation for BinaryHeap
impl<T: Task> TaskQueue<T> for BinaryHeap<TaskQueueElement<T>> {
	fn push(&mut self, elem: TaskQueueElement<T>) {
		BinaryHeap::push(self, elem);
	}

	fn try_next(&mut self) -> Option<TaskQueueElement<T>> {
		self.pop()
	}

	fn clear(&mut self) {
		BinaryHeap::clear(self)
	}

	fn len(&self) -> usize {
		BinaryHeap::len(self)
	}
}

#[cfg(test)]
mod test {
	use super::*;

	#[test]
	fn should_execute_task() {
		// given
		let e = Executors::same_thread();

		// when
		let future: Future<usize, ()> = e.execute(Executors::task(|| Ok(4)));

		// then
		assert_eq!(4, future.await().unwrap());
	}

	#[test]
	fn should_execute_task_in_thread_pool() {
		// given
		let e = Executors::thread_pool(1);

		// when
		let future: Future<usize, ()> = e.execute(Executors::task(|| Ok(4)));

		// then
		assert_eq!(4, future.await().unwrap());
	}

	#[test]
	fn should_execute_task_in_test_executor() {
		// given
		let e = Executors::test_executor();

		// when
		let future = e.execute(Executors::task(|| Ok(4)));
		let future2 = e.execute(Executors::task(|| Ok(5)));
		let future3 = e.execute(Executors::task(|| Err(6)));
		assert_eq!(3, e.queued());
		e.consume(3);

		// then
		assert_eq!(4, future.await().unwrap());
		assert_eq!(5, future2.await().unwrap());
		assert_eq!(6, future3.await().unwrap_err().unwrap());
	}
}