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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#![warn(missing_docs)]

/*!
### Description

This crate provides an interface for spawning worker threads and checking
if their work has finished and produced a result.

The primary struct is the [`Employer`](struct.Employer.html), which can be used to
start new jobs and also holds the results of finished jobs.

### When should you use this?

This crate is designed for applications where you need to do some expensive
task, but the main thread can carry on just fine without that task's result.
The most common domain for this type of problem is in asset loading. This crate
allows you to start loading assets and then render/play/use them when they are
finished loading.

### Example
```
use std::{thread, time::Duration};
use employer::*;

// Create a new `Employer`
// We give it a work function that will be run on each input
let employer = Employer::new(|i: i32| {
    // Sleep to simulate a more complex computation
    thread::sleep(Duration::from_millis(100));
    2 * i + 1
});

// Start some jobs
employer.start(1);
employer.start(2);
employer.start(3);

// Each job should take about 100 ms, so if we check them
// immediately, they should still all be in progress
assert!(employer.get(&1).is_in_progress());
assert!(employer.get(&2).is_in_progress());
assert!(employer.get(&3).is_in_progress());

// Sleep the main thread to let the jobs finish
thread::sleep(Duration::from_millis(200));

// Check the results
assert_eq!(employer.get(&1).finished().unwrap(), 3);
assert_eq!(employer.get(&2).finished().unwrap(), 5);
assert_eq!(employer.get(&3).finished().unwrap(), 7);
```
*/

use std::{
    borrow::Borrow,
    cmp::Ordering as CmpOrdering,
    fmt,
    hash::Hash,
    ops::Deref,
    sync::{
        atomic::{AtomicUsize, Ordering as AtomicOrdering},
        Arc, Mutex,
    },
    thread,
};

use lockfree::{
    map::{self, Map},
    set::{self, Set},
};

/**
A description of a job

This trait defines work to be done through an
[`Employer`](struct.Employer.html).
This crate provides two implementations. A stateless one,
and a stateful one.

### Stateless Example

`JobDescription<I, Output = O>` is emplemented for all
`F` where `F: Fn(I) -> O + Send + Sync`. This is the basic
stateless implementation.
```
use employer::*;

let employer = Employer::new(|i| i + 1); // No state, just a function

employer.start(1);

assert_eq!(employer.wait_for(&1).unwrap(), 2);
```

### Stateful Example

`JobDescription<I, Output = O>` is emplemented for all
`(S, F)` where `F: Fn(&S, I) -> O + Send + Sync, S: Send + Sync`.
This is the basic stateful implementation. **State is shared
accross threads**. The function [`Employer::with_state`](struct.Employer.html#method.with_state)
makes this easier to construct.

```
use employer::*;

let employer = Employer::with_state(3, |state: &i32, i| i + *state); // A state and a function

employer.start(1);

assert_eq!(employer.wait_for(&1).unwrap(), 4);
```
*/
pub trait JobDescription<I>: Send + Sync {
    /// The job output type
    type Output;
    /// Do the job
    fn work(&self, input: I) -> Self::Output;
}

impl<F, I, O> JobDescription<I> for F
where
    F: Fn(I) -> O + Send + Sync,
{
    type Output = O;
    fn work(&self, input: I) -> Self::Output {
        self(input)
    }
}

impl<'a, F, I, O, S> JobDescription<I> for (S, F)
where
    F: Fn(&S, I) -> O + Send + Sync,
    S: Send + Sync,
{
    type Output = O;
    fn work(&self, input: I) -> Self::Output {
        (self.1)(&self.0, input)
    }
}

/// The status of a job
pub enum Job<'a, K, V> {
    /// No job exists with the given input
    None,
    /// The job is in progress
    InProgress,
    /// The job has finished
    Finished(OutputGuard<'a, K, V>),
}

impl<'a, K, V> Default for Job<'a, K, V> {
    fn default() -> Self {
        Job::None
    }
}

impl<'a, K, V> fmt::Debug for Job<'a, K, V>
where
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Job::None => write!(f, "None"),
            Job::InProgress => write!(f, "InProgress"),
            Job::Finished(g) => g.fmt(f),
        }
    }
}

