pub fn register(&self, waker: &Waker) {
        match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) {
            WAITING => {
                unsafe {
                    // Locked acquired, update the waker cell
                    *self.waker.get() = Some(waker.clone());

                    // Release the lock. If the state transitioned to include
                    // the `WAKING` bit, this means that a wake has been
                    // called concurrently, so we have to remove the waker and
                    // wake it.`
                    //
                    // Start by assuming that the state is `REGISTERING` as this
                    // is what we jut set it to.
                    let res = self
                        .state
                        .compare_exchange(REGISTERING, WAITING, AcqRel, Acquire);

                    match res {
                        Ok(_) => {}
                        Err(actual) => {
                            // This branch can only be reached if a
                            // concurrent thread called `wake`. In this
                            // case, `actual` **must** be `REGISTERING |
                            // `WAKING`.
                            debug_assert_eq!(actual, REGISTERING | WAKING);

                            // Take the waker to wake once the atomic operation has
                            // completed.
                            let waker = (*self.waker.get()).take().unwrap();

                            // Just swap, because no one could change state while state == `REGISTERING` | `WAKING`.
                            self.state.swap(WAITING, AcqRel);

                            // The atomic swap was complete, now
                            // wake the task and return.
                            waker.wake();
                        }
                    }
                }
            }
            WAKING => {
                // Currently in the process of waking the task, i.e.,
                // `wake` is currently being called on the old task handle.
                // So, we call wake on the new waker
                waker.wake_by_ref();
            }
            state => {
                // In this case, a concurrent thread is holding the
                // "registering" lock. This probably indicates a bug in the
                // caller's code as racing to call `register` doesn't make much
                // sense.
                //
                // We just want to maintain memory safety. It is ok to drop the
                // call to `register`.
                debug_assert!(state == REGISTERING || state == REGISTERING | WAKING);
            }
        }
    }
/// Registers the waker to be notified on calls to `wake`.
///
/// The new task will take place of any previous tasks that were registered
/// by previous calls to `register`. Any calls to `wake` that happen after
/// a call to `register` (as defined by the memory ordering rules), will
/// notify the `register` caller's task and deregister the waker from future
/// notifications. Because of this, callers should ensure `register` gets
/// invoked with a new `Waker` **each** time they require a wakeup.
///
/// It is safe to call `register` with multiple other threads concurrently
/// calling `wake`. This will result in the `register` caller's current
/// task being notified once.
///
/// This function is safe to call concurrently, but this is generally a bad
/// idea. Concurrent calls to `register` will attempt to register different
/// tasks to be notified. One of the callers will win and have its task set,
/// but there is no guarantee as to which caller will succeed.
///
/// # Examples
///
/// Here is how `register` is used when implementing a flag.
///
/// ```
/// use std::future::Future;
/// use std::task::{Context, Poll};
/// use std::sync::atomic::AtomicBool;
/// use std::sync::atomic::Ordering::SeqCst;
/// use std::pin::Pin;
///
/// use futures::task::AtomicWaker;
///
/// struct Flag {
///     waker: AtomicWaker,
///     set: AtomicBool,
/// }
///
/// impl Future for Flag {
///     type Output = ();
///
///     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
///         // Register **before** checking `set` to avoid a race condition
///         // that would result in lost notifications.
///         self.waker.register(cx.waker());
///
///         if self.set.load(SeqCst) {
///             Poll::Ready(())
///         } else {
///             Poll::Pending
///         }
///     }
/// }
/// ```
Registers the waker to be notified on calls to `wake`.
The new task will take place of any previous tasks that were registered
by previous calls to `register`.

It is safe to call `register` with multiple other threads concurrently
calling `wake`. This will result in the `register` caller's current
task being notified once.

