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
//! A concurrent work-stealing deque.
//!
//! The data structure can be thought of as a dynamically growable and shrinkable buffer that has
//! two ends: bottom and top. A [`Deque`] can [`push`] elements into the bottom and [`pop`]
//! elements from the bottom, but it can only [`steal`][Deque::steal] elements from the top.
//!
//! A [`Deque`] doesn't implement `Sync` so it cannot be shared among multiple threads. However, it
//! can create [`Stealer`]s, and those can be easily cloned, shared, and sent to other threads.
//! [`Stealer`]s can only [`steal`][Stealer::steal] elements from the top.
//!
//! Here's a visualization of the data structure:
//!
//! ```text
//!                    top
//!                     _
//!    Deque::steal -> | | <- Stealer::steal
//!                    | |
//!                    | |
//!                    | |
//! Deque::push/pop -> |_|
//!
//!                  bottom
//! ```
//!
//! # Work-stealing schedulers
//!
//! Usually, the data structure is used in work-stealing schedulers as follows.
//!
//! There is a number of threads. Each thread owns a [`Deque`] and creates a [`Stealer`] that is
//! shared among all other threads. Alternatively, it creates multiple [`Stealer`]s - one for each
//! of the other threads.
//!
//! Then, all threads are executing in a loop. In the loop, each one attempts to [`pop`] some work
//! from its own [`Deque`]. But if it is empty, it attempts to [`steal`][Stealer::steal] work from
//! some other thread instead. When executing work (or being idle), a thread may produce more work,
//! which gets [`push`]ed into its [`Deque`].
//!
//! Of course, there are many variations of this strategy. For example, sometimes it may be
//! beneficial for a thread to always [`steal`][Deque::steal] work from the top of its deque
//! instead of calling [`pop`] and taking it from the bottom.
//!
//! # Examples
//!
//! ```
//! use crossbeam_deque::{Deque, Steal};
//! use std::thread;
//!
//! let d = Deque::new();
//! let s = d.stealer();
//!
//! d.push('a');
//! d.push('b');
//! d.push('c');
//!
//! assert_eq!(d.pop(), Some('c'));
//! drop(d);
//!
//! thread::spawn(move || {
//!     assert_eq!(s.steal(), Steal::Data('a'));
//!     assert_eq!(s.steal(), Steal::Data('b'));
//! }).join().unwrap();
//! ```
//!
//! # References
//!
//! The implementation is based on the following work:
//!
//! 1. [Chase and Lev. Dynamic circular work-stealing deque. SPAA 2005.][chase-lev]
//! 2. [Le, Pop, Cohen, and Nardelli. Correct and efficient work-stealing for weak memory models.
//!    PPoPP 2013.][weak-mem]
//! 3. [Norris and Demsky. CDSchecker: checking concurrent data structures written with C/C++
//!    atomics. OOPSLA 2013.][checker]
//!
//! [chase-lev]: https://dl.acm.org/citation.cfm?id=1073974
//! [weak-mem]: https://dl.acm.org/citation.cfm?id=2442524
//! [checker]: https://dl.acm.org/citation.cfm?id=2509514
//!
//! [`Deque`]: struct.Deque.html
//! [`Stealer`]: struct.Stealer.html
//! [`push`]: struct.Deque.html#method.push
//! [`pop`]: struct.Deque.html#method.pop
//! [Deque::steal]: struct.Deque.html#method.steal
//! [Stealer::steal]: struct.Stealer.html#method.steal

extern crate crossbeam_epoch as epoch;
extern crate crossbeam_utils as utils;

use std::cmp;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::ptr;
use std::sync::Arc;
use std::sync::atomic::{self, AtomicIsize};
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};

use epoch::{Atomic, Owned};
use utils::cache_padded::CachePadded;

/// Minimum buffer capacity for a deque.
const DEFAULT_MIN_CAP: usize = 16;

/// If a buffer of at least this size is retired, thread-local garbage is flushed so that it gets
/// deallocated as soon as possible.
const FLUSH_THRESHOLD_BYTES: usize = 1 << 10;

