kyron-foundation 0.0.1

kyron: foundational components for runtime
Documentation
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
//
// Copyright (c) 2025 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// SPDX-License-Identifier: Apache-2.0
//

use ::core::alloc::Layout;
use ::core::cell::UnsafeCell;
use ::core::marker::PhantomData;
use ::core::mem::MaybeUninit;
use std::alloc::{self}; // TODO: maybe replace this too from iceoryx2
use std::sync::Arc;

use crate::prelude::*;
use ::core::sync::atomic::Ordering;

use crate::containers::mpmc_queue::MpmcQueue;

///
/// Delivers access for push & pop actions for a SpmcStealQueue ensuring there is only one producer.
///
pub struct LocalProducerConsumer<'a, T: Send> {
    queue: &'a SpmcStealQueue<T>,
    _marker: PhantomData<*const ()>, // marker for !Send and !Sync
}

impl<T: Send> LocalProducerConsumer<'_, T> {
    ///
    /// Tries to push a single `value` to the local queue. If that does not work, because the queue
    /// is full, items from the local queue are pushed to the global `mpmc` queue. This frees some
    /// room for new items in the local queue. The `value` is then pushed again to the local queue.
    ///
    ///  # Returns
    ///
    ///
    ///
    pub fn push(&self, value: T, mpmc: &MpmcQueue<T>) -> Result<(), T> {
        self.queue.push(value, mpmc)
    }

    ///
    /// Pops single value out of Queue
    ///
    /// # Returns
    ///
    /// None if queue is empty, otherwise Some(value)
    ///
    pub fn pop(&self) -> Option<T> {
        self.queue.pop_local()
    }
}

impl<T: Send> Drop for LocalProducerConsumer<'_, T> {
    fn drop(&mut self) {
        self.queue.has_producer.store(false, Ordering::SeqCst);
    }
}

///
/// Delivers access for push & pop actions for a SpmcStealQueue ensuring there is only one producer.
///
pub struct BoundProducerConsumer<T: Send> {
    queue: Arc<SpmcStealQueue<T>>,
    _marker: PhantomData<*const ()>, // marker for !Send and !Sync
}

impl<T: Send> BoundProducerConsumer<T> {
    ///
    /// Pushes single value to the Queue
    ///
    ///  # Returns
    ///
    ///
    ///
    pub fn push(&self, value: T, mpmc: &MpmcQueue<T>) -> Result<(), T> {
        self.queue.push(value, mpmc)
    }

    ///
    /// Pops single value out of Queue
    ///
    /// # Returns
    ///
    /// None if queue is empty, otherwise Some(value)
    ///
    pub fn pop(&self) -> Option<T> {
        self.queue.pop_local()
    }

    pub fn count(&self) -> u32 {
        self.queue.count()
    }

    pub fn capacity(&self) -> u32 {
        self.queue.capacity()
    }

    ///
    /// Takes items out of the provided `MpmcQueue` by pointer, and puts them into the spmc. This
    /// does only involve a single copy operation per item. This operation is done holding the
    /// Mutex lock on the mpmc.
    ///
    pub fn fetch_from(&self, mpmc: &MpmcQueue<T>) -> usize {
        self.queue.fetch_from(mpmc)
    }
}

impl<T: Send> Drop for BoundProducerConsumer<T> {
    fn drop(&mut self) {
        self.queue.has_producer.store(false, Ordering::SeqCst);
    }
}

///
/// Lock free queue for Single-Producer-Multiple-Consumer, supporting stealing API and fixed size allocated during construction
///
/// Key safety assumptions:
/// - push can happen only from single thread, pop can happen from same thread as push
/// - stealing can happen from any thread
/// - stealing have same order of extracting values as 'pop'
/// - T cannot have self referential data directly inside itself
///
///
pub struct SpmcStealQueue<T: Send> {
    /// The counter used for insertion operation
    head: FoundationAtomicU32,

    /// This holds tuple (steal_place, real_tail). steal_place is used for tracking stealing, real_tail is used for pop values out of queue
    tail: FoundationAtomicU64,

    /// Used to turn head/tail into index of `data`
    access_mask: u32,

    /// Ensures that only one producer exists
    has_producer: FoundationAtomicBool,
    capacity: u32,
    data: Box<[UnsafeCell<MaybeUninit<T>>]>,
}

// Type is Safe to share across thread and change thread
unsafe impl<T: Send> Send for SpmcStealQueue<T> {}
unsafe impl<T: Send> Sync for SpmcStealQueue<T> {}