This function is safe to call concurrently, but this is generally a bad
idea. Concurrent calls to `register` will attempt to register different
tasks to be notified. One of the callers will win and have its task set,
but there is no guarantee as to which caller will succeed.
Registers the waker to be notified on calls to ` wake ` . The new task will take place of any previous tasks that were registered by previous calls to ` register ` . It is safe to call ` register ` with multiple other threads concurrently calling ` wake ` . This will result in the ` register ` caller ' s current task being notified once . This function is safe to call concurrently but this is generally a bad idea . Concurrent calls to ` register ` will attempt to register different tasks to be notified . One of the callers will win and have its task set but there is no guarantee as to which caller will succeed .
pub fn register(&self, waker: &Waker) {
        match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) {
            WAITING => {
                unsafe {
                    // Locked acquired, update the waker cell
                    *self.waker.get() = Some(waker.clone());
                    // Release the lock. If the state transitioned to include
                    // the `WAKING` bit, this means that a wake has been
                    // called concurrently, so we have to remove the waker and
                    // wake it.`
                    //
                    // Start by assuming that the state is `REGISTERING` as this
                    // is what we jut set it to.
                    let res = self
                        .state
                        .compare_exchange(REGISTERING, WAITING, AcqRel, Acquire);
                    match res {
                        Ok(_) => {}
                        Err(actual) => {
                            // This branch can only be reached if a
                            // concurrent thread called `wake`. In this
                            // case, `actual` **must** be `REGISTERING |
                            // `WAKING`.
                            debug_assert_eq!(actual, REGISTERING | WAKING);
                            // Take the waker to wake once the atomic operation has
                            // completed.
                            let waker = (*self.waker.get()).take().unwrap();
                            // Just swap, because no one could change state while state == `REGISTERING` | `WAKING`.
                            self.state.swap(WAITING, AcqRel);
                            // The atomic swap was complete, now
                            // wake the task and return.
                            waker.wake();
                        }
                    }
                }
            }
            WAKING => {
                // Currently in the process of waking the task, i.e.,
                // `wake` is currently being called on the old task handle.
                // So, we call wake on the new waker
                waker.wake_by_ref();
            }
            state => {
                // In this case, a concurrent thread is holding the
                // "registering" lock. This probably indicates a bug in the
                // caller's code as racing to call `register` doesn't make much
                // sense.
                //
                // We just want to maintain memory safety. It is ok to drop the
                // call to `register`.
                debug_assert!(state == REGISTERING || state == REGISTERING | WAKING);
            }
        }
    }
pub fn register ( & self , waker : & Waker ) { match self . state . compare_and_swap ( WAITING , REGISTERING , Acquire ) { WAITING => { unsafe { // Locked acquired, update the waker cell * self . waker . get ( ) = Some ( waker . clone ( ) ) ; // Release the lock. If the state transitioned to include // the `WAKING` bit, this means that a wake has been // called concurrently, so we have to remove the waker and // wake it.` // // Start by assuming that the state is `REGISTERING` as this // is what we jut set it to. let res = self . state . compare_exchange ( REGISTERING , WAITING , AcqRel , Acquire ) ; match res { Ok ( _ ) => { } Err ( actual ) => { // This branch can only be reached if a // concurrent thread called `wake`. In this // case, `actual` **must** be `REGISTERING | // `WAKING`. debug_assert_eq ! ( actual REGISTERING WAKING ) ; // Take the waker to wake once the atomic operation has // completed. let waker = ( * self . waker . get ( ) ) . take ( ) . unwrap ( ) ; // Just swap, because no one could change state while state == `REGISTERING` | `WAKING`. self . state . swap ( WAITING , AcqRel ) ; // The atomic swap was complete, now // wake the task and return. waker . wake ( ) ; } } } } WAKING => { // Currently in the process of waking the task, i.e., // `wake` is currently being called on the old task handle. // So, we call wake on the new waker waker . wake_by_ref ( ) ; } state => { // In this case, a concurrent thread is holding the // "registering" lock. This probably indicates a bug in the // caller's code as racing to call `register` doesn't make much // sense. // // We just want to maintain memory safety. It is ok to drop the // call to `register`. debug_assert ! ( state REGISTERING state REGISTERING WAKING ) ; } } }
Registers the waker to be notified on calls to `wake`.
Registers the waker to be notified on calls to ` wake ` .
&self
waker
&Waker
&self
waker
&Waker
examples
Here is how `register` is used when implementing a flag.

```
use std::future::Future;
use std::task::{Context, Poll};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::pin::Pin.

use futures::task::AtomicWaker.

struct Flag {
waker: AtomicWaker,
set: AtomicBool,
}

impl Future for Flag {
type Output = ().

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
Register **before** checking `set` to avoid a race condition
that would result in lost notifications.
self.waker.register(cx.waker()).

if self.set.load(SeqCst) {
Poll::Ready(())
} else {
Poll::Pending
}
}
}
```
Here is how ` register ` is used when implementing a flag . ` ` ` use std :: future :: Future ; use std :: task :: { Context Poll } ; use std :: sync :: atomic :: AtomicBool ; use std :: sync :: atomic :: Ordering :: SeqCst ; use std :: pin :: Pin . use futures :: task :: AtomicWaker . struct Flag { waker : AtomicWaker set : AtomicBool } impl Future for Flag { type Output = () . fn poll ( self : Pin<&mut Self > cx : &mut Context< ' _ > ) - > Poll< () > { Register ** before ** checking ` set ` to avoid a race condition that would result in lost notifications . self . waker . register ( cx . waker () ) . if self . set . load ( SeqCst ) { Poll :: Ready (( )) } else { Poll :: Pending } } } ` ` `