/// Possible outcomes of a steal operation.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub enum Steal<T> {
    /// The deque was empty at the time of stealing.
    Empty,

    /// Some data has been successfully stolen.
    Data(T),

    /// Lost the race for stealing data to another concurrent operation. Try again.
    Retry,
}

/// A buffer that holds elements in a deque.
struct Buffer<T> {
    /// Pointer to the allocated memory.
    ptr: *mut T,

    /// Capacity of the buffer. Always a power of two.
    cap: usize,
}

unsafe impl<T> Send for Buffer<T> {}

impl<T> Buffer<T> {
    /// Returns a new buffer with the specified capacity.
    fn new(cap: usize) -> Self {
        debug_assert_eq!(cap, cap.next_power_of_two());

        let mut v = Vec::with_capacity(cap);
        let ptr = v.as_mut_ptr();
        mem::forget(v);

        Buffer { ptr, cap }
    }

    /// Returns a pointer to the element at the specified `index`.
    unsafe fn at(&self, index: isize) -> *mut T {
        // `self.cap` is always a power of two.
        self.ptr.offset(index & (self.cap - 1) as isize)
    }

    /// Writes `value` into the specified `index`.
    unsafe fn write(&self, index: isize, value: T) {
        ptr::write(self.at(index), value)
    }

    /// Reads a value from the specified `index`.
    unsafe fn read(&self, index: isize) -> T {
        ptr::read(self.at(index))
    }
}

impl<T> Drop for Buffer<T> {
    fn drop(&mut self) {
        unsafe {
            drop(Vec::from_raw_parts(self.ptr, 0, self.cap));
        }
    }
}

/// Internal data that is shared between the deque and its stealers.
struct Inner<T> {
    /// The bottom index.
    bottom: AtomicIsize,

    /// The top index.
    top: AtomicIsize,

    /// The underlying buffer.
    buffer: Atomic<Buffer<T>>,

    /// Minimum capacity of the buffer. Always a power of two.
    min_cap: usize,
}

impl<T> Inner<T> {
    /// Returns a new `Inner` with default minimum capacity.
    fn new() -> Self {
        Self::with_min_capacity(DEFAULT_MIN_CAP)
    }

    /// Returns a new `Inner` with minimum capacity of `min_cap` rounded to the next power of two.
    fn with_min_capacity(min_cap: usize) -> Self {
        let power = min_cap.next_power_of_two();
        assert!(power >= min_cap, "capacity too large: {}", min_cap);
        Inner {
            bottom: AtomicIsize::new(0),
            top: AtomicIsize::new(0),
            buffer: Atomic::new(Buffer::new(power)),
            min_cap: power,
        }
    }

    /// Resizes the internal buffer to the new capacity of `new_cap`.
    #[cold]
    unsafe fn resize(&self, new_cap: usize) {
        // Load the bottom, top, and buffer.
        let b = self.bottom.load(Relaxed);
        let t = self.top.load(Relaxed);

        let buffer = self.buffer.load(Relaxed, epoch::unprotected());

        // Allocate a new buffer.
        let new = Buffer::new(new_cap);

        // Copy data from the old buffer to the new one.
        let mut i = t;
        while i != b {
            ptr::copy_nonoverlapping(buffer.deref().at(i), new.at(i), 1);
            i = i.wrapping_add(1);
        }

        let guard = &epoch::pin();

        // Replace the old buffer with the new one.
        let old = self.buffer
            .swap(Owned::new(new).into_shared(guard), Release, guard);

        // Destroy the old buffer later.
        guard.defer(move || old.into_owned());

        // If the buffer is very large, then flush the thread-local garbage in order to
        // deallocate it as soon as possible.
        if mem::size_of::<T>() * new_cap >= FLUSH_THRESHOLD_BYTES {
            guard.flush();
        }
    }
}