impl<T: Send> SpmcStealQueue<T> {
    ///
    /// Construct new queue of size `size`. It will panic if construction can not happen.
    /// # Arguments
    ///
    /// - `size` - must be power of two
    ///
    pub fn new(size: u32) -> Self {
        assert!(size.is_power_of_two());

        let layout = Layout::array::<UnsafeCell<MaybeUninit<T>>>(size as usize).expect("Invalid layout for array provided");

        let ptr = unsafe { alloc::alloc(layout) as *mut UnsafeCell<MaybeUninit<T>> };

        if ptr.is_null() {
            panic!("Memory allocation failed for SpmcStealQueue with size {}", size);
        }

        Self {
            head: FoundationAtomicU32::new(0),
            tail: FoundationAtomicU64::new(0),
            data: unsafe { Box::from_raw(::core::slice::from_raw_parts_mut(ptr, size as usize)) },
            access_mask: size - 1,
            has_producer: FoundationAtomicBool::new(false),
            capacity: size,
        }
    }

    ///
    /// Returns number of elements the queue can hold at max
    ///
    pub fn capacity(&self) -> u32 {
        self.capacity
    }

    ///
    /// Returns number of elements in the queue
    ///
    pub fn count(&self) -> u32 {
        // *Synchronization* : Any thread can call this fn, we want it to be really accurate.
        let head = self.head.load(Ordering::SeqCst);
        let tail = self.tail.load(Ordering::SeqCst);

        Self::count_internal(head, tail)
    }