impl<'a, K, V> Job<'a, K, V> {
    /// Check if the `Job` is finished
    pub fn is_finished(&self) -> bool {
        matches!(self, Job::Finished(_))
    }
    /// Check if the `Job` is in progress
    pub fn is_in_progress(&self) -> bool {
        matches!(self, Job::InProgress)
    }
    /// Check if the `Job` exists
    pub fn exists(&self) -> bool {
        !matches!(self, Job::None)
    }
    /// Get a reference to the result of the `Job` if is is finished
    pub fn as_finished(&self) -> Option<&OutputGuard<'a, K, V>> {
        if let Job::Finished(job) = self {
            Some(job)
        } else {
            None
        }
    }
    /// Convert the `Job` to an `Option<OutputGuard>`
    pub fn finished(self) -> Option<OutputGuard<'a, K, V>> {
        if let Job::Finished(job) = self {
            Some(job)
        } else {
            None
        }
    }
}

impl<'a, K, V> From<Job<'a, K, V>> for Option<OutputGuard<'a, K, V>> {
    fn from(job: Job<'a, K, V>) -> Self {
        job.finished()
    }
}

/**
An interface for spawning worker threads and storing their results

The `description` passed to `Employer::new` must implement the
[`JobDescription`](trait.JobDescription.html) trait.
*/
pub struct Employer<K, V, D> {
    locks: Arc<Map<K, Arc<Mutex<()>>>>,
    in_progress: Arc<Set<K>>,
    finished: Arc<Map<K, V>>,
    desc: Arc<D>,
    in_progress_len: Arc<AtomicUsize>,
    finished_len: Arc<AtomicUsize>,
}

impl<K, V, D> Employer<K, V, D> {
    /// Create a new `Employer` with the given job description
    pub fn new(description: D) -> Self {
        Employer {
            locks: Arc::new(Map::new()),
            in_progress: Arc::new(Set::new()),
            finished: Arc::new(Map::new()),
            desc: Arc::new(description),
            in_progress_len: Arc::new(AtomicUsize::new(0)),
            finished_len: Arc::new(AtomicUsize::new(0)),
        }
    }
}

impl<K, V, S, F> Employer<K, V, (S, F)>
where
    (S, F): JobDescription<K, Output = V>,
{
    /// Create a new `Employer` wither the given shared state and job function
    pub fn with_state(state: S, f: F) -> Self {
        Employer::new((state, f))
    }
}

impl<K, V, D> Employer<K, V, D> {
    /// Get the job with the given input
    pub fn get<'a, Q>(&'a self, input: &Q) -> Job<'a, K, V>
    where
        Q: Hash + Ord,
        K: Borrow<Q>,
    {
        if self.in_progress.contains(input) {
            Job::InProgress
        } else if let Some(rg) = self.finished.get(input) {
            Job::Finished(OutputGuard(rg))
        } else {
            Job::None
        }
    }
    /// Block the thread, wait for a job to finish, and get its result
    ///
    /// Returns `None` if a job with the given input does not exist
    pub fn wait_for<'a, Q>(&'a self, input: &Q) -> Option<OutputGuard<'a, K, V>>
    where
        Q: Hash + Ord,
        K: Borrow<Q>,
    {
        if let Some(rg) = self.locks.get(input) {
            loop {
                let done_guard = rg.val().lock().expect("Progress lock poisoned");
                if let Some(res) = self.get(input).finished() {
                    drop(done_guard);
                    break Some(res);
                } else {
                    drop(done_guard);
                }
            }
        } else {
            None
        }
    }
    /// Check if the job with the given input has finished
    pub fn finished<Q>(&self, input: &Q) -> bool
    where
        Q: Hash + Ord,
        K: Borrow<Q>,
    {
        self.get(input).is_finished()
    }
    /// Check the number of jobs that are in progress
    pub fn in_progress_len(&self) -> usize {
        self.in_progress_len.load(AtomicOrdering::Relaxed)
    }
    /// Check the number of jobs that are in progress
    pub fn finished_len(&self) -> usize {
        self.finished_len.load(AtomicOrdering::Relaxed)
    }
    /// Iterate over finished job input/output pairs
    pub fn finished_iter(&self) -> JobIter<K, V> {
        self.finished.iter()
    }
    /// Iterate over in_progress job inputs
    pub fn in_progress_inputs(&self) -> impl Iterator<Item = InputGuard<K, V>> {
        self.in_progress
            .iter()
            .map(|g| InputGuard(InputGuardInner::Set(g)))
    }
    /// Iterate over finished job inputs
    pub fn finished_inputs(&self) -> impl Iterator<Item = InputGuard<K, V>> {
        self.finished
            .iter()
            .map(|g| InputGuard(InputGuardInner::Map(g)))
    }
    /// Iterate over all job inputs
    pub fn inputs(&self) -> impl Iterator<Item = InputGuard<K, V>> {
        self.finished_inputs().chain(self.in_progress_inputs())
    }
    /// Iterate over finished job outputs
    pub fn outputs(&self) -> impl Iterator<Item = OutputGuard<K, V>> {
        self.finished.iter().map(OutputGuard)
    }
}