impl<T> Drop for Inner<T> {
    fn drop(&mut self) {
        // Load the bottom, top, and buffer.
        let b = self.bottom.load(Relaxed);
        let t = self.top.load(Relaxed);

        unsafe {
            let buffer = self.buffer.load(Relaxed, epoch::unprotected());

            // Go through the buffer from top to bottom and drop all elements in the deque.
            let mut i = t;
            while i != b {
                ptr::drop_in_place(buffer.deref().at(i));
                i = i.wrapping_add(1);
            }

            // Free the memory allocated by the buffer.
            drop(buffer.into_owned());
        }
    }
}

/// A concurrent work-stealing deque.
///
/// A deque has two ends: bottom and top. Elements can be [`push`]ed into the bottom and [`pop`]ped
/// from the bottom. The top end is special in that elements can only be stolen from it using the
/// [`steal`][Deque::steal] method.
///
/// # Stealers
///
/// While [`Deque`] doesn't implement `Sync`, it can create [`Stealer`]s using the method
/// [`stealer`][stealer], and those can be easily shared among multiple threads. [`Stealer`]s can
/// only [`steal`][Stealer::steal] elements from the top end of the deque.
///
/// # Capacity
///
/// The data structure is dynamically grows as elements are inserted and removed from it. If the
/// internal buffer gets full, a new one twice the size of the original is allocated. Similarly,
/// if it is less than a quarter full, a new buffer half the size of the original is allocated.
///
/// In order to prevent frequent resizing (reallocations may be costly), it is possible to specify
/// a large minimum capacity for the deque by calling [`Deque::with_min_capacity`]. This
/// constructor will make sure that the internal buffer never shrinks below that size.
///
/// # Examples
///
/// ```
/// use crossbeam_deque::{Deque, Steal};
///
/// let d = Deque::with_min_capacity(1000);
/// let s = d.stealer();
///
/// d.push('a');
/// d.push('b');
/// d.push('c');
///
/// assert_eq!(d.pop(), Some('c'));
/// assert_eq!(d.steal(), Steal::Data('a'));
/// assert_eq!(s.steal(), Steal::Data('b'));
/// ```
///
/// [`Deque`]: struct.Deque.html
/// [`Stealer`]: struct.Stealer.html
/// [`push`]: struct.Deque.html#method.push
/// [`pop`]: struct.Deque.html#method.pop
/// [stealer]: struct.Deque.html#method.stealer
/// [`Deque::with_min_capacity`]: struct.Deque.html#method.with_min_capacity
/// [Deque::steal]: struct.Deque.html#method.steal
/// [Stealer::steal]: struct.Stealer.html#method.steal
pub struct Deque<T> {
    inner: Arc<CachePadded<Inner<T>>>,
    _marker: PhantomData<*mut ()>, // !Send + !Sync
}

unsafe impl<T: Send> Send for Deque<T> {}

impl<T> Deque<T> {
    /// Returns a new deque.
    ///
    /// The internal buffer is destructed as soon as the deque and all its stealers get dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::<i32>::new();
    /// ```
    pub fn new() -> Deque<T> {
        Deque {
            inner: Arc::new(CachePadded::new(Inner::new())),
            _marker: PhantomData,
        }
    }

    /// Returns a new deque with the specified minimum capacity.
    ///
    /// If the capacity is not a power of two, it will be rounded up to the next one.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// // The minimum capacity will be rounded up to 1024.
    /// let d = Deque::<i32>::with_min_capacity(1000);
    /// assert_eq!(d.min_capacity(), 1024);
    /// assert_eq!(d.capacity(), 1024);
    /// ```
    pub fn with_min_capacity(min_cap: usize) -> Deque<T> {
        Deque {
            inner: Arc::new(CachePadded::new(Inner::with_min_capacity(min_cap))),
            _marker: PhantomData,
        }
    }

    /// Returns `true` if the deque is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::new();
    /// assert!(d.is_empty());
    /// d.push("foo");
    /// assert!(!d.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of elements in the deque.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::new();
    /// d.push('a');
    /// d.push('b');
    /// d.push('c');
    /// assert_eq!(d.len(), 3);
    /// ```
    pub fn len(&self) -> usize {
        let b = self.inner.bottom.load(Relaxed);
        let t = self.inner.top.load(Relaxed);
        b.wrapping_sub(t) as usize
    }