    ///
    /// Created `LocalProducerConsumer` if there is any existing, otherwise None
    ///
    pub fn get_local(&self) -> Option<LocalProducerConsumer<'_, T>> {
        // *Synchronization* : SeqCst only to provide strongest guarantee while obtaining producer
        if self
            .has_producer
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
            .is_ok()
        {
            Some(LocalProducerConsumer {
                queue: self,
                _marker: PhantomData,
            })
        } else {
            None
        }
    }

    ///
    /// Created `LocalProducerConsumer` if there is any existing, otherwise None
    ///
    pub fn get_boundedl(self: &Arc<Self>) -> Option<BoundProducerConsumer<T>> {
        // *Synchronization* : SeqCst only to provide strongest guarantee while obtaining producer
        if self
            .has_producer
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
            .is_ok()
        {
            Some(BoundProducerConsumer {
                queue: self.clone(),
                _marker: PhantomData,
            })
        } else {
            None
        }
    }

    ///
    /// Let caller steal a work from other queue into its queue
    ///
    /// # Arguments
    ///
    /// - `dst` - destination queue
    /// - `divider` - the call will try to steal min(half count of a queue, avail space in dst) by default. When divider is provided, it will calculate steal size as `half_count / divider`
    ///
    pub fn steal_into(&self, dst: &Self, divider: Option<u8>) -> Option<u32> {
        // dst is stealing thread queue and writing to it is done only by the one that calls this function now

        let dst_head = dst.head.load(Ordering::Relaxed); // Only we are changing our head
        let dst_tail = dst.tail.load(Ordering::Acquire);
        let (dst_steal, _) = Self::unpack_tail(dst_tail);

        let dst_avail_size = dst_head.wrapping_sub(dst_steal);

        if dst_avail_size >= dst.data.len() as u32 {
            return None; // No space left
        }

        // How much did we stolen
        let n = self.steal_internal(
            dst,
            dst_head,
            dst.data.len() as u32 - dst_avail_size,
            divider.unwrap_or(0) + 2, // By default, half content steal
        );

        if n == 0 {
            return None; // Nothing ;)
        }

        // *Synchronization*: This makes sure above writes are before we increment head, which basically publish data to others
        dst.head.store(dst_head.wrapping_add(n), Ordering::Release);

        Some(n)
    }

    //
    // Private impl part
    //

    ///
    /// Push to local spmc queue. If that does not work, try to make room in local queue by pushing
    /// items to global mpmc queue and then try again.
    /// An error is returned if global mpmc is also full.
    ///
    fn push(&self, value: T, mpmc: &MpmcQueue<T>) -> Result<(), T> {
        let res = self.push_local_queue(value);
        if let Err(v) = res {
            if self.push_to_mpmc(mpmc, None) == 0 {
                return Err(v);
            }

            return self.push_local_queue(v);
        }

        res
    }

    fn push_local_queue(&self, value: T) -> Result<(), T> {
        let head = self.head.load(Ordering::Relaxed); // Write only from current thread
        let tail = self.tail.load(Ordering::Acquire); // Access by multiple threads, so we want to make sure other threads have made visible all operations prior to Release

        let (steal, _) = Self::unpack_tail(tail); // We can push only based on where steel counter is, as this is real indicator of free space (if someone stealing, tail may have been already advanced)

        if head.wrapping_sub(steal) < self.data.len() as u32 {
            let index = head & self.access_mask;

            // Head index is only mutated when being here by single thread and is borrowed only once mutation finishes, so Cell req are fulfilled
            unsafe {
                let entry = self.data[index as usize].get();
                (*entry).as_mut_ptr().write(value);
            }

            // *Synchronization*: This makes sure above writes are before we increment head
            self.head.store(head.wrapping_add(1), Ordering::Release);
            return Ok(());
        }

        //TODO: consider push_overflow to global wait queue, currently we may end here even if queue is not full as tail might already been taken up.
        Err(value)
    }

    fn fetch_from(&self, mpmc: &MpmcQueue<T>) -> usize {
        let head = self.head.load(Ordering::Relaxed); // Write only from current thread
        let tail = self.tail.load(Ordering::Acquire); // Access by multiple threads, so we want to make sure other threads have made visible all operations prior to Release

        let num_possible = mpmc.len().min((self.capacity - Self::count_internal(head, tail)) as usize);
        let mut num_items = 0;

        // Head index is only mutated when being here by single thread and is borrowed only once mutation finishes, so Cell req are fulfilled
        let mut iter = mpmc.iter();
        while num_items < num_possible {
            // When we are taking a pointer to an item from the iterator by calling next, we are
            // also responsible for taking the item behind the pointer. If we do not do this, the
            // item is lost.
            if let Some(ptr) = iter.next() {
                let index = (head + num_items as u32) & self.access_mask;
                unsafe {
                    let entry = self.data[index as usize].get();
                    ::core::ptr::copy_nonoverlapping(ptr, entry, 1);
                }

                num_items += 1;
            } else {
                break;
            }
        }

        // *Synchronization*: This makes sure above writes are before we increment head
        self.head.store(head.wrapping_add(num_items as u32), Ordering::Release);
        num_items
    }

    fn pop_local(&self) -> Option<T> {
        let mut tail: u64 = self.tail.load(Ordering::Acquire); // Modified by multiple threads

        let index = loop {
            let head = self.head.load(Ordering::Relaxed); // We are in same thread that could only change it

            let (steal, real_tail) = Self::unpack_tail(tail);

            if head == real_tail {
                // Empty
                return None;
            }

            let index = (real_tail & self.access_mask) as usize;
            let next_real_tail = real_tail.wrapping_add(1);

            let expected_tail = if steal == real_tail {
                // No one stealing, keep both same
                Self::pack_tail(next_real_tail, next_real_tail)
            } else {
                Self::pack_tail(steal, next_real_tail) // someone stealing, keep steal as is and try to move only tail
            };

            let res = self.tail.compare_exchange(
                tail,
                expected_tail,
                Ordering::AcqRel,
                Ordering::Acquire, // TODO: SeqCst ?
            );

            match res {
                Ok(_) => break index,
                Err(actual) => tail = actual, // Start from beginning
            }
        };

        // If you wonder why we do it like that, we already marked index as taken, so write can happen there but this call is a call
        // for very same thread as write can happen, so no one will write us before we really read it
        unsafe {
            let entry: &UnsafeCell<MaybeUninit<T>> = &self.data[index];
            Some((*entry.get()).as_ptr().read())
        }
    }

    pub fn steal_internal(&self, dst: &Self, dst_head: u32, max_steal_size: u32, divider: u8) -> u32 {
        let mut tail = self.tail.load(Ordering::Acquire);

        let (start_idx, count) = loop {
            let (steal, real_tail) = Self::unpack_tail(tail);
            let head = self.head.load(Ordering::Acquire); // As writer thread can modify it any time, we need to fetch it always

            if head == real_tail {
                return 0; // Nothing in queue, empty already
            }

            if steal != real_tail {
                return 0; // Someone else is stealing now from us, lets stop the new thread from doing the same
            }

            // Now we can try to steal something
            let mut expected_steel_count: u32 = head.wrapping_sub(real_tail) / divider as u32;
            expected_steel_count = expected_steel_count.min(max_steal_size);
            expected_steel_count = expected_steel_count.max(1); // no less than one to steal

            let next_real_tail = real_tail.wrapping_add(expected_steel_count);
            let expected_tail = Self::pack_tail(steal, next_real_tail); // For now steel is as it was, this will mark that we are stealing and prevent others to steal from us

            let res = self.tail.compare_exchange(tail, expected_tail, Ordering::AcqRel, Ordering::Acquire);

            match res {
                Ok(_) => break (real_tail & self.access_mask, expected_steel_count),
                Err(actual) => {
                    tail = actual; // Lets try again, as someone else was doing something to queue
                }
            }
        };

        if count == 0 {
            return 0;
        }

        for i in 0..count {
            unsafe {
                let src_index = ((start_idx + i) & self.access_mask) as usize;
                let src_entry: &UnsafeCell<MaybeUninit<T>> = &self.data[src_index];

                let dst_index = ((dst_head + i) & dst.access_mask) as usize;
                let dst_entry: &UnsafeCell<MaybeUninit<T>> = &dst.data[dst_index];

                (*dst_entry.get()).as_mut_ptr().write((*src_entry.get()).as_ptr().read());
            }
        }

        tail = self.tail.load(Ordering::Acquire);
        loop {
            let (_, real_tail) = Self::unpack_tail(tail);

            let expected_tail = Self::pack_tail(real_tail, real_tail); // We are done, try align steal and tail to either our tail value or someone else if higher

            let res = self.tail.compare_exchange(tail, expected_tail, Ordering::AcqRel, Ordering::SeqCst);

            match res {
                Ok(_) => {
                    return count;
                }
                Err(actual) => tail = actual,
            }
        }
    }

    ///
    /// This makes room in our local spmc queue and pushes items to the global mpmc queue. How many items
    /// will be pushed is influenced by the `divider`.
    /// The function returns how many items were finally pushed.
    ///
    /// Note: This function is assumed to be only called from the single producer thread.
    ///
    fn push_to_mpmc(&self, mpmc: &MpmcQueue<T>, divider: Option<u8>) -> u32 {
        let head = self.head.load(Ordering::Relaxed); // Write only from current thread
        let mut mpmc_inner_queue = mpmc.lock();
        let mpmc_free_space = (mpmc_inner_queue.capacity() - mpmc_inner_queue.len()) as u32;
        let mut num_items;
        let mut index;
        let mut tail = self.tail.load(Ordering::Acquire); // Access by multiple threads, so we want to make sure other threads have made visible all operations prior to Release

        // Advance tail before actually copying out the items
        loop {
            let (steal, real_tail) = SpmcStealQueue::<T>::unpack_tail(tail); // We can push only based on where steal counter is, as this is real indicator of free space (if someone stealing, tail may have been already advanced)
            index = real_tail;

            let num_items_from_spmc = Self::count_internal(head, tail) / divider.unwrap_or(2) as u32;
            num_items = mpmc_free_space.min(num_items_from_spmc);
            let next_real_tail = real_tail.wrapping_add(num_items);
            let expected_tail = if steal == real_tail {
                // No one stealing, keep both same
                SpmcStealQueue::<T>::pack_tail(next_real_tail, next_real_tail)
            } else {
                SpmcStealQueue::<T>::pack_tail(steal, next_real_tail) // someone stealing, keep steal as is and try to move only tail
            };

            let res = self.tail.compare_exchange(
                tail,
                expected_tail,
                Ordering::AcqRel,
                Ordering::Acquire, // TODO: SeqCst ?
            );

            // if updating tail worked, we can break out. If it failed, someone stealed inbetween
            // and we retry.
            match res {
                Ok(_) => {
                    break;
                }
                Err(actual) => tail = actual,
            }
        }

        let mut successful_items = 0;
        for _ in 0..num_items {
            index &= self.access_mask;
            let item = self.data[index as usize].get();
            let item = unsafe { (*item).assume_init_read() };
            if !mpmc_inner_queue.push(item) {
                break;
            }

            index = index.wrapping_add(1);
            successful_items += 1;
        }

        successful_items
    }

    fn count_internal(head: u32, tail: u64) -> u32 {
        let (_, real_tail) = Self::unpack_tail(tail);

        head.wrapping_sub(real_tail)
    }

    fn unpack_tail(value: u64) -> (u32, u32) {
        ((value >> 32) as u32, value as u32)
    }

    fn pack_tail(steal: u32, tail: u32) -> u64 {
        ((steal as u64) << 32) | tail as u64
    }
}

