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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors
//

//!
//! Multi-Producer, Multi-Consumer
//!

use std::collections::VecDeque;
use std::fmt::{Debug, Formatter};
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
use std::sync::{Arc, Condvar, Mutex};

use log::trace;

use crate::TaskError;

///
/// Exchanger errors
pub enum ExchangerError<T> {
    /// Error with the underlying executor
    TaskError(TaskError),
    /// Exchanger cannot accept an element as it is full.
    ExchangerFull(T),
    /// Exchanger cannot deliver an element as it is empty.
    ExchangerEmpty,
}

impl<T> Debug for ExchangerError<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ExchangerError::TaskError(e) => {
                write!(f, "TaskError: {e:?}")
            }
            ExchangerError::ExchangerFull(_) => {
                write!(f, "ExchangerFull")
            }
            ExchangerError::ExchangerEmpty => {
                write!(f, "ExchangerEmpty")
            }
        }
    }
}

impl<T> PartialEq for ExchangerError<T> {
    fn eq(&self, other: &Self) -> bool {
        match self {
            ExchangerError::TaskError(e) => {
                if let ExchangerError::TaskError(e2) = other {
                    return e == e2;
                }
                false
            }
            ExchangerError::ExchangerFull(_) => {
                matches!(other, ExchangerError::ExchangerFull(_))
            }
            ExchangerError::ExchangerEmpty => {
                matches!(other, ExchangerError::ExchangerEmpty)
            }
        }
    }
}

struct InnerExchange<T: Send> {
    mutex: Mutex<VecDeque<T>>,
    take_condition: Condvar,
    put_condition: Condvar,
    shutdown: AtomicBool,
    max_size: usize,
    num_waiting_takers: AtomicU16,
    num_waiting_putters: AtomicU16,
}

impl<T: Send> InnerExchange<T> {
    /// Creates a new InnerExchange.
    pub fn new(max_size: usize) -> Self {
        InnerExchange {
            max_size,
            mutex: Default::default(),
            take_condition: Default::default(),
            put_condition: Default::default(),
            shutdown: AtomicBool::new(false),
            num_waiting_takers: AtomicU16::new(0),
            num_waiting_putters: AtomicU16::new(0),
        }
    }

    ///
    /// Takes and returns an element, blocking if none are available.
    pub fn take_blocking(&self) -> Result<T, ExchangerError<T>> {
        let Ok(mut elems) = self.mutex.lock() else {
            return Err(ExchangerError::TaskError(TaskError::LockingError));
        };
        if let Some(e) = elems.pop_front() {
            trace!("Take_blocking popped one");
            self.put_condition.notify_one();
            return Ok(e);
        }
        // If there's still a few elements left in the queue, at this point they can be safely
        // flushed while shutting down, since no new elements can be added if this exchanger
        // is shutdown.  Prevent the waiter from waiting if we're shutting down.
        if self.shutdown.load(Ordering::SeqCst) {
            return Err(ExchangerError::TaskError(TaskError::ExecutorStoppingError));
        }
        trace!("Take_blocking waiting for element");
        self.num_waiting_takers.fetch_add(1, Ordering::SeqCst);
        let Ok(mut elems) = self.take_condition.wait_while(elems, |e| {
            e.is_empty() && !self.shutdown.load(Ordering::SeqCst)
        }) else {
            return Err(ExchangerError::TaskError(TaskError::LockingError));
        };
        self.num_waiting_takers.fetch_sub(1, Ordering::SeqCst);

        let Some(e) = elems.pop_front() else {
            trace!("Take_blocking woken up for empty exchange");
            // empty wakeup?  probably stopping.
            return Err(ExchangerError::TaskError(TaskError::ExecutorStoppingError));
        };
        trace!("Take_blocking woken up for new element");
        self.put_condition.notify_one();
        Ok(e)
    }

    ///
    /// Attempts to take one from this exchanger.  If one is available, it is returned. This
    /// function will not block, and if it would have blocked, returns
    /// [`Err(TaskError::ExchangerEmpty)`]
    pub fn try_take(&self) -> Result<T, ExchangerError<T>> {
        let Ok(mut elems) = self.mutex.lock() else {
            return Err(ExchangerError::TaskError(TaskError::LockingError));
        };
        if let Some(e) = elems.pop_front() {
            trace!("Take_blocking popped one");
            self.put_condition.notify_one();
            return Ok(e);
        }
        // If there's still a few elements left in the queue, at this point they can be safely
        // flushed while shutting down, since no new elements can be added if this exchanger
        // is shutdown.  Prevent the waiter from waiting if we're shutting down.
        if self.shutdown.load(Ordering::SeqCst) {
            return Err(ExchangerError::TaskError(TaskError::ExecutorStoppingError));
        }
        Err(ExchangerError::ExchangerEmpty)
    }