    /// Returns the minimum capacity of the deque.
    ///
    /// The minimum capacity can be specified in [`Deque::with_min_capacity`].
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// // Gets rounded to the next power of two.
    /// let d = Deque::<i32>::with_min_capacity(50);
    /// assert_eq!(d.min_capacity(), 64);
    /// assert_eq!(d.capacity(), 64);
    /// ```
    ///
    /// [`Deque::with_min_capacity`]: struct.Deque.html#method.with_min_capacity
    pub fn min_capacity(&self) -> usize {
        self.inner.min_cap
    }

    /// Returns the number of elements the deque can hold without reallocating.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::with_min_capacity(50);
    /// assert_eq!(d.capacity(), 64);
    ///
    /// for i in 0..200 {
    ///     d.push(i);
    /// }
    /// assert_eq!(d.capacity(), 256);
    /// ```
    pub fn capacity(&self) -> usize {
        unsafe {
            let buf = self.inner.buffer.load(Relaxed, epoch::unprotected());
            buf.deref().cap
        }
    }

    /// Shrinks the capacity of the deque as much as possible.
    ///
    /// The capacity will drop down as close as possible to the length but there may still be some
    /// free space left.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// // Insert a lot of elements. This makes the buffer grow.
    /// let d = Deque::new();
    /// for i in 0..200 {
    ///     d.push(i);
    /// }
    ///
    /// // Remove all elements.
    /// let s = d.stealer();
    /// for i in 0..200 {
    ///     s.steal();
    /// }
    ///
    /// // Stealers cannot shrink the buffer, so the capacity is still very large.
    /// assert!(d.capacity() >= 200);
    ///
    /// // Shrink the buffer. The capacity drops down, but some free space may still be left.
    /// d.shrink_to_fit();
    /// assert!(d.capacity() < 50);
    /// ```
    pub fn shrink_to_fit(&self) {
        let b = self.inner.bottom.load(Relaxed);
        let t = self.inner.top.load(Acquire);
        let cap = self.capacity();
        let len = b.wrapping_sub(t);

        // Shrink the capacity as much as possible without overshooting `min_cap` or `len`.
        let mut new_cap = cap;
        while self.inner.min_cap <= new_cap / 2 && len <= new_cap as isize / 2 {
            new_cap /= 2;
        }

        if new_cap != cap {
            unsafe {
                self.inner.resize(new_cap);
            }
        }
    }

    /// Pushes an element into the bottom of the deque.
    ///
    /// If the internal buffer is full, a new one twice the capacity of the current one will be
    /// allocated.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::new();
    /// d.push(1);
    /// d.push(2);
    /// ```
    pub fn push(&self, value: T) {
        unsafe {
            // Load the bottom, top, and buffer. The buffer doesn't have to be epoch-protected
            // because the current thread (the worker) is the only one that grows and shrinks it.
            let b = self.inner.bottom.load(Relaxed);
            let t = self.inner.top.load(Acquire);

            let mut buffer = self.inner.buffer.load(Relaxed, epoch::unprotected());

            // Calculate the length of the deque.
            let len = b.wrapping_sub(t);

            let cap = buffer.deref().cap;
            // Is the deque full?
            if len >= cap as isize {
                // Yes. Grow the underlying buffer.
                self.inner.resize(2 * cap);
                buffer = self.inner.buffer.load(Relaxed, epoch::unprotected());
            // Is the new length less than one fourth the capacity?
            } else if cap > self.inner.min_cap && len + 1 < cap as isize / 4 {
                // Yes. Shrink the underlying buffer.
                self.inner.resize(cap / 2);
                buffer = self.inner.buffer.load(Relaxed, epoch::unprotected());
            }

            // Write `value` into the right slot and increment `b`.
            buffer.deref().write(b, value);
            atomic::fence(Release);
            self.inner.bottom.store(b.wrapping_add(1), Relaxed);
        }
    }