impl<K, V, D> Employer<K, V, D>
where
    K: Ord + Hash + Clone + Send + Sync + 'static,
    V: Send + Sync + 'static,
    D: JobDescription<K, Output = V> + 'static,
{
    fn _start(&self, input: K) {
        // Create lock
        let done_lock = Arc::new(Mutex::new(()));
        self.locks.insert(input.clone(), Arc::clone(&done_lock));
        // Add job to in_progress
        let _ = self.in_progress.insert(input.clone());
        // Increment in_progress_len
        self.in_progress_len.fetch_add(1, AtomicOrdering::Relaxed);
        // Clone Arcs for job thread
        let finished = Arc::clone(&self.finished);
        let in_progress = Arc::clone(&self.in_progress);
        let desc = Arc::clone(&self.desc);
        let in_progress_len = Arc::clone(&self.in_progress_len);
        let finished_len = Arc::clone(&self.finished_len);
        // Spawn job thread
        thread::spawn(move || {
            // Lock until done
            let done_guard = done_lock.lock().expect("Progress lock poisoned");
            // Do work
            let res = desc.work(input.clone());
            // Remove job from in_progress
            in_progress.remove(&input);
            // Decrement in_progress_len
            in_progress_len.fetch_sub(1, AtomicOrdering::Relaxed);
            // Increment finished_len
            finished_len.fetch_add(1, AtomicOrdering::Relaxed);
            // Add result to finished
            finished.insert(input, res);
            // Unlock
            drop(done_guard);
        });
    }
    /**
    Start a new job with the the given input if one with the same
    input is not in progress or finished

    If you want to start the job even if one with the same input
    is already finished, use
    [`Employer::restart`](struct.Employer.html#method.restart)
    or
    [`Employer::restart_if`](struct.Employer.html#method.restart_if).
    */
    pub fn start(&self, input: K) {
        if !self.get(&input).exists() {
            self._start(input);
        }
    }
    /**
    Start a new job with the given input if one with the same input
    is not in progress

    If you want to avoid starting a new job if one with the same
    input has already finished, use
    [`Employer::start`](struct.Employer.html#method.start).
    */
    pub fn restart(&self, input: K) {
        if !self.get(&input).is_in_progress() {
            self._start(input);
        }
    }
    /**
    Start a new job with the given input if one with the same input
    is not in progress, and if it exists and is finished, the result
    satisfies the supplied condition

    An example use case is if your job involves IO. In this case,
    your work function could return a `Result`, and the condition
    supplied to this function would be `Result::is_err`.

    If you want to avoid starting a new job if one with the same
    input has already finished, use
    [`Employer::start`](struct.Employer.html#method.start).
    */
    pub fn restart_if<G>(&self, input: K, condition: G)
    where
        G: FnOnce(&V) -> bool,
    {
        let restart = match self.get(&input) {
            Job::None => true,
            Job::InProgress => false,
            Job::Finished(guard) => condition(&*guard),
        };
        if restart {
            self._start(input)
        }
    }
    /// Remove a finished job with the given input
    pub fn remove<Q>(&self, input: &Q)
    where
        Q: Hash + Ord,
        K: Borrow<Q>,
    {
        if self.finished.remove(input).is_some() {
            self.locks.remove(input);
            self.finished_len.fetch_sub(1, AtomicOrdering::Relaxed);
        }
    }
}

impl<K, V, D> fmt::Debug for Employer<K, V, D> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Employer")
            .field("in progress", &self.in_progress_len())
            .field("finished", &self.finished)
            .finish()
    }
}

