     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

    #![cfg(target_thread_local)]
    #![unstable(feature = "thread_local_internals", issue = "0")]

    // Since what appears to be glibc 2.18 this symbol has been shipped which
    // GCC and clang both use to invoke destructors in thread_local globals, so
    // let's do the same!
    //
    // Note, however, that we run on lots older linuxes, as well as cross
    // compiling from a newer linux to an older linux, so we also have a
    // fallback implementation to use as well.
    //
    // Due to rust-lang/rust#18804, make sure this is not generic!
    #[cfg(any(target_os = "linux", target_os = "fuchsia", target_os = "hermit"))]
    pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
        use libc;
        use mem;
        use sys_common::thread_local::register_dtor_fallback;

        extern {
            #[linkage = "extern_weak"]
            static __dso_handle: *mut u8;
            #[linkage = "extern_weak"]
            static __cxa_thread_atexit_impl: *const libc::c_void;
        }
        if !__cxa_thread_atexit_impl.is_null() {
            type F = unsafe extern fn(dtor: unsafe extern fn(*mut u8),
                                      arg: *mut u8,
                                      dso_handle: *mut u8) -> libc::c_int;
            mem::transmute::<*const libc::c_void, F>(__cxa_thread_atexit_impl)
                (dtor, t, &__dso_handle as *const _ as *mut _);
            return
        }
        register_dtor_fallback(t, dtor);
    }

    // This implementation is very similar to register_dtor_fallback in
    // sys_common/thread_local.rs. The main difference is that we want to hook into
    // macOS's analog of the above linux function, _tlv_atexit. OSX will run the
    // registered dtors before any TLS slots get freed, and when the main thread
    // exits.
    //
    // Unfortunately, calling _tlv_atexit while tls dtors are running is UB. The
    // workaround below is to register, via _tlv_atexit, a custom DTOR list once per
    // thread. thread_local dtors are pushed to the DTOR list without calling
    // _tlv_atexit.
    #[cfg(target_os = "macos")]
    pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
        use cell::Cell;
        use ptr;

        #[thread_local]
        static REGISTERED: Cell<bool> = Cell::new(false);
        if !REGISTERED.get() {
            _tlv_atexit(run_dtors, ptr::null_mut());
            REGISTERED.set(true);
        }

        type List = Vec<(*mut u8, unsafe extern fn(*mut u8))>;

        #[thread_local]
        static DTORS: Cell<*mut List> = Cell::new(ptr::null_mut());
        if DTORS.get().is_null() {
            let v: Box<List> = box Vec::new();
            DTORS.set(Box::into_raw(v));
        }

        extern {
            fn _tlv_atexit(dtor: unsafe extern fn(*mut u8),
                           arg: *mut u8);
        }

        let list: &mut List = &mut *DTORS.get();
        list.push((t, dtor));

        unsafe extern fn run_dtors(_: *mut u8) {
            let mut ptr = DTORS.replace(ptr::null_mut());
            while !ptr.is_null() {
                let list = Box::from_raw(ptr);
                for (ptr, dtor) in list.into_iter() {
                    dtor(ptr);
                }
                ptr = DTORS.replace(ptr::null_mut());
            }
        }
    }

    pub fn requires_move_before_drop() -> bool {
        false
    }

# 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`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)