    /// Pops an element from the bottom of the deque.
    ///
    /// If the internal buffer is less than a quarter full, a new buffer half the capacity of the
    /// current one will be allocated.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::new();
    /// d.push(1);
    /// d.push(2);
    ///
    /// assert_eq!(d.pop(), Some(2));
    /// assert_eq!(d.pop(), Some(1));
    /// assert_eq!(d.pop(), None);
    /// ```
    pub fn pop(&self) -> Option<T> {
        // Load the bottom.
        let b = self.inner.bottom.load(Relaxed);

        // If the deque is empty, return early without incurring the cost of a SeqCst fence.
        let t = self.inner.top.load(Relaxed);
        if b.wrapping_sub(t) <= 0 {
            return None;
        }

        // Decrement the bottom.
        let b = b.wrapping_sub(1);
        self.inner.bottom.store(b, Relaxed);

        // Load the buffer. The buffer doesn't have to be epoch-protected because the current
        // thread (the worker) is the only one that grows and shrinks it.
        let buf = unsafe { self.inner.buffer.load(Relaxed, epoch::unprotected()) };

        atomic::fence(SeqCst);

        // Load the top.
        let t = self.inner.top.load(Relaxed);

        // Compute the length after the bottom was decremented.
        let len = b.wrapping_sub(t);

        if len < 0 {
            // The deque is empty. Restore the bottom back to the original value.
            self.inner.bottom.store(b.wrapping_add(1), Relaxed);
            None
        } else {
            // Read the value to be popped.
            let mut value = unsafe { Some(buf.deref().read(b)) };

            // Are we popping the last element from the deque?
            if len == 0 {
                // Try incrementing the top.
                if self.inner
                    .top
                    .compare_exchange(t, t.wrapping_add(1), SeqCst, Relaxed)
                    .is_err()
                {
                    // Failed. We didn't pop anything.
                    mem::forget(value.take());
                }

                // Restore the bottom back to the original value.
                self.inner.bottom.store(b.wrapping_add(1), Relaxed);
            } else {
                // Shrink the buffer if `len` is less than one fourth of `self.inner.min_cap`.
                unsafe {
                    let cap = buf.deref().cap;
                    if cap > self.inner.min_cap && len < cap as isize / 4 {
                        self.inner.resize(cap / 2);
                    }
                }
            }

            value
        }
    }

    /// Steals an element from the top of the deque.
    ///
    /// Unlike most methods in concurrent data structures, if another operation gets in the way
    /// while attempting to steal data, this method will return immediately with [`Steal::Retry`]
    /// instead of retrying.
    ///
    /// If the internal buffer is less than a quarter full, a new buffer half the capacity of the
    /// current one will be allocated.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::{Deque, Steal};
    ///
    /// let d = Deque::new();
    /// d.push(1);
    /// d.push(2);
    ///
    /// // Attempt to steal an element.
    /// //
    /// // No other threads are working with the deque, so this time we know for sure that we
    /// // won't get `Steal::Retry` as the result.
    /// assert_eq!(d.steal(), Steal::Data(1));
    ///
    /// // Attempt to steal an element, but keep retrying if we get `Retry`.
    /// let stolen = loop {
    ///     match d.steal() {
    ///         Steal::Empty => break None,
    ///         Steal::Data(data) => break Some(data),
    ///         Steal::Retry => {}
    ///     }
    /// };
    /// assert_eq!(stolen, Some(2));
    /// ```
    ///
    /// [`Steal::Retry`]: enum.Steal.html#variant.Retry
    pub fn steal(&self) -> Steal<T> {
        let b = self.inner.bottom.load(Relaxed);
        let buf = unsafe { self.inner.buffer.load(Relaxed, epoch::unprotected()) };
        let t = self.inner.top.load(Relaxed);
        let len = b.wrapping_sub(t);

        // Is the deque empty?
        if len <= 0 {
            return Steal::Empty;
        }

        // Try incrementing the top to steal the value.
        if self.inner
            .top
            .compare_exchange(t, t.wrapping_add(1), SeqCst, Relaxed)
            .is_ok()
        {
            let data = unsafe { buf.deref().read(t) };

            // Shrink the buffer if `len - 1` is less than one fourth of `self.inner.min_cap`.
            unsafe {
                let cap = buf.deref().cap;
                if cap > self.inner.min_cap && len <= cap as isize / 4 {
                    self.inner.resize(cap / 2);
                }
            }

            return Steal::Data(data);
        }

        Steal::Retry
    }