impl<K, V, D> Default for Employer<K, V, D>
where
    D: Default,
{
    fn default() -> Self {
        Employer::new(D::default())
    }
}

impl<K, V, D> From<Employer<K, V, D>> for Arc<D> {
    fn from(employer: Employer<K, V, D>) -> Self {
        employer.desc
    }
}

impl<K, V, D> From<D> for Employer<K, V, D> {
    fn from(desc: D) -> Self {
        Employer::new(desc)
    }
}

/// A guard to finished job input/output pairs
pub type JobGuard<'a, K, V> = lockfree::map::ReadGuard<'a, K, V>;
/// An iterator over guards to finished job input/output pairs
pub type JobIter<'a, K, V> = lockfree::map::Iter<'a, K, V>;

impl<'a, K, V, H> IntoIterator for &'a Employer<K, V, H> {
    type Item = JobGuard<'a, K, V>;
    type IntoIter = JobIter<'a, K, V>;
    fn into_iter(self) -> Self::IntoIter {
        self.finished.iter()
    }
}

/// A guard to the result of a finished job
pub struct OutputGuard<'a, K, V>(map::ReadGuard<'a, K, V>);

impl<'a, K, V> Deref for OutputGuard<'a, K, V> {
    type Target = V;
    fn deref(&self) -> &Self::Target {
        self.0.val()
    }
}

impl<'a, K, V, T> PartialEq<T> for OutputGuard<'a, K, V>
where
    V: PartialEq<T>,
{
    fn eq(&self, other: &T) -> bool {
        (**self).eq(other)
    }
}

impl<'a, K, V, T> PartialOrd<T> for OutputGuard<'a, K, V>
where
    V: PartialOrd<T>,
{
    fn partial_cmp(&self, other: &T) -> Option<CmpOrdering> {
        (**self).partial_cmp(other)
    }
}

impl<'a, K, V> fmt::Debug for OutputGuard<'a, K, V>
where
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <V as fmt::Debug>::fmt(self, f)
    }
}

impl<'a, K, V> fmt::Display for OutputGuard<'a, K, V>
where
    V: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <V as fmt::Display>::fmt(self, f)
    }
}

enum InputGuardInner<'a, K, V> {
    Map(map::ReadGuard<'a, K, V>),
    Set(set::ReadGuard<'a, K>),
}

/// A guard to the input of a job
pub struct InputGuard<'a, K, V>(InputGuardInner<'a, K, V>);

impl<'a, K, V> Deref for InputGuard<'a, K, V> {
    type Target = K;
    fn deref(&self) -> &Self::Target {
        match &self.0 {
            InputGuardInner::Map(g) => g.key(),
            InputGuardInner::Set(g) => &*g,
        }
    }
}

impl<'a, K, V, T> PartialEq<T> for InputGuard<'a, K, V>
where
    K: PartialEq<T>,
{
    fn eq(&self, other: &T) -> bool {
        (**self).eq(other)
    }
}

impl<'a, K, V, T> PartialOrd<T> for InputGuard<'a, K, V>
where
    K: PartialOrd<T>,
{
    fn partial_cmp(&self, other: &T) -> Option<CmpOrdering> {
        (**self).partial_cmp(other)
    }
}

impl<'a, K, V> fmt::Debug for InputGuard<'a, K, V>
where
    K: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <K as fmt::Debug>::fmt(self, f)
    }
}

impl<'a, K, V> fmt::Display for InputGuard<'a, K, V>
where
    K: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <K as fmt::Display>::fmt(self, f)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::time::Duration;
    #[test]
    fn wait_for() {
        let employer = Employer::new(|i| {
            thread::sleep(Duration::from_millis(200));
            2 * i + 1
        });
        employer.start(1);
        employer.start(2);
        employer.start(3);
        assert_eq!(employer.wait_for(&1).unwrap(), 3);
        assert_eq!(employer.wait_for(&2).unwrap(), 5);
        assert_eq!(employer.wait_for(&3).unwrap(), 7);
    }
    #[test]
    fn state() {
        let employer = Employer::new((3, |state: &i32, i| i + *state));
        employer.start(1);
        assert_eq!(employer.wait_for(&1).unwrap(), 4);
    }
}