#[cfg(test)]
#[cfg(not(loom))]
mod tests {

    static DUMMY: u32 = 0xDEADBEEF;

    use super::*;
    use std::vec::Vec;
    use std::{sync::Arc, thread};

    fn push_n_elems_with_increment(start_val: u32, n: u32, queue: &LocalProducerConsumer<u32>) -> std::vec::Vec<u32> {
        let mut vec_pushed = std::vec::Vec::new();
        let mpmc = MpmcQueue::new(1);
        for i in 0..n {
            assert!(queue.push(i + start_val, &mpmc).is_ok());
            vec_pushed.push(i + start_val);
        }

        vec_pushed
    }

    #[test]
    fn test_unpack() {
        let (upper, lower) = SpmcStealQueue::<bool>::unpack_tail(0xdeadbeef12345678);
        assert_eq!(upper, 0xdeadbeef);
        assert_eq!(lower, 0x12345678);
    }

    #[test]
    fn test_push_pop_same_thread() {
        let queue: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let pc = queue.get_local().unwrap();

        push_n_elems_with_increment(0, 5, &pc);

        assert_eq!(pc.pop().unwrap_or(DUMMY), 0);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 1);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 2);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 3);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 4);

        assert!(pc.pop().is_none()); // No more data
        assert!(pc.pop().is_none()); // No more data
    }

    #[test]
    fn test_push_pop_interleaved_same_thread() {
        let queue: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let pc = queue.get_local().unwrap();

        push_n_elems_with_increment(0, 3, &pc);

        assert_eq!(pc.pop().unwrap_or(DUMMY), 0);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 1);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 2);

        push_n_elems_with_increment(3, 2, &pc);

        assert_eq!(pc.pop().unwrap_or(DUMMY), 3);
        assert_eq!(pc.pop().unwrap_or(DUMMY), 4);

        assert!(pc.pop().is_none()); // No more data
        assert!(pc.pop().is_none()); // No more data
    }

    #[test]
    fn test_push_overflow_same_thread() {
        let queue: SpmcStealQueue<u32> = SpmcStealQueue::new(8);
        let pc = queue.get_local().unwrap();

        push_n_elems_with_increment(0, 8, &pc);

        let mpmc = MpmcQueue::new(1);
        // queue is full, push will make room by pushing first item from spmc to the mpmc and then
        // push the actual item to spmc
        let res = pc.push(8, &mpmc);
        assert!(res.is_ok());
        let res = pc.push(9, &mpmc);
        assert!(res.is_err());
        assert_eq!(res.unwrap_err(), 9); // We get back value as error so we can use it again
        assert_eq!(mpmc.pop(), Some(0)); // First item was pushed to mpmc
        for i in 1..9 {
            let item = pc.pop();
            assert_eq!(item, Some(i));
        }

        assert_eq!(pc.pop(), None);
    }

    #[test]
    fn test_pop_underflow_same_thread() {
        let queue: SpmcStealQueue<u32> = SpmcStealQueue::new(8);
        let pc = queue.get_local().unwrap();

        assert!(pc.pop().is_none());

        push_n_elems_with_increment(0, 3, &pc);

        pc.pop();
        pc.pop();
        pc.pop();

        assert!(pc.pop().is_none());

        let mpmc = MpmcQueue::new(1);
        assert!(queue.push(4, &mpmc).is_ok());
        pc.pop();

        assert!(pc.pop().is_none());
    }

    #[test]
    fn test_only_single_producer_can_be_created_same_thread() {
        let queue: SpmcStealQueue<u32> = SpmcStealQueue::new(8);
        let pc = queue.get_local().unwrap();

        assert!(queue.get_local().is_none());
        assert!(queue.get_local().is_none());

        drop(pc);

        let new_pc = queue.get_local();

        assert!(new_pc.is_some());
        assert!(queue.get_local().is_none());
    }

    #[test]
    fn test_steal_single_thread() {
        let queue: SpmcStealQueue<u32> = SpmcStealQueue::new(8);
        let pc = queue.get_local().unwrap();

        push_n_elems_with_increment(0, 5, &pc);

        assert_eq!(pc.pop().unwrap_or(DUMMY), 0);

        let steal_into_queue: SpmcStealQueue<u32> = SpmcStealQueue::new(8);
        let steal_pc = steal_into_queue.get_local().unwrap();

        {
            let ret = queue.steal_into(&steal_into_queue, None); // Test if by default we get 2 values (since there are 4)

            assert_eq!(ret.unwrap_or(DUMMY), 2);
            assert_eq!(steal_pc.pop().unwrap_or(DUMMY), 1);
            assert_eq!(steal_pc.pop().unwrap_or(DUMMY), 2);

            assert_eq!(pc.pop().unwrap_or(DUMMY), 3);
            assert_eq!(pc.pop().unwrap_or(DUMMY), 4);
        }

        // test divider
        {
            assert!(pc.pop().is_none());

            push_n_elems_with_increment(0, 8, &pc);

            let ret = queue.steal_into(&steal_into_queue, Some(2)); // Test if by default we get 2 values (since there are 4)

            assert_eq!(ret.unwrap_or(DUMMY), 2);
            assert_eq!(steal_pc.pop().unwrap_or(DUMMY), 0);
            assert_eq!(steal_pc.pop().unwrap_or(DUMMY), 1);
        }
    }

    fn steal_fn(queue: Arc<SpmcStealQueue<u32>>, stop: Arc<FoundationAtomicBool>) -> Vec<u32> {
        let mut vec = Vec::new();
        let steal_queue: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let steal_pc = steal_queue.get_local().unwrap();

        while !stop.load(Ordering::SeqCst) {
            let ret = queue.steal_into(&steal_queue, None);

            if let Some(count) = ret {
                for _ in 0..count {
                    let v = steal_pc.pop();

                    if let Some(val) = v {
                        vec.push(val);
                    }
                }
            }
        }

        vec
    }
    fn steal_for_pop_fn(queue: Arc<SpmcStealQueue<u32>>) -> Vec<u32> {
        let mut vec = Vec::new();
        let steal_queue: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let steal_pc = steal_queue.get_local().unwrap();

        loop {
            if queue.steal_into(&steal_queue, None).is_some() {
                while let Some(val) = steal_pc.pop() {
                    vec.push(val);
                }
            } else {
                break;
            }
        }

        vec
    }

    #[test]
    fn test_mt_one_pop_one_stealer() {
        let mut tasks_done: [Option<u32>; 32768] = [None; 32768];
        let queue: Arc<SpmcStealQueue<u32>> = Arc::new(SpmcStealQueue::new(32768));
        let pc = queue.get_local().unwrap();
        push_n_elems_with_increment(0, 32768, &pc);

        let handle1 = {
            let qc = queue.clone();
            thread::spawn(move || steal_for_pop_fn(qc))
        };

        while let Some(i) = pc.pop() {
            assert_eq!(tasks_done[i as usize], None);
            tasks_done[i as usize] = Some(i);
        }

        let worker1_vec = handle1.join().unwrap();
        for i in worker1_vec {
            assert_eq!(tasks_done[i as usize], None);
            tasks_done[i as usize] = Some(i);
        }

        for i in tasks_done {
            assert_ne!(i, None);
        }
    }

    #[test]
    fn test_one_producer_one_stealer_mt_thread() {
        let stop = Arc::new(FoundationAtomicBool::new(false));
        let queue: Arc<SpmcStealQueue<u32>> = Arc::new(SpmcStealQueue::new(256));
        let pc = queue.get_local().unwrap();

        let mut vec_produced: Vec<u32> = Vec::new();

        vec_produced.extend(push_n_elems_with_increment(0, 10, &pc));

        let handle1 = {
            let sc = stop.clone();
            let qc = queue.clone();
            thread::spawn(move || steal_fn(qc, sc))
        };

        let mut i = *vec_produced.last().unwrap();
        let mpmc = MpmcQueue::new(256);
        while i < 100000 {
            if pc.push(i, &mpmc).is_ok() {
                vec_produced.push(i);
                i += 1;
            }

            if i % 1000 == 0 {
                thread::yield_now();
            }
        }

        // Stop all
        stop.store(true, Ordering::SeqCst);

        let mut worker1_vec = handle1.join().unwrap();

        worker1_vec.sort();

        // Threads may have finished before final end, so something is still there, so we shall pop it here
        let remaining_in_queue = queue.count();
        while let Some(v) = pc.pop() {
            worker1_vec.push(v);
        }

        let is_more_than_20 = remaining_in_queue > (vec_produced.len() as f32 * 0.2) as u32;
        assert!(!is_more_than_20, "Was all {}, remaining {}", vec_produced.len(), remaining_in_queue); // We assume no more than 20% can stay in queue on producer thread

        // We may have some things in global queue now, we need to account them into comparison
        while let Some(v) = mpmc.pop() {
            worker1_vec.push(v);
        }

        worker1_vec.sort();

        assert_eq!(worker1_vec.len() + queue.count() as usize, vec_produced.len());

        assert_eq!(
            vec_produced,
            worker1_vec,
            "producer (len: {}) vs consumer(len: {}) vectors not equal, was remaining in queue {}",
            vec_produced.len(),
            worker1_vec.len(),
            remaining_in_queue
        );
    }

    #[test]
    fn test_one_producer_multi_stealer_mt_thread() {
        let stop = Arc::new(FoundationAtomicBool::new(false));
        let queue: Arc<SpmcStealQueue<u32>> = Arc::new(SpmcStealQueue::new(256));
        let pc = queue.get_local().unwrap();

        let mut vec_produced: Vec<u32> = Vec::new();

        vec_produced.extend(push_n_elems_with_increment(0, 10, &pc));

        let handle1 = {
            let sc = stop.clone();
            let qc = queue.clone();
            thread::spawn(move || steal_fn(qc, sc))
        };

        let handle2 = {
            let sc = stop.clone();
            let qc = queue.clone();
            thread::spawn(move || steal_fn(qc, sc))
        };

        let handle3 = {
            let sc = stop.clone();
            let qc = queue.clone();
            thread::spawn(move || steal_fn(qc, sc))
        };

        let mut i = *vec_produced.last().unwrap();
        let mpmc: MpmcQueue<u32> = MpmcQueue::new(256);
        while i < 1000000 {
            if pc.push(i, &mpmc).is_ok() {
                vec_produced.push(i);
                i += 1;
            }

            if i % 12000 == 0 {
                thread::yield_now();
            }
        }

        // Stop all
        stop.store(true, Ordering::SeqCst);

        let mut worker1_vec = handle1.join().unwrap();
        let worker2_vec = handle2.join().unwrap();
        let worker3_vec = handle3.join().unwrap();

        worker1_vec.extend(worker2_vec);
        worker1_vec.extend(worker3_vec);

        // Threads may have finished before final end, so something is still there, so we shall pop it here
        let remaining_in_queue = queue.count();
        while let Some(v) = pc.pop() {
            worker1_vec.push(v);
        }

        let is_more_than_20 = remaining_in_queue > (worker1_vec.len() as f32 * 0.2) as u32;
        assert!(!is_more_than_20, "Was all {}, remaining {}", vec_produced.len(), remaining_in_queue); // We assume no more than 20% can stay in queue on producer thread

        // We may have some things in global queue now, we need to account them into comparison
        while let Some(v) = mpmc.pop() {
            worker1_vec.push(v);
        }

        worker1_vec.sort();

        assert_eq!(worker1_vec.len() + queue.count() as usize, vec_produced.len());

        assert_eq!(
            vec_produced,
            worker1_vec,
            "producer (len: {}) vs consumer(len: {}) vectors not equal, was remaining in queue {}",
            vec_produced.len(),
            worker1_vec.len(),
            remaining_in_queue
        );
    }

    #[test]
    fn test_1_thread_push_mpmc() {
        let src: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let src_pc = src.get_local().unwrap();

        push_n_elems_with_increment(0, 30, &src_pc);
        let dst: MpmcQueue<u32> = MpmcQueue::new(16);
        assert_eq!(src.push_to_mpmc(&dst, None), 15);

        for i in 0..15 {
            assert_eq!(dst.pop(), Some(i));
        }

        assert_eq!(dst.pop(), None);

        for i in 15..30 {
            assert_eq!(src_pc.pop(), Some(i));
        }

        assert_eq!(src_pc.pop(), None);

        push_n_elems_with_increment(30, 30, &src_pc);
        assert_eq!(src.push_to_mpmc(&dst, None), 15);

        for i in 30..45 {
            assert_eq!(dst.pop(), Some(i));
        }

        assert_eq!(dst.pop(), None);

        for i in 45..60 {
            assert_eq!(src_pc.pop(), Some(i));
        }

        assert_eq!(src_pc.pop(), None);
    }

    ///
    /// This is a multithreaded test that does stealing from a SpmcStealQueue in one thread and
    /// `push_to_mpmc` in another thread.
    ///
    #[test]
    fn test_mt_one_push_mpmc_one_stealer() {
        const QUEUE_LEN: usize = 32768;
        let mut tasks_done: [Option<u32>; QUEUE_LEN] = [None; QUEUE_LEN];
        let queue: Arc<SpmcStealQueue<u32>> = Arc::new(SpmcStealQueue::new(QUEUE_LEN as u32));
        let pc = queue.get_local().unwrap();
        push_n_elems_with_increment(0, QUEUE_LEN as u32, &pc);

        let handle1 = {
            let qc = queue.clone();
            thread::spawn(move || steal_for_pop_fn(qc))
        };

        let dst: MpmcQueue<u32> = MpmcQueue::new(16);
        loop {
            // push to mpmc
            if queue.push_to_mpmc(&dst, None) == 0 {
                break;
            }

            // empty the mpmc
            while let Some(i) = dst.pop() {
                assert_eq!(tasks_done[i as usize], None);
                tasks_done[i as usize] = Some(i);
            }
        }

        let worker1_vec = handle1.join().unwrap();
        // insert tasks from steal thread into our tasks_done array
        for i in worker1_vec {
            assert_eq!(tasks_done[i as usize], None);
            tasks_done[i as usize] = Some(i);
        }

        // Is there one last remaining task in spmc that can't be stolen?
        while let Some(i) = pc.pop() {
            assert_eq!(tasks_done[i as usize], None);
            tasks_done[i as usize] = Some(i);
        }

        // check if every task was exactly one time in a task queue
        for i in tasks_done {
            assert_ne!(i, None);
        }
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by TAKE_GLOBAL_WORK_SIZE
    ///
    #[test]
    fn test_push_mpmc_multi_1() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 0..16 {
            assert!(src.push(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 16);

        for i in 0..16 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);
        assert_eq!(src.pop(), None);
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by space left in spmc
    ///
    #[test]
    fn test_push_mpmc_multi_2() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 0..27 {
            assert_eq!(dst_pc.push(i, &src), Ok(()));
        }

        for i in 0..16 {
            assert!(src.push(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 5);

        for i in 0..27 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 0..5 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);

        for i in 5..16 {
            assert_eq!(src.pop(), Some(i));
        }

        assert_eq!(src.pop(), None);
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by space left in a wrapped spmc
    ///
    #[test]
    fn test_push_mpmc_multi_3() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 0..32 {
            assert_eq!(dst_pc.push(i, &src), Ok(()));
        }

        for i in 0..7 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 0..16 {
            assert!(src.push(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 7);

        for i in 7..32 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 0..7 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);

        for i in 7..16 {
            assert_eq!(src.pop(), Some(i));
        }

        assert_eq!(src.pop(), None);
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by items left in mpmc
    ///
    #[test]
    fn test_push_mpmc_multi_4() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 0..3 {
            assert_eq!(dst_pc.push(i, &src), Ok(()));
        }

        for i in 0..4 {
            assert!(src.push(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 4);

        for i in 0..3 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 0..4 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);
        assert_eq!(src.pop(), None);
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by items left in a wrapped mpmc
    /// with head at index 0
    ///
    #[test]
    fn test_push_mpmc_multi_5() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 0..3 {
            assert_eq!(dst_pc.push(i, &src), Ok(()));
        }

        for i in 0..16 {
            assert!(src.push(i));
        }

        for i in 0..8 {
            assert_eq!(src.pop(), Some(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 8);

        for i in 0..3 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 8..16 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);
        assert_eq!(src.pop(), None);
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by items left in a wrapped mpmc
    /// with head < than tail
    ///
    #[test]
    fn test_push_mpmc_multi_6() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 100..106 {
            assert_eq!(dst_pc.push(i, &src), Ok(()));
        }

        for i in 0..16 {
            assert!(src.push(i));
        }

        for i in 0..12 {
            assert_eq!(src.pop(), Some(i));
        }

        for i in 16..17 {
            assert!(src.push(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 5);

        for i in 100..106 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 12..17 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);
        assert_eq!(src.pop(), None);
    }

    ///
    /// Number of items transfered from mpmc -> spmc is limited by items left in a wrapped mpmc
    /// with head at the end
    ///
    #[test]
    fn test_push_mpmc_multi_7() {
        let dst: SpmcStealQueue<u32> = SpmcStealQueue::new(32);
        let dst_pc = Arc::new(dst).get_boundedl().unwrap();

        let src: MpmcQueue<u32> = MpmcQueue::new(16);
        for i in 0..3 {
            assert_eq!(dst_pc.push(i, &src), Ok(()));
        }

        for i in 0..14 {
            assert!(src.push(i));
        }

        for i in 0..8 {
            assert_eq!(src.pop(), Some(i));
        }

        assert_eq!(dst_pc.fetch_from(&src), 6);

        for i in 0..3 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        for i in 8..14 {
            assert_eq!(dst_pc.pop(), Some(i));
        }

        assert_eq!(dst_pc.pop(), None);
        assert_eq!(src.pop(), None);
    }
}