    /// Creates a stealer that can be shared with other threads.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::{Deque, Steal};
    /// use std::thread;
    ///
    /// let d = Deque::new();
    /// d.push(1);
    /// d.push(2);
    ///
    /// let s = d.stealer();
    ///
    /// thread::spawn(move || {
    ///     assert_eq!(s.steal(), Steal::Data(1));
    /// }).join().unwrap();
    /// ```
    pub fn stealer(&self) -> Stealer<T> {
        Stealer {
            inner: self.inner.clone(),
            _marker: PhantomData,
        }
    }
}

impl<T> fmt::Debug for Deque<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Deque {{ ... }}")
    }
}

impl<T> Default for Deque<T> {
    fn default() -> Deque<T> {
        Deque::new()
    }
}

/// A stealer that steals elements from the top of a deque.
///
/// The only operation a stealer can do that manipulates the deque is [`steal`].
///
/// Stealers can be cloned in order to create more of them. They also implement `Send` and `Sync`
/// so they can be easily shared among multiple threads.
///
/// [`steal`]: struct.Stealer.html#method.steal
pub struct Stealer<T> {
    inner: Arc<CachePadded<Inner<T>>>,
    _marker: PhantomData<*mut ()>, // !Send + !Sync
}

unsafe impl<T: Send> Send for Stealer<T> {}
unsafe impl<T: Send> Sync for Stealer<T> {}

impl<T> Stealer<T> {
    /// Returns `true` if the deque is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::new();
    /// d.push("foo");
    ///
    /// let s = d.stealer();
    /// assert!(!d.is_empty());
    /// s.steal();
    /// assert!(d.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of elements in the deque.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::Deque;
    ///
    /// let d = Deque::new();
    /// let s = d.stealer();
    /// d.push('a');
    /// d.push('b');
    /// d.push('c');
    /// assert_eq!(s.len(), 3);
    /// ```
    pub fn len(&self) -> usize {
        let t = self.inner.top.load(Relaxed);
        atomic::fence(SeqCst);
        let b = self.inner.bottom.load(Relaxed);
        cmp::max(b.wrapping_sub(t), 0) as usize
    }

    /// Steals an element from the top of the deque.
    ///
    /// Unlike most methods in concurrent data structures, if another operation gets in the way
    /// while attempting to steal data, this method will return immediately with [`Steal::Retry`]
    /// instead of retrying.
    ///
    /// This method will not attempt to resize the internal buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use crossbeam_deque::{Deque, Steal};
    ///
    /// let d = Deque::new();
    /// let s = d.stealer();
    /// d.push(1);
    /// d.push(2);
    ///
    /// // Attempt to steal an element, but keep retrying if we get `Retry`.
    /// let stolen = loop {
    ///     match s.steal() {
    ///         Steal::Empty => break None,
    ///         Steal::Data(data) => break Some(data),
    ///         Steal::Retry => {}
    ///     }
    /// };
    /// assert_eq!(stolen, Some(1));
    /// ```
    ///
    /// [`Steal::Retry`]: enum.Steal.html#variant.Retry
    pub fn steal(&self) -> Steal<T> {
        // Load the top.
        let t = self.inner.top.load(Acquire);

        // A SeqCst fence is needed here.
        // If the current thread is already pinned (reentrantly), we must manually issue the fence.
        // Otherwise, the following pinning will issue the fence anyway, so we don't have to.
        if epoch::is_pinned() {
            atomic::fence(SeqCst);
        }

        let guard = &epoch::pin();

        // Load the bottom.
        let b = self.inner.bottom.load(Acquire);

        // Is the deque empty?
        if b.wrapping_sub(t) <= 0 {
            return Steal::Empty;
        }

        // Load the buffer and read the value at the top.
        let buf = self.inner.buffer.load(Acquire, guard);
        let value = unsafe { buf.deref().read(t) };

        // Try incrementing the top to steal the value.
        if self.inner
            .top
            .compare_exchange(t, t.wrapping_add(1), SeqCst, Relaxed)
            .is_ok()
        {
            return Steal::Data(value);
        }

        // We didn't steal this value, forget it.
        mem::forget(value);

        Steal::Retry
    }
}

