Upgrade rust/crates/parking_lot_core to 0.8.5 am: e339207ab6

Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/parking_lot_core/+/1833675

Change-Id: I8efc57586d75f25d0131414989720bc8cb2dd36e
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index b278af6..7e564f3 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
 {
   "git": {
-    "sha1": "f3ea9e2082735525e84b30acd39c3f1d9d845c9a"
+    "sha1": "aecb031e795391025d33f3f26f83ab24102ca42f"
   }
 }
diff --git a/Android.bp b/Android.bp
index dee3192..dcf43e5 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,4 +1,5 @@
-// This file is generated by cargo2android.py --run --device --dependencies --tests.
+// This file is generated by cargo2android.py --run --device --tests.
+// Do not modify this file as changes will be overridden on upgrade.
 
 package {
     default_applicable_licenses: [
@@ -40,8 +41,11 @@
 
 rust_library {
     name: "libparking_lot_core",
+    // has rustc warnings
     host_supported: true,
     crate_name: "parking_lot_core",
+    cargo_env_compat: true,
+    cargo_pkg_version: "0.8.5",
     srcs: ["src/lib.rs"],
     edition: "2018",
     rustlibs: [
@@ -53,9 +57,12 @@
 }
 
 rust_defaults {
-    name: "parking_lot_core_defaults",
+    name: "parking_lot_core_test_defaults",
     crate_name: "parking_lot_core",
+    // has rustc warnings
     srcs: ["src/lib.rs"],
+    cargo_env_compat: true,
+    cargo_pkg_version: "0.8.5",
     test_suites: ["general-tests"],
     auto_gen_config: true,
     edition: "2018",
@@ -69,7 +76,7 @@
 
 rust_test_host {
     name: "parking_lot_core_host_test_src_lib",
-    defaults: ["parking_lot_core_defaults"],
+    defaults: ["parking_lot_core_test_defaults"],
     test_options: {
         unit_test: true,
     },
@@ -77,11 +84,5 @@
 
 rust_test {
     name: "parking_lot_core_device_test_src_lib",
-    defaults: ["parking_lot_core_defaults"],
+    defaults: ["parking_lot_core_test_defaults"],
 }
-
-// dependent_library ["feature_list"]
-//   cfg-if-1.0.0
-//   instant-0.1.9
-//   libc-0.2.86 "default,std"
-//   smallvec-1.6.1
diff --git a/Cargo.toml b/Cargo.toml
index a9184c2..7afeb41 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,17 +3,16 @@
 # When uploading crates to the registry Cargo will automatically
 # "normalize" Cargo.toml files for maximal compatibility
 # with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g., crates.io) dependencies
+# to registry (e.g., crates.io) dependencies.
 #
-# If you believe there's an error in this file please file an
-# issue against the rust-lang/cargo repository. If you're
-# editing this file be aware that the upstream Cargo.toml
-# will likely look very different (and much more reasonable)
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
 
 [package]
 edition = "2018"
 name = "parking_lot_core"
-version = "0.8.3"
+version = "0.8.5"
 authors = ["Amanieu d'Antras <amanieu@gmail.com>"]
 description = "An advanced API for creating custom synchronization primitives."
 keywords = ["mutex", "condvar", "rwlock", "once", "thread"]
@@ -21,14 +20,14 @@
 license = "Apache-2.0/MIT"
 repository = "https://github.com/Amanieu/parking_lot"
 [dependencies.backtrace]
-version = "0.3.49"
+version = "0.3.60"
 optional = true
 
 [dependencies.cfg-if]
 version = "1.0.0"
 
 [dependencies.instant]
-version = "0.1.4"
+version = "0.1.9"
 
 [dependencies.petgraph]
 version = "0.5.1"
@@ -38,16 +37,16 @@
 version = "1.6.1"
 
 [dependencies.thread-id]
-version = "3.3.0"
+version = "4.0.0"
 optional = true
 
 [features]
 deadlock_detection = ["petgraph", "thread-id", "backtrace"]
 nightly = []
 [target."cfg(target_os = \"redox\")".dependencies.redox_syscall]
-version = "0.2.4"
+version = "0.2.8"
 [target."cfg(unix)".dependencies.libc]
-version = "0.2.71"
+version = "0.2.95"
 [target."cfg(windows)".dependencies.winapi]
-version = "0.3.8"
+version = "0.3.9"
 features = ["winnt", "ntstatus", "minwindef", "winerror", "winbase", "errhandlingapi", "handleapi"]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index e4af2fc..2585329 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
 [package]
 name = "parking_lot_core"
-version = "0.8.3"
+version = "0.8.5"
 authors = ["Amanieu d'Antras <amanieu@gmail.com>"]
 description = "An advanced API for creating custom synchronization primitives."
 license = "Apache-2.0/MIT"
@@ -13,18 +13,18 @@
 cfg-if = "1.0.0"
 smallvec = "1.6.1"
 petgraph = { version = "0.5.1", optional = true }
-thread-id = { version = "3.3.0", optional = true }
-backtrace = { version = "0.3.49", optional = true }
-instant = "0.1.4"
+thread-id = { version = "4.0.0", optional = true }
+backtrace = { version = "0.3.60", optional = true }
+instant = "0.1.9"
 
 [target.'cfg(unix)'.dependencies]
-libc = "0.2.71"
+libc = "0.2.95"
 
 [target.'cfg(target_os = "redox")'.dependencies]
-redox_syscall = "0.2.4"
+redox_syscall = "0.2.8"
 
 [target.'cfg(windows)'.dependencies]
-winapi = { version = "0.3.8", features = ["winnt", "ntstatus", "minwindef",
+winapi = { version = "0.3.9", features = ["winnt", "ntstatus", "minwindef",
     "winerror", "winbase", "errhandlingapi", "handleapi"] }
 
 [features]
diff --git a/METADATA b/METADATA
index a3eea08..15649a7 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@
   }
   url {
     type: ARCHIVE
-    value: "https://static.crates.io/crates/parking_lot_core/parking_lot_core-0.8.3.crate"
+    value: "https://static.crates.io/crates/parking_lot_core/parking_lot_core-0.8.5.crate"
   }
-  version: "0.8.3"
+  version: "0.8.5"
   license_type: NOTICE
   last_upgrade_date {
     year: 2021
-    month: 2
-    day: 12
+    month: 9
+    day: 22
   }
 }
diff --git a/build.rs b/build.rs
new file mode 100644
index 0000000..d29c769
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,10 @@
+// Automatically detect tsan in a way that's compatible with both stable (which
+// doesn't support sanitizers) and nightly (which does). Works because build
+// scripts gets `cfg` info, even if the cfg is unstable.
+fn main() {
+    println!("cargo:rerun-if-changed=build.rs");
+    let santizer_list = std::env::var("CARGO_CFG_SANITIZE").unwrap_or_default();
+    if santizer_list.contains("thread") {
+        println!("cargo:rustc-cfg=tsan_enabled");
+    }
+}
diff --git a/src/word_lock.rs b/src/word_lock.rs
index 450e985..1109401 100644
--- a/src/word_lock.rs
+++ b/src/word_lock.rs
@@ -154,7 +154,7 @@
                 if let Err(x) = self.state.compare_exchange_weak(
                     state,
                     state.with_queue_head(thread_data),
-                    Ordering::Release,
+                    Ordering::AcqRel,
                     Ordering::Relaxed,
                 ) {
                     return x;
@@ -238,7 +238,7 @@
                 }
 
                 // Need an acquire fence before reading the new queue
-                fence(Ordering::Acquire);
+                fence_acquire(&self.state);
                 continue;
             }
 
@@ -263,7 +263,7 @@
                         continue;
                     } else {
                         // Need an acquire fence before reading the new queue
-                        fence(Ordering::Acquire);
+                        fence_acquire(&self.state);
                         continue 'outer;
                     }
                 }
@@ -286,6 +286,17 @@
     }
 }
 
+// Thread-Sanitizer only has partial fence support, so when running under it, we
+// try and avoid false positives by using a discarded acquire load instead.
+#[inline]
+fn fence_acquire(a: &AtomicUsize) {
+    if cfg!(tsan_enabled) {
+        let _ = a.load(Ordering::Acquire);
+    } else {
+        fence(Ordering::Acquire);
+    }
+}
+
 trait LockState {
     fn is_locked(self) -> bool;
     fn is_queue_locked(self) -> bool;