| 1 |
// Copyright 2016 Amanieu d'Antras |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
| 4 |
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 |
// http://opensource.org/licenses/MIT>, at your option. This file may not be |
| 6 |
// copied, modified, or distributed except according to those terms. |
| 7 |
|
| 8 |
use core::cmp; |
| 9 |
use core::mem; |
| 10 |
use core::num::Wrapping; |
| 11 |
use core::ops; |
| 12 |
use core::sync::atomic::Ordering; |
| 13 |
use fallback; |
| 14 |
|
| 15 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 16 |
use core::sync::atomic::{ AtomicI8, AtomicU8 }; |
| 17 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 18 |
use core::sync::atomic::{ AtomicI16, AtomicU16 }; |
| 19 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 20 |
use core::sync::atomic::{ AtomicI32, AtomicU32 }; |
| 21 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 22 |
use core::sync::atomic::{ AtomicI64, AtomicU64 }; |
| 23 |
|
| 24 |
#[cfg(not(feature = "nightly"))] |
| 25 |
use core::sync::atomic::AtomicUsize; |
| 26 |
#[cfg(not(feature = "nightly"))] |
| 27 |
const SIZEOF_USIZE: usize = mem::size_of::<usize>(); |
| 28 |
#[cfg(not(feature = "nightly"))] |
| 29 |
const ALIGNOF_USIZE: usize = mem::align_of::<usize>(); |
| 30 |
|
| 31 |
#[cfg(feature = "nightly")] |
| 32 |
#[inline] |
| 33 |
pub const fn atomic_is_lock_free<T>() -> bool { |
| 34 |
let size = mem::size_of::<T>(); |
| 35 |
// FIXME: switch to … && … && … once that operator is supported in const functions |
| 36 |
(1 == size.count_ones()) & (8 >= size) & (mem::align_of::<T>() >= size) |
| 37 |
} |
| 38 |
|
| 39 |
#[cfg(not(feature = "nightly"))] |
| 40 |
#[inline] |
| 41 |
pub fn atomic_is_lock_free<T>() -> bool { |
| 42 |
let size = mem::size_of::<T>(); |
| 43 |
1 == size.count_ones() && SIZEOF_USIZE >= size && mem::align_of::<T>() >= ALIGNOF_USIZE |
| 44 |
} |
| 45 |
|
| 46 |
#[inline] |
| 47 |
pub unsafe fn atomic_load<T>(dst: *mut T, order: Ordering) -> T { |
| 48 |
match mem::size_of::<T>() { |
| 49 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 50 |
1 if mem::align_of::<T>() >= 1 => |
| 51 |
{ |
| 52 |
mem::transmute_copy(&(*(dst as *const AtomicU8)).load(order)) |
| 53 |
} |
| 54 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 55 |
2 if mem::align_of::<T>() >= 2 => |
| 56 |
{ |
| 57 |
mem::transmute_copy(&(*(dst as *const AtomicU16)).load(order)) |
| 58 |
} |
| 59 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 60 |
4 if mem::align_of::<T>() >= 4 => |
| 61 |
{ |
| 62 |
mem::transmute_copy(&(*(dst as *const AtomicU32)).load(order)) |
| 63 |
} |
| 64 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 65 |
8 if mem::align_of::<T>() >= 8 => |
| 66 |
{ |
| 67 |
mem::transmute_copy(&(*(dst as *const AtomicU64)).load(order)) |
| 68 |
} |
| 69 |
#[cfg(not(feature = "nightly"))] |
| 70 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 71 |
{ |
| 72 |
mem::transmute_copy(&(*(dst as *const AtomicUsize)).load(order)) |
| 73 |
} |
| 74 |
_ => fallback::atomic_load(dst), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[inline] |
| 79 |
pub unsafe fn atomic_store<T>(dst: *mut T, val: T, order: Ordering) { |
| 80 |
match mem::size_of::<T>() { |
| 81 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 82 |
1 if mem::align_of::<T>() >= 1 => |
| 83 |
{ |
| 84 |
(*(dst as *const AtomicU8)).store(mem::transmute_copy(&val), order) |
| 85 |
} |
| 86 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 87 |
2 if mem::align_of::<T>() >= 2 => |
| 88 |
{ |
| 89 |
(*(dst as *const AtomicU16)).store(mem::transmute_copy(&val), order) |
| 90 |
} |
| 91 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 92 |
4 if mem::align_of::<T>() >= 4 => |
| 93 |
{ |
| 94 |
(*(dst as *const AtomicU32)).store(mem::transmute_copy(&val), order) |
| 95 |
} |
| 96 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 97 |
8 if mem::align_of::<T>() >= 8 => |
| 98 |
{ |
| 99 |
(*(dst as *const AtomicU64)).store(mem::transmute_copy(&val), order) |
| 100 |
} |
| 101 |
#[cfg(not(feature = "nightly"))] |
| 102 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 103 |
{ |
| 104 |
(*(dst as *const AtomicUsize)).store(mem::transmute_copy(&val), order) |
| 105 |
} |
| 106 |
_ => fallback::atomic_store(dst, val), |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[inline] |
| 111 |
pub unsafe fn atomic_swap<T>(dst: *mut T, val: T, order: Ordering) -> T { |
| 112 |
match mem::size_of::<T>() { |
| 113 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 114 |
1 if mem::align_of::<T>() >= 1 => |
| 115 |
{ |
| 116 |
mem::transmute_copy(&(*(dst as *const AtomicU8)).swap(mem::transmute_copy(&val), order)) |
| 117 |
} |
| 118 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 119 |
2 if mem::align_of::<T>() >= 2 => |
| 120 |
{ |
| 121 |
mem::transmute_copy( |
| 122 |
&(*(dst as *const AtomicU16)).swap(mem::transmute_copy(&val), order), |
| 123 |
) |
| 124 |
} |
| 125 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 126 |
4 if mem::align_of::<T>() >= 4 => |
| 127 |
{ |
| 128 |
mem::transmute_copy( |
| 129 |
&(*(dst as *const AtomicU32)).swap(mem::transmute_copy(&val), order), |
| 130 |
) |
| 131 |
} |
| 132 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 133 |
8 if mem::align_of::<T>() >= 8 => |
| 134 |
{ |
| 135 |
mem::transmute_copy( |
| 136 |
&(*(dst as *const AtomicU64)).swap(mem::transmute_copy(&val), order), |
| 137 |
) |
| 138 |
} |
| 139 |
#[cfg(not(feature = "nightly"))] |
| 140 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 141 |
{ |
| 142 |
mem::transmute_copy( |
| 143 |
&(*(dst as *const AtomicUsize)).swap(mem::transmute_copy(&val), order), |
| 144 |
) |
| 145 |
} |
| 146 |
_ => fallback::atomic_swap(dst, val), |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
#[inline] |
| 151 |
unsafe fn map_result<T, U>(r: Result<T, T>) -> Result<U, U> { |
| 152 |
match r { |
| 153 |
Ok(x) => Ok(mem::transmute_copy(&x)), |
| 154 |
Err(x) => Err(mem::transmute_copy(&x)), |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[inline] |
| 159 |
pub unsafe fn atomic_compare_exchange<T>( |
| 160 |
dst: *mut T, |
| 161 |
current: T, |
| 162 |
new: T, |
| 163 |
success: Ordering, |
| 164 |
failure: Ordering, |
| 165 |
) -> Result<T, T> { |
| 166 |
match mem::size_of::<T>() { |
| 167 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 168 |
1 if mem::align_of::<T>() >= 1 => |
| 169 |
{ |
| 170 |
map_result((*(dst as *const AtomicU8)).compare_exchange( |
| 171 |
mem::transmute_copy(¤t), |
| 172 |
mem::transmute_copy(&new), |
| 173 |
success, |
| 174 |
failure, |
| 175 |
)) |
| 176 |
} |
| 177 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 178 |
2 if mem::align_of::<T>() >= 2 => |
| 179 |
{ |
| 180 |
map_result((*(dst as *const AtomicU16)).compare_exchange( |
| 181 |
mem::transmute_copy(¤t), |
| 182 |
mem::transmute_copy(&new), |
| 183 |
success, |
| 184 |
failure, |
| 185 |
)) |
| 186 |
} |
| 187 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 188 |
4 if mem::align_of::<T>() >= 4 => |
| 189 |
{ |
| 190 |
map_result((*(dst as *const AtomicU32)).compare_exchange( |
| 191 |
mem::transmute_copy(¤t), |
| 192 |
mem::transmute_copy(&new), |
| 193 |
success, |
| 194 |
failure, |
| 195 |
)) |
| 196 |
} |
| 197 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 198 |
8 if mem::align_of::<T>() >= 8 => |
| 199 |
{ |
| 200 |
map_result((*(dst as *const AtomicU64)).compare_exchange( |
| 201 |
mem::transmute_copy(¤t), |
| 202 |
mem::transmute_copy(&new), |
| 203 |
success, |
| 204 |
failure, |
| 205 |
)) |
| 206 |
} |
| 207 |
#[cfg(not(feature = "nightly"))] |
| 208 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 209 |
{ |
| 210 |
map_result((*(dst as *const AtomicUsize)).compare_exchange( |
| 211 |
mem::transmute_copy(¤t), |
| 212 |
mem::transmute_copy(&new), |
| 213 |
success, |
| 214 |
failure, |
| 215 |
)) |
| 216 |
} |
| 217 |
_ => fallback::atomic_compare_exchange(dst, current, new), |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
#[inline] |
| 222 |
pub unsafe fn atomic_compare_exchange_weak<T>( |
| 223 |
dst: *mut T, |
| 224 |
current: T, |
| 225 |
new: T, |
| 226 |
success: Ordering, |
| 227 |
failure: Ordering, |
| 228 |
) -> Result<T, T> { |
| 229 |
match mem::size_of::<T>() { |
| 230 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 231 |
1 if mem::align_of::<T>() >= 1 => |
| 232 |
{ |
| 233 |
map_result((*(dst as *const AtomicU8)).compare_exchange_weak( |
| 234 |
mem::transmute_copy(¤t), |
| 235 |
mem::transmute_copy(&new), |
| 236 |
success, |
| 237 |
failure, |
| 238 |
)) |
| 239 |
} |
| 240 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 241 |
2 if mem::align_of::<T>() >= 2 => |
| 242 |
{ |
| 243 |
map_result((*(dst as *const AtomicU16)).compare_exchange_weak( |
| 244 |
mem::transmute_copy(¤t), |
| 245 |
mem::transmute_copy(&new), |
| 246 |
success, |
| 247 |
failure, |
| 248 |
)) |
| 249 |
} |
| 250 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 251 |
4 if mem::align_of::<T>() >= 4 => |
| 252 |
{ |
| 253 |
map_result((*(dst as *const AtomicU32)).compare_exchange_weak( |
| 254 |
mem::transmute_copy(¤t), |
| 255 |
mem::transmute_copy(&new), |
| 256 |
success, |
| 257 |
failure, |
| 258 |
)) |
| 259 |
} |
| 260 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 261 |
8 if mem::align_of::<T>() >= 8 => |
| 262 |
{ |
| 263 |
map_result((*(dst as *const AtomicU64)).compare_exchange_weak( |
| 264 |
mem::transmute_copy(¤t), |
| 265 |
mem::transmute_copy(&new), |
| 266 |
success, |
| 267 |
failure, |
| 268 |
)) |
| 269 |
} |
| 270 |
#[cfg(not(feature = "nightly"))] |
| 271 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 272 |
{ |
| 273 |
map_result((*(dst as *const AtomicUsize)).compare_exchange_weak( |
| 274 |
mem::transmute_copy(¤t), |
| 275 |
mem::transmute_copy(&new), |
| 276 |
success, |
| 277 |
failure, |
| 278 |
)) |
| 279 |
} |
| 280 |
_ => fallback::atomic_compare_exchange(dst, current, new), |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[inline] |
| 285 |
pub unsafe fn atomic_add<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T |
| 286 |
where |
| 287 |
Wrapping<T>: ops::Add<Output = Wrapping<T>>, |
| 288 |
{ |
| 289 |
match mem::size_of::<T>() { |
| 290 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 291 |
1 if mem::align_of::<T>() >= 1 => |
| 292 |
{ |
| 293 |
mem::transmute_copy( |
| 294 |
&(*(dst as *const AtomicU8)).fetch_add(mem::transmute_copy(&val), order), |
| 295 |
) |
| 296 |
} |
| 297 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 298 |
2 if mem::align_of::<T>() >= 2 => |
| 299 |
{ |
| 300 |
mem::transmute_copy( |
| 301 |
&(*(dst as *const AtomicU16)).fetch_add(mem::transmute_copy(&val), order), |
| 302 |
) |
| 303 |
} |
| 304 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 305 |
4 if mem::align_of::<T>() >= 4 => |
| 306 |
{ |
| 307 |
mem::transmute_copy( |
| 308 |
&(*(dst as *const AtomicU32)).fetch_add(mem::transmute_copy(&val), order), |
| 309 |
) |
| 310 |
} |
| 311 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 312 |
8 if mem::align_of::<T>() >= 8 => |
| 313 |
{ |
| 314 |
mem::transmute_copy( |
| 315 |
&(*(dst as *const AtomicU64)).fetch_add(mem::transmute_copy(&val), order), |
| 316 |
) |
| 317 |
} |
| 318 |
#[cfg(not(feature = "nightly"))] |
| 319 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 320 |
{ |
| 321 |
mem::transmute_copy( |
| 322 |
&(*(dst as *const AtomicUsize)).fetch_add(mem::transmute_copy(&val), order), |
| 323 |
) |
| 324 |
} |
| 325 |
_ => fallback::atomic_add(dst, val), |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
#[inline] |
| 330 |
pub unsafe fn atomic_sub<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T |
| 331 |
where |
| 332 |
Wrapping<T>: ops::Sub<Output = Wrapping<T>>, |
| 333 |
{ |
| 334 |
match mem::size_of::<T>() { |
| 335 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 336 |
1 if mem::align_of::<T>() >= 1 => |
| 337 |
{ |
| 338 |
mem::transmute_copy( |
| 339 |
&(*(dst as *const AtomicU8)).fetch_sub(mem::transmute_copy(&val), order), |
| 340 |
) |
| 341 |
} |
| 342 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 343 |
2 if mem::align_of::<T>() >= 2 => |
| 344 |
{ |
| 345 |
mem::transmute_copy( |
| 346 |
&(*(dst as *const AtomicU16)).fetch_sub(mem::transmute_copy(&val), order), |
| 347 |
) |
| 348 |
} |
| 349 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 350 |
4 if mem::align_of::<T>() >= 4 => |
| 351 |
{ |
| 352 |
mem::transmute_copy( |
| 353 |
&(*(dst as *const AtomicU32)).fetch_sub(mem::transmute_copy(&val), order), |
| 354 |
) |
| 355 |
} |
| 356 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 357 |
8 if mem::align_of::<T>() >= 8 => |
| 358 |
{ |
| 359 |
mem::transmute_copy( |
| 360 |
&(*(dst as *const AtomicU64)).fetch_sub(mem::transmute_copy(&val), order), |
| 361 |
) |
| 362 |
} |
| 363 |
#[cfg(not(feature = "nightly"))] |
| 364 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 365 |
{ |
| 366 |
mem::transmute_copy( |
| 367 |
&(*(dst as *const AtomicUsize)).fetch_sub(mem::transmute_copy(&val), order), |
| 368 |
) |
| 369 |
} |
| 370 |
_ => fallback::atomic_sub(dst, val), |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
#[inline] |
| 375 |
pub unsafe fn atomic_and<T: Copy + ops::BitAnd<Output = T>>( |
| 376 |
dst: *mut T, |
| 377 |
val: T, |
| 378 |
order: Ordering, |
| 379 |
) -> T { |
| 380 |
match mem::size_of::<T>() { |
| 381 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 382 |
1 if mem::align_of::<T>() >= 1 => |
| 383 |
{ |
| 384 |
mem::transmute_copy( |
| 385 |
&(*(dst as *const AtomicU8)).fetch_and(mem::transmute_copy(&val), order), |
| 386 |
) |
| 387 |
} |
| 388 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 389 |
2 if mem::align_of::<T>() >= 2 => |
| 390 |
{ |
| 391 |
mem::transmute_copy( |
| 392 |
&(*(dst as *const AtomicU16)).fetch_and(mem::transmute_copy(&val), order), |
| 393 |
) |
| 394 |
} |
| 395 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 396 |
4 if mem::align_of::<T>() >= 4 => |
| 397 |
{ |
| 398 |
mem::transmute_copy( |
| 399 |
&(*(dst as *const AtomicU32)).fetch_and(mem::transmute_copy(&val), order), |
| 400 |
) |
| 401 |
} |
| 402 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 403 |
8 if mem::align_of::<T>() >= 8 => |
| 404 |
{ |
| 405 |
mem::transmute_copy( |
| 406 |
&(*(dst as *const AtomicU64)).fetch_and(mem::transmute_copy(&val), order), |
| 407 |
) |
| 408 |
} |
| 409 |
#[cfg(not(feature = "nightly"))] |
| 410 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 411 |
{ |
| 412 |
mem::transmute_copy( |
| 413 |
&(*(dst as *const AtomicUsize)).fetch_and(mem::transmute_copy(&val), order), |
| 414 |
) |
| 415 |
} |
| 416 |
_ => fallback::atomic_and(dst, val), |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
#[inline] |
| 421 |
pub unsafe fn atomic_or<T: Copy + ops::BitOr<Output = T>>( |
| 422 |
dst: *mut T, |
| 423 |
val: T, |
| 424 |
order: Ordering, |
| 425 |
) -> T { |
| 426 |
match mem::size_of::<T>() { |
| 427 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 428 |
1 if mem::align_of::<T>() >= 1 => |
| 429 |
{ |
| 430 |
mem::transmute_copy( |
| 431 |
&(*(dst as *const AtomicU8)).fetch_or(mem::transmute_copy(&val), order), |
| 432 |
) |
| 433 |
} |
| 434 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 435 |
2 if mem::align_of::<T>() >= 2 => |
| 436 |
{ |
| 437 |
mem::transmute_copy( |
| 438 |
&(*(dst as *const AtomicU16)).fetch_or(mem::transmute_copy(&val), order), |
| 439 |
) |
| 440 |
} |
| 441 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 442 |
4 if mem::align_of::<T>() >= 4 => |
| 443 |
{ |
| 444 |
mem::transmute_copy( |
| 445 |
&(*(dst as *const AtomicU32)).fetch_or(mem::transmute_copy(&val), order), |
| 446 |
) |
| 447 |
} |
| 448 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 449 |
8 if mem::align_of::<T>() >= 8 => |
| 450 |
{ |
| 451 |
mem::transmute_copy( |
| 452 |
&(*(dst as *const AtomicU64)).fetch_or(mem::transmute_copy(&val), order), |
| 453 |
) |
| 454 |
} |
| 455 |
#[cfg(not(feature = "nightly"))] |
| 456 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 457 |
{ |
| 458 |
mem::transmute_copy( |
| 459 |
&(*(dst as *const AtomicUsize)).fetch_or(mem::transmute_copy(&val), order), |
| 460 |
) |
| 461 |
} |
| 462 |
_ => fallback::atomic_or(dst, val), |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
#[inline] |
| 467 |
pub unsafe fn atomic_xor<T: Copy + ops::BitXor<Output = T>>( |
| 468 |
dst: *mut T, |
| 469 |
val: T, |
| 470 |
order: Ordering, |
| 471 |
) -> T { |
| 472 |
match mem::size_of::<T>() { |
| 473 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 474 |
1 if mem::align_of::<T>() >= 1 => |
| 475 |
{ |
| 476 |
mem::transmute_copy( |
| 477 |
&(*(dst as *const AtomicU8)).fetch_xor(mem::transmute_copy(&val), order), |
| 478 |
) |
| 479 |
} |
| 480 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 481 |
2 if mem::align_of::<T>() >= 2 => |
| 482 |
{ |
| 483 |
mem::transmute_copy( |
| 484 |
&(*(dst as *const AtomicU16)).fetch_xor(mem::transmute_copy(&val), order), |
| 485 |
) |
| 486 |
} |
| 487 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 488 |
4 if mem::align_of::<T>() >= 4 => |
| 489 |
{ |
| 490 |
mem::transmute_copy( |
| 491 |
&(*(dst as *const AtomicU32)).fetch_xor(mem::transmute_copy(&val), order), |
| 492 |
) |
| 493 |
} |
| 494 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 495 |
8 if mem::align_of::<T>() >= 8 => |
| 496 |
{ |
| 497 |
mem::transmute_copy( |
| 498 |
&(*(dst as *const AtomicU64)).fetch_xor(mem::transmute_copy(&val), order), |
| 499 |
) |
| 500 |
} |
| 501 |
#[cfg(not(feature = "nightly"))] |
| 502 |
SIZEOF_USIZE if mem::align_of::<T>() >= ALIGNOF_USIZE => |
| 503 |
{ |
| 504 |
mem::transmute_copy( |
| 505 |
&(*(dst as *const AtomicUsize)).fetch_xor(mem::transmute_copy(&val), order), |
| 506 |
) |
| 507 |
} |
| 508 |
_ => fallback::atomic_xor(dst, val), |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
#[inline] |
| 513 |
pub unsafe fn atomic_min<T: Copy + cmp::Ord>(dst: *mut T, val: T, order: Ordering) -> T { |
| 514 |
// Silence warning, fetch_min is not stable yet |
| 515 |
#[cfg(not(feature = "nightly"))] |
| 516 |
let _ = order; |
| 517 |
|
| 518 |
match mem::size_of::<T>() { |
| 519 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 520 |
1 if mem::align_of::<T>() >= 1 => |
| 521 |
{ |
| 522 |
mem::transmute_copy( |
| 523 |
&(*(dst as *const AtomicI8)).fetch_min(mem::transmute_copy(&val), order), |
| 524 |
) |
| 525 |
} |
| 526 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 527 |
2 if mem::align_of::<T>() >= 2 => |
| 528 |
{ |
| 529 |
mem::transmute_copy( |
| 530 |
&(*(dst as *const AtomicI16)).fetch_min(mem::transmute_copy(&val), order), |
| 531 |
) |
| 532 |
} |
| 533 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 534 |
4 if mem::align_of::<T>() >= 4 => |
| 535 |
{ |
| 536 |
mem::transmute_copy( |
| 537 |
&(*(dst as *const AtomicI32)).fetch_min(mem::transmute_copy(&val), order), |
| 538 |
) |
| 539 |
} |
| 540 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 541 |
8 if mem::align_of::<T>() >= 8 => |
| 542 |
{ |
| 543 |
mem::transmute_copy( |
| 544 |
&(*(dst as *const AtomicI64)).fetch_min(mem::transmute_copy(&val), order), |
| 545 |
) |
| 546 |
} |
| 547 |
_ => fallback::atomic_min(dst, val), |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
#[inline] |
| 552 |
pub unsafe fn atomic_max<T: Copy + cmp::Ord>(dst: *mut T, val: T, order: Ordering) -> T { |
| 553 |
// Silence warning, fetch_min is not stable yet |
| 554 |
#[cfg(not(feature = "nightly"))] |
| 555 |
let _ = order; |
| 556 |
|
| 557 |
match mem::size_of::<T>() { |
| 558 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 559 |
1 if mem::align_of::<T>() >= 1 => |
| 560 |
{ |
| 561 |
mem::transmute_copy( |
| 562 |
&(*(dst as *const AtomicI8)).fetch_max(mem::transmute_copy(&val), order), |
| 563 |
) |
| 564 |
} |
| 565 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 566 |
2 if mem::align_of::<T>() >= 2 => |
| 567 |
{ |
| 568 |
mem::transmute_copy( |
| 569 |
&(*(dst as *const AtomicI16)).fetch_max(mem::transmute_copy(&val), order), |
| 570 |
) |
| 571 |
} |
| 572 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 573 |
4 if mem::align_of::<T>() >= 4 => |
| 574 |
{ |
| 575 |
mem::transmute_copy( |
| 576 |
&(*(dst as *const AtomicI32)).fetch_max(mem::transmute_copy(&val), order), |
| 577 |
) |
| 578 |
} |
| 579 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 580 |
8 if mem::align_of::<T>() >= 8 => |
| 581 |
{ |
| 582 |
mem::transmute_copy( |
| 583 |
&(*(dst as *const AtomicI64)).fetch_max(mem::transmute_copy(&val), order), |
| 584 |
) |
| 585 |
} |
| 586 |
_ => fallback::atomic_max(dst, val), |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
#[inline] |
| 591 |
pub unsafe fn atomic_umin<T: Copy + cmp::Ord>(dst: *mut T, val: T, order: Ordering) -> T { |
| 592 |
// Silence warning, fetch_min is not stable yet |
| 593 |
#[cfg(not(feature = "nightly"))] |
| 594 |
let _ = order; |
| 595 |
|
| 596 |
match mem::size_of::<T>() { |
| 597 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 598 |
1 if mem::align_of::<T>() >= 1 => |
| 599 |
{ |
| 600 |
mem::transmute_copy( |
| 601 |
&(*(dst as *const AtomicU8)).fetch_min(mem::transmute_copy(&val), order), |
| 602 |
) |
| 603 |
} |
| 604 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 605 |
2 if mem::align_of::<T>() >= 2 => |
| 606 |
{ |
| 607 |
mem::transmute_copy( |
| 608 |
&(*(dst as *const AtomicU16)).fetch_min(mem::transmute_copy(&val), order), |
| 609 |
) |
| 610 |
} |
| 611 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 612 |
4 if mem::align_of::<T>() >= 4 => |
| 613 |
{ |
| 614 |
mem::transmute_copy( |
| 615 |
&(*(dst as *const AtomicU32)).fetch_min(mem::transmute_copy(&val), order), |
| 616 |
) |
| 617 |
} |
| 618 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 619 |
8 if mem::align_of::<T>() >= 8 => |
| 620 |
{ |
| 621 |
mem::transmute_copy( |
| 622 |
&(*(dst as *const AtomicU64)).fetch_min(mem::transmute_copy(&val), order), |
| 623 |
) |
| 624 |
} |
| 625 |
_ => fallback::atomic_min(dst, val), |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
#[inline] |
| 630 |
pub unsafe fn atomic_umax<T: Copy + cmp::Ord>(dst: *mut T, val: T, order: Ordering) -> T { |
| 631 |
// Silence warning, fetch_min is not stable yet |
| 632 |
#[cfg(not(feature = "nightly"))] |
| 633 |
let _ = order; |
| 634 |
|
| 635 |
match mem::size_of::<T>() { |
| 636 |
#[cfg(all(feature = "nightly", target_has_atomic = "8"))] |
| 637 |
1 if mem::align_of::<T>() >= 1 => |
| 638 |
{ |
| 639 |
mem::transmute_copy( |
| 640 |
&(*(dst as *const AtomicU8)).fetch_max(mem::transmute_copy(&val), order), |
| 641 |
) |
| 642 |
} |
| 643 |
#[cfg(all(feature = "nightly", target_has_atomic = "16"))] |
| 644 |
2 if mem::align_of::<T>() >= 2 => |
| 645 |
{ |
| 646 |
mem::transmute_copy( |
| 647 |
&(*(dst as *const AtomicU16)).fetch_max(mem::transmute_copy(&val), order), |
| 648 |
) |
| 649 |
} |
| 650 |
#[cfg(all(feature = "nightly", target_has_atomic = "32"))] |
| 651 |
4 if mem::align_of::<T>() >= 4 => |
| 652 |
{ |
| 653 |
mem::transmute_copy( |
| 654 |
&(*(dst as *const AtomicU32)).fetch_max(mem::transmute_copy(&val), order), |
| 655 |
) |
| 656 |
} |
| 657 |
#[cfg(all(feature = "nightly", target_has_atomic = "64"))] |
| 658 |
8 if mem::align_of::<T>() >= 8 => |
| 659 |
{ |
| 660 |
mem::transmute_copy( |
| 661 |
&(*(dst as *const AtomicU64)).fetch_max(mem::transmute_copy(&val), order), |
| 662 |
) |
| 663 |
} |
| 664 |
_ => fallback::atomic_max(dst, val), |
| 665 |
} |
| 666 |
} |