    ///
    /// Puts an element into this exchanger to allow exchanges to occur.  Will block indefinitely
    /// until there's enough space to put one in.
    pub fn put_blocking(&self, elem: T) -> Result<(), ExchangerError<T>> {
        // prevent any new elements from being put in if we're shutting down.
        if self.shutdown.load(Ordering::SeqCst) {
            return Err(ExchangerError::TaskError(TaskError::ExecutorStoppingError));
        }

        let Ok(mut elems) = self.mutex.lock() else {
            return Err(ExchangerError::TaskError(TaskError::LockingError));
        };
        if elems.len() < self.max_size {
            trace!("Put_blocking added one");
            elems.push_back(elem);
            self.take_condition.notify_one();
            return Ok(());
        }
        trace!("Put_blocking full, waiting for empty spot");
        // full, wait until a spot opens.
        self.num_waiting_putters.fetch_add(1, Ordering::SeqCst);
        let Ok(mut elems) = self.put_condition.wait_while(elems, |e| {
            e.len() >= self.max_size && !self.shutdown.load(Ordering::SeqCst)
        }) else {
            return Err(ExchangerError::TaskError(TaskError::LockingError));
        };
        self.num_waiting_putters.fetch_sub(1, Ordering::SeqCst);

        if elems.len() == self.max_size {
            trace!("Put_blocking woken up for full, cannot add new element");
            return Err(ExchangerError::ExchangerFull(elem));
        };

        trace!("Put_blocking woken up for free space");
        elems.push_back(elem);
        self.take_condition.notify_one();
        Ok(())
    }

    ///
    /// Attempts to put a new element into the exchanger, returning [`Ok`] if it was successful.
    /// This function will not block, if the call would have blocked, returns
    /// [`Err(ExchangerError::ExchangerFull(T))`] so you can have the element back.
    pub fn try_put(&self, elem: T) -> Result<(), ExchangerError<T>> {
        // prevent any new elements from being put in if we're shutting down.
        if self.shutdown.load(Ordering::SeqCst) {
            return Err(ExchangerError::TaskError(TaskError::ExecutorStoppingError));
        }

        let Ok(mut elems) = self.mutex.lock() else {
            return Err(ExchangerError::TaskError(TaskError::LockingError));
        };
        if elems.len() < self.max_size {
            trace!("try_put added one");
            elems.push_back(elem);
            self.take_condition.notify_one();
            return Ok(());
        }
        Err(ExchangerError::ExchangerFull(elem))
    }

    ///
    /// Stops this exchanger, waking up all waiters and
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::SeqCst);
        while self.num_waiting_putters.load(Ordering::SeqCst) > 0 {
            self.put_condition.notify_all();
        }
        while self.num_waiting_takers.load(Ordering::SeqCst) > 0 {
            self.take_condition.notify_all();
        }
    }
}

///
/// A thread-safe, shared exchange buffer.  Allows multiple producers to push elements in, up to a
/// blocking max capacity.  Allows multiple consumers to take elements out, blocking if none
/// available.
pub struct Exchanger<T: Send> {
    exchange: Arc<InnerExchange<T>>,
}

impl<T: Send> Clone for Exchanger<T> {
    fn clone(&self) -> Self {
        Exchanger {
            exchange: self.exchange.clone(),
        }
    }
}

impl<T: Send> Exchanger<T> {
    ///
    /// Creates a new exchanger, that can store at most these elements in the queue.
    ///
    /// Note:  Putting a value of `0`/zero for max_size implies that this exchanger will do no work
    /// and exchange no items.  A recommended minimum value of `1` should be used instead.
    pub fn new(max_size: usize) -> Self {
        Exchanger {
            exchange: Arc::new(InnerExchange::new(max_size)),
        }
    }

    ///
    /// Push a new element into the exchanger, blocking until space is available.
    pub fn push(&self, elem: T) -> Result<(), ExchangerError<T>> {
        self.exchange.put_blocking(elem)
    }

    pub fn try_push(&self, elem: T) -> Result<(), ExchangerError<T>> {
        self.exchange.try_put(elem)
    }

    ///
    /// Take a new element from the exchanger, blocking until one is available.
    pub fn take(&self) -> Result<T, ExchangerError<T>> {
        self.exchange.take_blocking()
    }

    pub fn try_take(&self) -> Result<T, ExchangerError<T>> {
        self.exchange.try_take()
    }