impl<T> Clone for Stealer<T> {
    /// Creates another stealer.
    fn clone(&self) -> Stealer<T> {
        Stealer {
            inner: self.inner.clone(),
            _marker: PhantomData,
        }
    }
}

impl<T> fmt::Debug for Stealer<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Stealer {{ ... }}")
    }
}

#[cfg(test)]
mod tests {
    extern crate rand;

    use std::sync::{Arc, Mutex};
    use std::sync::atomic::{AtomicBool, AtomicUsize};
    use std::sync::atomic::Ordering::SeqCst;
    use std::thread;

    use epoch;
    use self::rand::Rng;

    use super::{Deque, Steal};

    #[test]
    fn smoke() {
        let d = Deque::new();
        let s = d.stealer();
        assert_eq!(d.pop(), None);
        assert_eq!(s.steal(), Steal::Empty);
        assert_eq!(d.len(), 0);
        assert_eq!(s.len(), 0);

        d.push(1);
        assert_eq!(d.len(), 1);
        assert_eq!(s.len(), 1);
        assert_eq!(d.pop(), Some(1));
        assert_eq!(d.pop(), None);
        assert_eq!(s.steal(), Steal::Empty);
        assert_eq!(d.len(), 0);
        assert_eq!(s.len(), 0);

        d.push(2);
        assert_eq!(s.steal(), Steal::Data(2));
        assert_eq!(s.steal(), Steal::Empty);
        assert_eq!(d.pop(), None);

        d.push(3);
        d.push(4);
        d.push(5);
        assert_eq!(d.steal(), Steal::Data(3));
        assert_eq!(s.steal(), Steal::Data(4));
        assert_eq!(d.steal(), Steal::Data(5));
        assert_eq!(d.steal(), Steal::Empty);
    }

    #[test]
    fn steal_push() {
        const STEPS: usize = 50_000;

        let d = Deque::new();
        let s = d.stealer();
        let t = thread::spawn(move || for i in 0..STEPS {
            loop {
                if let Steal::Data(v) = s.steal() {
                    assert_eq!(i, v);
                    break;
                }
            }
        });

        for i in 0..STEPS {
            d.push(i);
        }
        t.join().unwrap();
    }

    #[test]
    fn stampede() {
        const COUNT: usize = 50_000;

        let d = Deque::new();

        for i in 0..COUNT {
            d.push(Box::new(i + 1));
        }
        let remaining = Arc::new(AtomicUsize::new(COUNT));

        let threads = (0..8)
            .map(|_| {
                let s = d.stealer();
                let remaining = remaining.clone();

                thread::spawn(move || {
                    let mut last = 0;
                    while remaining.load(SeqCst) > 0 {
                        if let Steal::Data(x) = s.steal() {
                            assert!(last < *x);
                            last = *x;
                            remaining.fetch_sub(1, SeqCst);
                        }
                    }
                })
            })
            .collect::<Vec<_>>();

        let mut last = COUNT + 1;
        while remaining.load(SeqCst) > 0 {
            if let Some(x) = d.pop() {
                assert!(last > *x);
                last = *x;
                remaining.fetch_sub(1, SeqCst);
            }
        }

        for t in threads {
            t.join().unwrap();
        }
    }

