      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

    //! A mutex-like lock with a conserved global ordering which can be shared
    //! between threads and can interact with OpenCL events.
    //!
    //!
    //! TODO: Add doc links.
    //
    //

    use std::marker::PhantomData;
    use std::ops::{Deref, DerefMut};
    use futures::{Future, Poll, Async};
    use futures::sync::oneshot::{self, Receiver};
    use core::{ClContextPtr, ClNullEventPtr};
    use error::{Error as OclError, Result as OclResult};
    use ::{Event, EventList};
    use async::qutex::{QrwLock, QrwRequest, RequestKind};

    const PRINT_DEBUG: bool = false;

    pub type FutureReadGuard<V> = FutureGuard<V, ReadGuard<V>>;
    pub type FutureWriteGuard<V> = FutureGuard<V, WriteGuard<V>>;

    /// Prints a debugging message.
    fn print_debug(id: usize, msg: &str) {
        if PRINT_DEBUG {
            println!("###### [{}] {} (thread: {})", id, msg,
                ::std::thread::current().name().unwrap_or("<unnamed>"));
        }
    }

    /// Extracts an `OrderLock` from a guard of either type.
    //
    // This saves us two unnecessary atomic stores (the reference count of lock
    // going up then down when releasing or up/downgrading) which would occur if
    // we were to clone then drop.
    unsafe fn extract_order_lock<V, G: OrderGuard<V>>(guard: G) -> OrderLock<V> {
        let order_lock = ::std::ptr::read(guard.order_lock());
        guard.forget();
        order_lock
    }

    /// A read or write guard for an `OrderLock`.
    pub trait OrderGuard<V> where Self: ::std::marker::Sized {
        fn new(order_lock: OrderLock<V>, release_event: Option<Event>) -> Self;
        fn order_lock(&self) -> &OrderLock<V>;

        unsafe fn forget(self) {
            ::std::mem::forget(self);
        }
    }

    // /// A guard that releases its lock and completes its release event
    // /// immediately.
    // pub struct VoidGuard();

    // impl<V> OrderGuard<V> for VoidGuard {
    //     fn new(_order_lock: OrderLock<V>, release_event: Option<Event>) -> VoidGuard {
    //         // order_lock.lock.release_read_lock();

    //         if let Some(ref e) = release_event {
    //             if !e.is_complete().expect("VoidGuard::new") {
    //                 print_debug(0, "VoidGuard::new: setting release event complete");
    //                 e.set_complete().expect("VoidGuard::new");
    //             }
    //         }

    //         VoidGuard()
    //     }

    //     fn order_lock(&self) -> &OrderLock<V> {
    //         panic!("`::order_lock` not implemented for `VoidGuard`.");
    //     }
    // }

    /// Allows access to the data contained within a lock just like a mutex guard.
    #[derive(Debug)]
    pub struct ReadGuard<V> {
        order_lock: OrderLock<V>,
        release_event: Option<Event>,
    }

    impl<V> ReadGuard<V> {
        /// Returns a new `ReadGuard`.
        fn new(order_lock: OrderLock<V>, release_event: Option<Event>) -> ReadGuard<V> {
            print_debug(order_lock.id(), "ReadGuard::new: read lock acquired");
            ReadGuard {
                order_lock: order_lock,
                release_event: release_event,
            }
        }

        /// Returns a reference to the event previously set using
        /// `create_release_event` on the `FutureGuard` which preceded this
        /// `ReadGuard`. The event can be manually 'triggered' by calling
        /// `...set_complete()...` or used normally (as a wait event) by
        /// subsequent commands. If the event is not manually completed it will be
        /// automatically set complete when this `ReadGuard` is dropped.
        pub fn release_event(guard: &ReadGuard<V>) -> Option<&Event> {
            guard.release_event.as_ref()
        }

        /// Triggers the release event and releases the lock held by this `ReadGuard`
        /// before returning the original `OrderLock`.
        pub fn release(mut guard: ReadGuard<V>) -> OrderLock<V> {
            print_debug(guard.order_lock.id(), "WriteGuard::release: releasing read lock");
            unsafe {
                Self::release_components(&mut guard);
                extract_order_lock(guard)
            }
        }

        /// Triggers the release event by setting it complete.
        fn complete_release_event(guard: &mut ReadGuard<V>) {
            if let Some(ref e) = guard.release_event.take() {
                if !e.is_complete().expect("ReadGuard::complete_release_event") {
                    print_debug(guard.order_lock.id(), "ReadGuard::complete_release_event: \
                        setting release event complete");
                    e.set_complete().expect("ReadGuard::complete_release_event");
                }
            }
        }

        /// Releases the lock and completes the release event.
        unsafe fn release_components(guard: &mut ReadGuard<V>) {
            guard.order_lock.lock.release_read_lock();
            Self::complete_release_event(guard);
        }
    }

    impl<V> Deref for ReadGuard<V> {
        type Target = V;

        fn deref(&self) -> &V {
            unsafe { &*self.order_lock.lock.as_ptr() }
        }
    }

    impl<V> Drop for ReadGuard<V> {
        fn drop(&mut self) {
            print_debug(self.order_lock.id(), "dropping and releasing ReadGuard");
            unsafe { Self::release_components(self) }
        }
    }

    impl<V> OrderGuard<V> for ReadGuard<V> {
        fn new(order_lock: OrderLock<V>, release_event: Option<Event>) -> ReadGuard<V> {
            ReadGuard::new(order_lock, release_event)
        }

        fn order_lock(&self) -> &OrderLock<V> {
            &self.order_lock
        }
    }

    /// Allows access to the data contained within just like a mutex guard.
    #[derive(Debug)]
    pub struct WriteGuard<V> {
        order_lock: OrderLock<V>,
        release_event: Option<Event>,
    }

    impl<V> WriteGuard<V> {
        /// Returns a new `WriteGuard`.
        fn new(order_lock: OrderLock<V>, release_event: Option<Event>) -> WriteGuard<V> {
            print_debug(order_lock.id(), "WriteGuard::new: Write lock acquired");
            WriteGuard {
                order_lock: order_lock,
                release_event: release_event,
            }
        }

        /// Returns a reference to the event previously set using
        /// `create_release_event` on the `FutureGuard` which preceded this
        /// `WriteGuard`. The event can be manually 'triggered' by calling
        /// `...set_complete()...` or used normally (as a wait event) by
        /// subsequent commands. If the event is not manually completed it will be
        /// automatically set complete when this `WriteGuard` is dropped.
        pub fn release_event(guard: &WriteGuard<V>) -> Option<&Event> {
            guard.release_event.as_ref()
        }

        /// Triggers the release event and releases the lock held by this `WriteGuard`
        /// before returning the original `OrderLock`.
        pub fn release(mut guard: WriteGuard<V>) -> OrderLock<V> {
            print_debug(guard.order_lock.id(), "WriteGuard::release: Releasing write lock");
            unsafe {
                Self::release_components(&mut guard);
                extract_order_lock(guard)
            }
        }

        /// Triggers the release event by setting it complete.
        fn complete_release_event(guard: &mut WriteGuard<V>) {
            if let Some(ref e) = guard.release_event.take() {
                if !e.is_complete().expect("WriteGuard::complete_release_event") {
                    print_debug(guard.order_lock.id(), "WriteGuard::complete_release_event: \
                        Setting release event complete");
                    e.set_complete().expect("WriteGuard::complete_release_event");
                }
            }
        }

        /// Releases the lock and completes the release event.
        unsafe fn release_components(guard: &mut WriteGuard<V>) {
            guard.order_lock.lock.release_write_lock();
            Self::complete_release_event(guard);
        }
    }

    impl<V> Deref for WriteGuard<V> {
        type Target = V;

        fn deref(&self) -> &V {
            unsafe { &*self.order_lock.lock.as_ptr() }
        }
    }

    impl<V> DerefMut for WriteGuard<V> {
        fn deref_mut(&mut self) -> &mut V {
            unsafe { &mut *self.order_lock.lock.as_mut_ptr() }
        }
    }

    impl<V> Drop for WriteGuard<V> {
        fn drop(&mut self) {
            print_debug(self.order_lock.id(), "WriteGuard::drop: Dropping and releasing WriteGuard");
            unsafe { Self::release_components(self) }
        }
    }

    impl<V> OrderGuard<V> for WriteGuard<V> {
        fn new(order_lock: OrderLock<V>, release_event: Option<Event>) -> WriteGuard<V> {
            WriteGuard::new(order_lock, release_event)
        }

        fn order_lock(&self) -> &OrderLock<V> {
            &self.order_lock
        }
    }

    /// The polling stage of a `FutureGuard`.
    #[derive(Debug, PartialEq)]
    enum Stage {
        WaitEvents,
        LockQueue,
        Command,
        Upgrade,
    }

    /// A future that resolves to a read or write guard after ensuring that the
    /// data being guarded is appropriately locked during the execution of an
    /// OpenCL command.
    ///
    /// 1. Waits until both an exclusive data lock can be obtained **and** all
    ///    prerequisite OpenCL commands have completed.
    /// 2. Triggers an OpenCL command, remaining locked while the command
    ///    executes.
    /// 3. Returns a guard which provides exclusive (write) or shared (read)
    ///    access to the locked data.
    ///
    #[must_use = "futures do nothing unless polled"]
    #[derive(Debug)]
    pub struct FutureGuard<V, G> where G: OrderGuard<V> {
        order_lock: Option<OrderLock<V>>,
        lock_rx: Option<Receiver<()>>,
        wait_events: Option<EventList>,
        lock_event: Option<Event>,
        command_event: Option<Event>,
        upgrade_after_command: bool,
        upgrade_rx: Option<Receiver<()>>,
        release_event: Option<Event>,
        stage: Stage,
        _guard: PhantomData<G>,
    }

    impl<V, G> FutureGuard<V, G> where G: OrderGuard<V> {
        /// Returns a new `FutureGuard`.
        fn new(order_lock: OrderLock<V>, lock_rx: Receiver<()>) -> FutureGuard<V, G> {
            FutureGuard {
                order_lock: Some(order_lock),
                lock_rx: Some(lock_rx),
                wait_events: None,
                lock_event: None,
                command_event: None,
                upgrade_after_command: false,
                upgrade_rx: None,
                release_event: None,
                stage: Stage::WaitEvents,
                _guard: PhantomData,
            }
        }

        /// Sets an event wait list.
        ///
        /// Setting a wait list will cause this `FutureGuard` to wait until
        /// contained events have their status set to complete before obtaining a
        /// lock on the guarded internal value.
        ///
        /// [UNSTABLE]: This method may be renamed or otherwise changed at any time.
        pub fn set_lock_wait_events<L: Into<EventList>>(&mut self, wait_events: L) {
            assert!(self.wait_events.is_none(), "Wait list has already been set.");
            self.wait_events = Some(wait_events.into());
        }

        /// Sets an event wait list.
        ///
        /// See `::set_lock_wait_events`.
        ///
        /// [UNSTABLE]: This method may be renamed or otherwise changed at any time.
        pub fn ewait_lock<L: Into<EventList>>(mut self, wait_events: L) -> FutureGuard<V, G> {
            self.set_lock_wait_events(wait_events);
            self
        }

        /// Creates an event which will be triggered when a lock is obtained on
        /// the guarded internal value.
        ///
        /// The returned event can be added to the wait list of subsequent OpenCL
        /// commands with the expectation that when all preceding futures are
        /// complete, the event will automatically be 'triggered' by having its
        /// status set to complete, causing those commands to execute. This can be
        /// used to inject host side code in amongst OpenCL commands without
        /// thread blocking or extra delays of any kind.
        pub fn create_lock_event<C: ClContextPtr>(&mut self, context: C) -> OclResult<&Event> {
            assert!(self.lock_event.is_none(), "Lock event has already been created.");
            self.lock_event = Some(Event::user(context)?);
            Ok(self.lock_event.as_mut().unwrap())
        }

        /// Creates an event which will be triggered when a lock is obtained on
        /// the guarded internal value.
        ///
        /// `enew` must be an empty (null) event or event list.
        ///
        /// See `::create_lock_event`
        ///
        /// ## Panics
        ///
        /// Panics if there is an error creating the lock event.
        ///
        /// [UNSTABLE]: This method may be renamed or otherwise changed at any time.
        pub fn enew_lock<C, En>(mut self, context: C, mut enew: En) -> FutureGuard<V, G>
                where C: ClContextPtr, En: ClNullEventPtr {
            {
                let lock_event = self.create_lock_event(context).expect("FutureGuard::enew_lock");
                unsafe { enew.clone_from(lock_event); }
            }
            self
        }

        /// Sets a command completion wait event.
        ///
        /// `command_event` must be an event created by enqueuing an OpenCL
        /// command which interacts (reads/writes) with the data associated with
        /// this `FutureGuard`.
        ///
        /// If the command completion event is specified, this `FutureGuard` will
        /// "suffix" itself with an additional future that will wait until the
        /// command completes before resolving.
        ///
        /// Not specifying a command completion event will cause this
        /// `FutureGuard` to resolve into an `OrderGuard` immediately after the
        /// lock is obtained (indicated by the optionally created lock event).
        ///
        /// [UNSTABLE]: This method may be renamed or otherwise changed at any time.
        pub fn set_command_wait_event(&mut self, command_event: Event) {
            assert!(self.command_event.is_none(), "Command completion event has already been set.");
            self.command_event = Some(command_event);
        }

        /// Sets a command completion wait event.
        ///
        /// See `::set_command_wait_event`.
        ///
        /// [UNSTABLE]: This method may be renamed or otherwise changed at any time.
        pub fn ewait_command(mut self, command_event: Event) -> FutureGuard<V, G> {
            self.set_command_wait_event(command_event);
            self
        }

        /// Creates an event which will be triggered after this future resolves
        /// **and** the ensuing `OrderGuard` is dropped or manually released.
        ///
        /// The returned event can be added to the wait list of subsequent OpenCL
        /// commands with the expectation that when all preceding futures are
        /// complete, the event will automatically be 'triggered' by having its
        /// status set to complete, causing those commands to execute. This can be
        /// used to inject host side code in amongst OpenCL commands without
        /// thread blocking or extra delays of any kind.
        pub fn create_release_event<C: ClContextPtr>(&mut self, context: C) -> OclResult<&Event> {
            assert!(self.release_event.is_none(), "Release event has already been created.");
            self.release_event = Some(Event::user(context)?);
            Ok(self.release_event.as_ref().unwrap())
        }

        /// Creates an event which will be triggered after this future resolves
        /// **and** the ensuing `OrderGuard` is dropped or manually released.
        ///
        /// `enew` must be an empty (null) event or event list.
        ///
        /// See `::create_release_event`.
        ///
        /// ## Panics
        ///
        /// Panics if there is an error creating the release event.
        ///
        /// [UNSTABLE]: This method may be renamed or otherwise changed at any time.
        pub fn enew_release<C, En>(mut self, context: C, mut enew: En) -> FutureGuard<V, G>
                where C: ClContextPtr, En: ClNullEventPtr {
            {
                let release_event = self.create_release_event(context).expect("FutureGuard::enew_release");
                unsafe { enew.clone_from(release_event); }
            }
            self
        }

        /// Returns a reference to the event previously created with
        /// `::create_lock_event` or `::enew_lock` which will trigger (be
        /// completed) when the wait events are complete and the lock is locked.
        pub fn lock_event(&self) -> Option<&Event> {
            self.lock_event.as_ref()
        }

        /// Returns a reference to the event previously created with
        /// `::create_release_event` or `::enew_release` which will trigger (be
        /// completed) when a lock is obtained on the guarded internal value.
        pub fn release_event(&self) -> Option<&Event> {
            self.release_event.as_ref()
        }

        /// Blocks the current thread until the OpenCL command is complete and an
        /// appropriate lock can be obtained on the underlying data.
        pub fn wait(self) -> OclResult<G> {
            <Self as Future>::wait(self)
        }

        /// Returns a mutable pointer to the data contained within the internal
        /// value, bypassing all locks and protections.
        ///
        /// ## Panics
        ///
        /// This future must not have already resolved into a guard.
        ///
        pub fn as_ptr(&self) -> *const V {
            self.order_lock.as_ref().map(|order_lock| order_lock.lock.as_ptr())
                .expect("FutureGuard::as_ptr: No OrderLock found.")
        }

        /// Returns a mutable pointer to the data contained within the internal
        /// value, bypassing all locks and protections.
        ///
        /// ## Panics
        ///
        /// This future must not have already resolved into a guard.
        ///
        pub fn as_mut_ptr(&self) -> *mut V {
            self.order_lock.as_ref().map(|order_lock| order_lock.lock.as_mut_ptr())
                .expect("FutureGuard::as_mut_ptr: No OrderLock found.")
        }

        // /// The 'id' of the associated `OrderLock`.
        // pub fn id(&self) -> usize {
        //     self.order_lock.as_ref().expect("FutureGuard::id: No OrderLock found.").id()
        // }

        /// Returns a reference to the `OrderLock` used to create this future.
        pub fn order_lock(&self) -> &OrderLock<V> {
            self.order_lock.as_ref().expect("FutureGuard::order_lock: No OrderLock found.")
        }

        /// Polls the wait events until all requisite commands have completed then
        /// polls the lock queue.
        fn poll_wait_events(&mut self) -> OclResult<Async<G>> {
            debug_assert!(self.stage == Stage::WaitEvents);
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_wait_events: Called");

            // Check completion of wait list, if it exists:
            if let Some(ref mut wait_events) = self.wait_events {
                // if PRINT_DEBUG { println!("###### [{}] FutureGuard::poll_wait_events: \
                //     Polling wait_events (thread: {})...", self.order_lock.as_ref().unwrap().id(),
                //     ::std::thread::current().name().unwrap_or("<unnamed>")); }

                if let Async::NotReady = wait_events.poll()? {
                    return Ok(Async::NotReady);
                }

            }

            self.stage = Stage::LockQueue;
            self.poll_lock()
        }

        /// Polls the lock until we have obtained a lock then polls the command
        /// event.
        #[cfg(not(feature = "async_block"))]
        fn poll_lock(&mut self) -> OclResult<Async<G>> {
            debug_assert!(self.stage == Stage::LockQueue);
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_lock: Called");

            // Move the queue along:
            unsafe { self.order_lock.as_ref().unwrap().lock.process_queues(); }

            // Check for completion of the lock rx:
            if let Some(ref mut lock_rx) = self.lock_rx {
                match lock_rx.poll() {
                    // If the poll returns `Async::Ready`, we have been popped from
                    // the front of the lock queue and we now have exclusive access.
                    // Otherwise, return the `NotReady`. The rx (oneshot channel) will
                    // arrange for this task to be awakened when it's ready.
                    Ok(status) => {
                        if PRINT_DEBUG { println!("###### [{}] FutureGuard::poll_lock: status: {:?}, \
                            (thread: {}).", self.order_lock.as_ref().unwrap().id(), status,
                            ::std::thread::current().name().unwrap_or("<unnamed>")); }
                        match status {
                            Async::Ready(_) => {
                                if let Some(ref lock_event) = self.lock_event {
                                    lock_event.set_complete()?
                                }
                                self.stage = Stage::Command;
                            },
                            Async::NotReady => return Ok(Async::NotReady),
                        }
                    },
                    // Err(e) => return Err(e.into()),
                    Err(e) => panic!("FutureGuard::poll_lock: {:?}", e),
                }
            } else {
                unreachable!();
            }

            self.poll_command()
        }

        /// Polls the lock until we have obtained a lock then polls the command
        /// event.
        #[cfg(feature = "async_block")]
        fn poll_lock(&mut self) -> OclResult<Async<G>> {
            debug_assert!(self.stage == Stage::LockQueue);
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_lock: Called");

            // Move the queue along:
            unsafe { self.order_lock.as_ref().unwrap().lock.process_queues(); }

            // Wait until completion of the lock rx:
            self.lock_rx.take().wait()?;

            if let Some(ref lock_event) = self.lock_event {
                lock_event.set_complete()?
            }

            self.stage = Stage::Command;
            // if PRINT_DEBUG { println!("###### [{}] FutureGuard::poll_lock: Moving to command stage.",
            //     self.order_lock.as_ref().unwrap().id()); }
            return self.poll_command();
        }

        /// Polls the command event until it is complete then returns an `OrderGuard`
        /// which can be safely accessed immediately.
        fn poll_command(&mut self) -> OclResult<Async<G>> {
            debug_assert!(self.stage == Stage::Command);
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_command: Called");

            if let Some(ref mut command_event) = self.command_event {
                print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_command: Event exists");

                if let Async::NotReady = command_event.poll()? {
                    print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_command: Event not ready");
                    return Ok(Async::NotReady);
                }
                print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_command: Event is ready");
            }

            // Set cmd event to `None` so it doesn't get waited on unnecessarily
            // when this `FutureGuard` drops.
            self.command_event = None;

            if self.upgrade_after_command {
                self.stage = Stage::Upgrade;
                self.poll_upgrade()
            } else {
                Ok(Async::Ready(self.into_guard()))
            }
        }

        /// Polls the lock until it has been upgraded.
        ///
        /// Only used if `::upgrade_after_command` has been called.
        ///
        #[cfg(not(feature = "async_block"))]
        fn poll_upgrade(&mut self) -> OclResult<Async<G>> {
            debug_assert!(self.stage == Stage::Upgrade);
            debug_assert!(self.upgrade_after_command);
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_upgrade: Called");

            if self.upgrade_rx.is_none() {
                match unsafe { self.order_lock.as_ref().unwrap().lock.upgrade_read_lock() } {
                    Ok(_) => {
                        print_debug(self.order_lock.as_ref().unwrap().id(),
                            "FutureGuard::poll_upgrade: Write lock acquired. Upgrading immediately.");
                        Ok(Async::Ready(self.into_guard()))
                    },
                    Err(rx) => {
                        self.upgrade_rx = Some(rx);
                        match self.upgrade_rx.as_mut().unwrap().poll() {
                            Ok(res) => {
                                // print_debug(self.order_lock.as_ref().unwrap().id(),
                                //     "FutureGuard::poll_upgrade: Channel completed. Upgrading.");
                                // Ok(res.map(|_| self.into_guard()))
                                match res {
                                    Async::Ready(_) => {
                                        print_debug(self.order_lock.as_ref().unwrap().id(),
                                            "FutureGuard::poll_upgrade: Channel completed. Upgrading.");
                                        Ok(Async::Ready(self.into_guard()))
                                    },
                                    Async::NotReady => {
                                        print_debug(self.order_lock.as_ref().unwrap().id(),
                                            "FutureGuard::poll_upgrade: Upgrade rx not ready.");
                                        Ok(Async::NotReady)
                                    },
                                }
                            },
                            // Err(e) => Err(e.into()),
                            Err(e) => panic!("FutureGuard::poll_upgrade: {:?}", e),
                       }
                    },
                }
            } else {
                // Check for completion of the upgrade rx:
                match self.upgrade_rx.as_mut().unwrap().poll() {
                    Ok(status) => {
                        print_debug(self.order_lock.as_ref().unwrap().id(),
                            &format!("FutureGuard::poll_upgrade: Status: {:?}", status));
                        Ok(status.map(|_| self.into_guard()))
                    },
                    // Err(e) => Err(e.into()),
                    Err(e) => panic!("FutureGuard::poll_upgrade: {:?}", e),
                }
            }
        }

        /// Polls the lock until it has been upgraded.
        ///
        /// Only used if `::upgrade_after_command` has been called.
        ///
        #[cfg(feature = "async_block")]
        fn poll_upgrade(&mut self) -> OclResult<Async<G>> {
            debug_assert!(self.stage == Stage::Upgrade);
            debug_assert!(self.upgrade_after_command);
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::poll_upgrade: Called");

            match unsafe { self.order_lock.as_ref().unwrap().lock.upgrade_read_lock() } {
                Ok(_) => Ok(Async::Ready(self.into_guard())),
                Err(rx) => {
                    self.upgrade_rx = Some(rx);
                    self.upgrade_rx.take().unwrap().wait()?;
                    Ok(Async::Ready(self.into_guard()))
                }
            }
        }

        /// Resolves this `FutureGuard` into the appropriate result guard.
        fn into_guard(&mut self) -> G {
            print_debug(self.order_lock.as_ref().unwrap().id(), "FutureGuard::into_guard: All polling complete");
            G::new(self.order_lock.take().unwrap(), self.release_event.take())
        }
    }

    impl<V, G> Future for FutureGuard<V, G> where G: OrderGuard<V> {
        type Item = G;
        type Error = OclError;

        #[inline]
        fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
            if self.order_lock.is_some() {
                match self.stage {
                    Stage::WaitEvents => self.poll_wait_events(),
                    Stage::LockQueue => self.poll_lock(),
                    Stage::Command => self.poll_command(),
                    Stage::Upgrade => self.poll_upgrade(),
                }
            } else {
                Err("FutureGuard::poll: Task already completed.".into())
            }
        }
    }

    impl<V, G> Drop for FutureGuard<V, G> where G: OrderGuard<V> {
        /// Drops this FutureGuard.
        ///
        /// Blocks the current thread until the command associated with this
        /// `FutureGuard` (represented by the command completion event)
        /// completes. This ensures that the underlying value is not dropped
        /// before the command completes (which would cause obvious problems).
        ///
        /// ## `future_guard_drop_panic` Feature
        ///
        /// If the `future_guard_drop_panic` feature is enabled, dropping a
        /// `FutureGuard` before it is polled will cause a panic.
        ///
        fn drop(&mut self) {
            if cfg!(feature = "future_guard_drop_panic") {
                if let Some(ref _order_lock) = self.order_lock {
                    panic!("FutureGuard dropped before being polled. Not polling a FutureGuard \
                        can cause deadlocks. Call '.wait()' before dropping if necessary.");
                }
            }
            if let Some(ref mut lock_rx) = self.lock_rx {
                lock_rx.close();

                match lock_rx.poll() {
                    Ok(status) => {
                        match status {
                            Async::Ready(_) => {
                                if let Some(ref lock_event) = self.lock_event {
                                    lock_event.set_complete().ok();
                                }
                                // Drop and release lock.
                                let _guard = G::new(self.order_lock.take().unwrap(),
                                    self.release_event.take());
                            },
                            Async::NotReady => (),
                        }
                    },
                    Err(_) => (),
                }
            }
            if let Some(ref ccev) = self.command_event {
                // println!("###### FutureGuard::drop: Event ({:?}) incomplete...", ccev);
                // panic!("FutureGuard::drop: FutureGuard dropped before being polled.");
                ccev.wait_for().expect("Error waiting on command completion event \
                    while dropping 'FutureGuard'");
            }
            if let Some(ref rev) = self.release_event {
                rev.set_complete().expect("Error setting release event complete \
                    while dropping 'FutureGuard'");
            }
        }
    }

    // a.k.a. FutureRead<V>
    impl<V> FutureGuard<V, ReadGuard<V>> {
        pub fn upgrade_after_command(self) -> FutureGuard<V, WriteGuard<V>> {
            use std::ptr::read;

            let future_guard = unsafe {
                FutureGuard {
                    order_lock: read(&self.order_lock),
                    lock_rx: read(&self.lock_rx),
                    wait_events: read(&self.wait_events),
                    lock_event: read(&self.lock_event),
                    upgrade_after_command: true,
                    upgrade_rx: None,
                    command_event: read(&self.command_event),
                    release_event: read(&self.release_event),
                    stage: read(&self.stage),
                    _guard: PhantomData,
                }
            };

            ::std::mem::forget(self);

            future_guard
        }
    }

    /// A lock with conserved global order which interoperates with OpenCL events
    /// and Rust futures to provide exclusive access to data.
    ///
    /// Calling `::read` or `::write` returns a future which will resolve into a
    /// `OrderGuard`.
    ///
    /// ## Platform Compatibility
    ///
    /// Some CPU device/platform combinations have synchronization problems when
    /// accessing an `OrderLock` from multiple threads. Known platforms with problems
    /// are 2nd and 4th gen Intel Core processors (Sandy Bridge and Haswell) with
    /// Intel OpenCL CPU drivers. Others may be likewise affected. Run the
    /// `device_check.rs` example to determine if your device/platform is
    /// affected. AMD platform drivers are known to work properly on the
    /// aforementioned CPUs so use those instead if possible.
    #[derive(Debug)]
    pub struct OrderLock<V> {
        lock: QrwLock<V>,
    }

    impl<V> OrderLock<V> {
        /// Creates and returns a new `OrderLock`.
        #[inline]
        pub fn new(data: V) -> OrderLock<V> {
            OrderLock {
                lock: QrwLock::new(data)
            }
        }

        /// Returns a new `FutureGuard` which will resolve into a a `OrderGuard`.
        pub fn read(self) -> FutureGuard<V, ReadGuard<V>> {
            print_debug(self.id(), "OrderLock::read: Read lock requested");
            let (tx, rx) = oneshot::channel();
            unsafe { self.lock.enqueue_lock_request(QrwRequest::new(tx, RequestKind::Read)); }
            FutureGuard::new(self.into(), rx)
        }

        /// Returns a new `FutureGuard` which will resolve into a a `OrderGuard`.
        pub fn write(self) -> FutureGuard<V, WriteGuard<V>> {
            print_debug(self.id(), "OrderLock::write: Write lock requested");
            let (tx, rx) = oneshot::channel();
            unsafe { self.lock.enqueue_lock_request(QrwRequest::new(tx, RequestKind::Write)); }
            FutureGuard::new(self.into(), rx)
        }

        /// Returns a reference to the inner value.
        ///
        #[inline]
        pub fn as_ptr(&self) -> *const V {
            self.lock.as_ptr()
        }

        /// Returns a mutable reference to the inner value.
        ///
        #[inline]
        pub fn as_mut_ptr(&self) -> *mut V {
            self.lock.as_mut_ptr()
        }

        /// Returns a pointer address to the internal array, usable as a unique
        /// identifier.
        fn id(&self) -> usize {
            self.lock.as_ptr() as usize
        }
    }

    impl<V> From<QrwLock<V>> for OrderLock<V> {
        fn from(q: QrwLock<V>) -> OrderLock<V> {
            OrderLock { lock: q }
        }
    }

    impl<V> From<V> for OrderLock<V> {
        fn from(vec: V) -> OrderLock<V> {
            OrderLock { lock: QrwLock::new(vec) }
        }
    }

    impl<V> Clone for OrderLock<V> {
        #[inline]
        fn clone(&self) -> OrderLock<V> {
            OrderLock {
                lock: self.lock.clone(),
            }
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)