    ///
    /// Shuts down this exchanger, preventing new pushes.  Any objects already pushed will be
    /// permitted to be taken, and once empty, takers will receive a
    /// [`TaskError::ExecutorStoppingError`]
    pub fn shutdown(&self) {
        self.exchange.shutdown();
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::sync::Arc;
    use std::thread::JoinHandle;
    use std::time::Duration;

    use log::{error, info, Level};

    use crate::{Exchanger, ExchangerError, TaskError};

    #[test]
    pub fn test_single_sender_receiver() -> Result<(), ExchangerError<u32>> {
        // irox_log::init_console_level(Level::Trace);
        let (err_sender, err_receiver) = std::sync::mpsc::channel();
        let err_sender2 = err_sender.clone();
        let exch1 = Exchanger::<u32>::new(10);
        let exch2 = exch1.clone();
        let genthrd = std::thread::Builder::new()
            .name("Sender".to_string())
            .spawn(move || {
                let mut sent = 0;
                for i in 0..1_000 {
                    if let Err(e) = exch2.push(i) {
                        eprintln!("Error sending exchange: {e:?}");
                        if let Err(e) = err_sender.send((e, i, "send")) {
                            panic!("{e:?}");
                        }
                    }
                    sent += 1;
                }
                println!("Sent {sent}");
            })
            .unwrap();

        let recv_thrd = std::thread::Builder::new()
            .name("Receiver".to_string())
            .spawn(move || {
                let mut recvd = 0;
                for i in 0..1_000 {
                    if let Err(e) = exch1.take() {
                        eprintln!("Error receiving exchange: {e:?}");
                        if let Err(e) = err_sender2.send((e, i, "recv")) {
                            panic!("{e:?}");
                        }
                    }
                    std::thread::sleep(Duration::from_millis(1)); // simulate work.
                    recvd += 1;
                }
                println!("Received {recvd}");
            })
            .unwrap();

        genthrd.join().unwrap();
        recv_thrd.join().unwrap();

        let mut errors: bool = false;
        while let Ok(r) = err_receiver.recv() {
            let (e, i, s) = r;
            eprintln!("Error received {e:?} : {i} : {s}");
            errors = true;
        }

        assert!(!errors);

        Ok(())
    }

    #[test]
    pub fn test_multiple_receivers() {
        irox_log::init_console_level(Level::Info);
        let (err_sender, err_receiver) = std::sync::mpsc::channel();
        let err_sender2 = err_sender.clone();
        let exch1 = Exchanger::<u32>::new(10);
        let exch2 = exch1.clone();
        let exch3 = exch1.clone();
        let genthrd = std::thread::Builder::new()
            .name("Sender".to_string())
            .spawn(move || {
                let mut sent = 0;
                for i in 0..1_000_000 {
                    if let Err(e) = exch2.push(i) {
                        eprintln!("Error sending exchange: {e:?}");
                        if let Err(e) = err_sender.send((e, i, "send")) {
                            panic!("{e:?}");
                        }
                    }
                    sent += 1;
                }
                info!("Sent {sent}");
            })
            .unwrap();

        let recv_count = Arc::new(AtomicU64::new(0));
        let mut receivers: Vec<JoinHandle<()>> = Vec::new();
        for thread_idx in 0..10 {
            let counter = recv_count.clone();
            let err_sender2 = err_sender2.clone();
            let exch1 = exch1.clone();
            let recv_thrd = std::thread::Builder::new()
                .name(format!("Receiver {thread_idx}"))
                .spawn(move || {
                    let counter = counter;

                    let mut recvd = 0;
                    loop {
                        if let Err(e) = exch1.take() {
                            if e == ExchangerError::TaskError(TaskError::ExecutorStoppingError) {
                                // it's a good thing!
                                break;
                            }
                            error!("Error receiving exchange: {e:?}");
                            if let Err(e) = err_sender2.send((e, recvd, "recv")) {
                                panic!("Error sending error: {e:?}");
                            }
                            break;
                        }
                        // std::thread::sleep(Duration::from_millis(1));// simulate work.
                        recvd += 1;
                        counter.fetch_add(1, Ordering::Relaxed);
                    }
                    info!(
                        "Received {recvd} in thread {}",
                        std::thread::current().name().unwrap_or("")
                    );
                })
                .unwrap();
            receivers.push(recv_thrd);
        }
        drop(err_sender2);

        genthrd.join().unwrap();
        info!("Generator thread joined");
        exch3.shutdown();
        info!("Executor shutdown");

        for recv in receivers {
            info!("Waiting on {}", recv.thread().name().unwrap_or(""));
            recv.join().unwrap();
        }

        let mut errors: bool = false;
        while let Ok(r) = err_receiver.recv() {
            let (e, i, s) = r;
            error!("Error received {e:?} : {i} : {s}");
            errors = true;
        }

        assert!(!errors);
    }
}