    fn run_stress() {
        const COUNT: usize = 50_000;

        let d = Deque::new();
        let done = Arc::new(AtomicBool::new(false));
        let hits = Arc::new(AtomicUsize::new(0));

        let threads = (0..8)
            .map(|_| {
                let s = d.stealer();
                let done = done.clone();
                let hits = hits.clone();

                thread::spawn(move || while !done.load(SeqCst) {
                    if let Steal::Data(_) = s.steal() {
                        hits.fetch_add(1, SeqCst);
                    }
                })
            })
            .collect::<Vec<_>>();

        let mut rng = rand::thread_rng();
        let mut expected = 0;
        while expected < COUNT {
            if rng.gen_range(0, 3) == 0 {
                if d.pop().is_some() {
                    hits.fetch_add(1, SeqCst);
                }
            } else {
                d.push(expected);
                expected += 1;
            }
        }

        while hits.load(SeqCst) < COUNT {
            if d.pop().is_some() {
                hits.fetch_add(1, SeqCst);
            }
        }
        done.store(true, SeqCst);

        for t in threads {
            t.join().unwrap();
        }
    }

    #[test]
    fn stress() {
        run_stress();
    }

    #[test]
    fn stress_pinned() {
        let _guard = epoch::pin();
        run_stress();
    }

    #[test]
    fn no_starvation() {
        const COUNT: usize = 50_000;

        let d = Deque::new();
        let done = Arc::new(AtomicBool::new(false));

        let (threads, hits): (Vec<_>, Vec<_>) = (0..8)
            .map(|_| {
                let s = d.stealer();
                let done = done.clone();
                let hits = Arc::new(AtomicUsize::new(0));

                let t = {
                    let hits = hits.clone();
                    thread::spawn(move || while !done.load(SeqCst) {
                        if let Steal::Data(_) = s.steal() {
                            hits.fetch_add(1, SeqCst);
                        }
                    })
                };

                (t, hits)
            })
            .unzip();

        let mut rng = rand::thread_rng();
        let mut my_hits = 0;
        loop {
            for i in 0..rng.gen_range(0, COUNT) {
                if rng.gen_range(0, 3) == 0 && my_hits == 0 {
                    if d.pop().is_some() {
                        my_hits += 1;
                    }
                } else {
                    d.push(i);
                }
            }

            if my_hits > 0 && hits.iter().all(|h| h.load(SeqCst) > 0) {
                break;
            }
        }
        done.store(true, SeqCst);

        for t in threads {
            t.join().unwrap();
        }
    }

    #[test]
    fn destructors() {
        const COUNT: usize = 50_000;

        struct Elem(usize, Arc<Mutex<Vec<usize>>>);

        impl Drop for Elem {
            fn drop(&mut self) {
                self.1.lock().unwrap().push(self.0);
            }
        }

        let d = Deque::new();

        let dropped = Arc::new(Mutex::new(Vec::new()));
        let remaining = Arc::new(AtomicUsize::new(COUNT));
        for i in 0..COUNT {
            d.push(Elem(i, dropped.clone()));
        }

        let threads = (0..8)
            .map(|_| {
                let s = d.stealer();
                let remaining = remaining.clone();

                thread::spawn(move || for _ in 0..1000 {
                    if let Steal::Data(_) = s.steal() {
                        remaining.fetch_sub(1, SeqCst);
                    }
                })
            })
            .collect::<Vec<_>>();

        for _ in 0..1000 {
            if d.pop().is_some() {
                remaining.fetch_sub(1, SeqCst);
            }
        }

        for t in threads {
            t.join().unwrap();
        }

        let rem = remaining.load(SeqCst);
        assert!(rem > 0);
        assert_eq!(d.len(), rem);

        {
            let mut v = dropped.lock().unwrap();
            assert_eq!(v.len(), COUNT - rem);
            v.clear();
        }

        drop(d);

        {
            let mut v = dropped.lock().unwrap();
            assert_eq!(v.len(), rem);
            v.sort();
            for pair in v.windows(2) {
                assert_eq!(pair[0] + 1, pair[1]);
            }
        }
    }
}