-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
1005 lines (938 loc) · 46.8 KB
/
lib.rs
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Byte-wise atomic memcpy.
//!
//! This is an attempt to implement equivalent of C++ ["P1478R1: Byte-wise atomic memcpy"][p1478r1] in Rust.
//!
//! This is expected to allow algorithms such as Seqlock and Chase-Lev deque to be implemented without UB of data races.
//! See [P1478R1][p1478r1] for more.
//!
//! [p1478r1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1478r1.html
#![no_std]
#![doc(test(
no_crate_inject,
attr(
deny(warnings, rust_2018_idioms, single_use_lifetimes),
allow(dead_code, unused_variables)
)
))]
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
single_use_lifetimes,
unreachable_pub
)]
#![cfg_attr(test, warn(unsafe_op_in_unsafe_fn))] // unsafe_op_in_unsafe_fn requires Rust 1.52
#![cfg_attr(not(test), allow(unused_unsafe))]
#![warn(
clippy::exhaustive_enums,
clippy::exhaustive_structs,
clippy::missing_inline_in_public_items,
clippy::pedantic,
clippy::undocumented_unsafe_blocks
)]
#![allow(clippy::too_many_lines)]
#![cfg_attr(feature = "inline-always", allow(clippy::inline_always))]
// This crate should work on targets with power-of-two pointer widths,
// but it is not clear how it will work on targets without them.
// There are currently no 8-bit, 128-bit, or higher builtin targets.
#[cfg(not(any(
target_pointer_width = "16",
target_pointer_width = "32",
target_pointer_width = "64",
)))]
compile_error!(
"atomic-memcpy currently only supports targets with {16,32,64}-bit pointer width; \
if you need support for 8-bit or others, \
please submit an issue at <https://github.com/taiki-e/atomic-memcpy>"
);
use core::sync::atomic::{self, Ordering};
/// Byte-wise atomic load.
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// - `src` must be valid for reads.
/// - `src` must be properly aligned.
/// - `src` must go through [`UnsafeCell::get`](core::cell::UnsafeCell::get).
/// - There are no concurrent non-atomic write operations.
/// - There are no concurrent atomic write operations of different
/// granularity. The granularity of atomic operations is an implementation
/// detail, so the concurrent write operation that can always
/// safely be used is only [`atomic_store`].
///
/// Like [`ptr::read`](core::ptr::read), `atomic_load` creates a bitwise copy of `T`, regardless of
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
/// value and the value at `*src` can [violate memory safety][read-ownership].
///
/// Note that even if `T` has size `0`, the pointer must be non-null.
///
/// ## Returned value
///
/// This function returns [`MaybeUninit<T>`](core::mem::MaybeUninit) instead of `T`.
///
/// - All bits in the returned value are guaranteed to be copied from `src`.
/// - There is *no* guarantee that all bits in the return have been copied at
/// the same time, so if `src` is updated by a concurrent write operation,
/// it is up to the caller to make sure that the returned value is valid as `T`.
///
/// [read-ownership]: core::ptr::read#ownership-of-the-returned-value
/// [valid]: core::ptr#safety
///
/// # Panics
///
/// Panics if `order` is [`Release`](Ordering::Release) or [`AcqRel`](Ordering::AcqRel).
///
/// # Examples
///
/// ```rust
/// use std::{cell::UnsafeCell, sync::atomic::Ordering};
///
/// let v = UnsafeCell::new([0_u8; 64]);
/// let result = unsafe { atomic_memcpy::atomic_load(v.get(), Ordering::Acquire) };
/// // SAFETY: there was no concurrent write operations during load.
/// assert_eq!(unsafe { result.assume_init() }, [0; 64]);
/// ```
#[cfg_attr(all(feature = "no-panic", not(debug_assertions)), no_panic::no_panic)]
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub unsafe fn atomic_load<T>(src: *const T, order: Ordering) -> core::mem::MaybeUninit<T> {
match order {
Ordering::Release => panic!("there is no such thing as a release load"),
Ordering::AcqRel => panic!("there is no such thing as an acquire/release load"),
_ => {}
}
// clippy bug that does not recognize safety comments inside macros.
#[allow(clippy::undocumented_unsafe_blocks)]
// SAFETY: the caller must uphold the safety contract for `atomic_load`.
let val = unsafe { imp::atomic_load(src) };
match order {
Ordering::Relaxed => { /* no-op */ }
_ => atomic::fence(order),
}
val
}
/// Byte-wise atomic store.
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// - `dst` must be [valid] for writes.
/// - `dst` must be properly aligned.
/// - `dst` must go through [`UnsafeCell::get`](core::cell::UnsafeCell::get).
/// - There are no concurrent non-atomic operations.
/// - There are no concurrent atomic operations of different
/// granularity. The granularity of atomic operations is an implementation
/// detail, so the concurrent operation that can always
/// safely be used is only [`atomic_load`].
///
/// If there are concurrent write operations, the resulting value at `*dst` may
/// contain a mixture of bytes written by this thread and bytes written by
/// another thread. If `T` is not valid for all bit patterns, using the value at
/// `*dst` can violate memory safety.
///
/// Note that even if `T` has size `0`, the pointer must be non-null.
///
/// [valid]: core::ptr#safety
///
/// # Panics
///
/// Panics if `order` is [`Acquire`](Ordering::Acquire) or [`AcqRel`](Ordering::AcqRel).
///
/// # Examples
///
/// ```rust
/// use std::{cell::UnsafeCell, sync::atomic::Ordering};
///
/// let v = UnsafeCell::new([0_u8; 64]);
/// unsafe {
/// atomic_memcpy::atomic_store(v.get(), [1; 64], Ordering::Release);
/// }
/// let result = unsafe { atomic_memcpy::atomic_load(v.get(), Ordering::Acquire) };
/// // SAFETY: there was no concurrent write operations during load.
/// assert_eq!(unsafe { result.assume_init() }, [1; 64]);
/// ```
#[cfg_attr(all(feature = "no-panic", not(debug_assertions)), no_panic::no_panic)]
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub unsafe fn atomic_store<T>(dst: *mut T, val: T, order: Ordering) {
match order {
Ordering::Acquire => panic!("there is no such thing as an acquire store"),
Ordering::AcqRel => panic!("there is no such thing as an acquire/release store"),
Ordering::Relaxed => { /* no-op */ }
_ => atomic::fence(order),
}
// clippy bug that does not recognize safety comments inside macros.
#[allow(clippy::undocumented_unsafe_blocks)]
// SAFETY: the caller must uphold the safety contract for `atomic_store`.
unsafe {
imp::atomic_store(dst, val);
}
}
/// Since `#[cfg(target_has_atomic_load_store = "ptr")]` is not available on
/// stable, the following heuristic is used.
///
/// - 16-bit targets (e.g., avr, msp430) don't support atomic load/store.
/// msp430 can actually support atomic load/store, but the LLVM backend does not support it yet.
/// - <https://github.com/rust-lang/rust/blob/788b1fe5b79a8b74215022f9df49b0eae68a50b9/compiler/rustc_target/src/spec/msp430_none_elf.rs#L22-L30>
/// - <https://github.com/rust-lang/rust/issues/45085#issuecomment-385090816>
/// - <https://github.com/rust-lang/rust/pull/55450>
/// - riscv32 targets without the A extension (e.g., riscv32i, riscv32imc) don't support atomic load/store.
/// However, if OS is available, atomic operations are supported: <https://github.com/rust-lang/rust/blob/788b1fe5b79a8b74215022f9df49b0eae68a50b9/compiler/rustc_target/src/spec/riscv32imc_esp_espidf.rs#L20-L26>
///
/// This heuristic is based on a list of builtin targets that currently do no support
/// atomic load/store, so it should be quite accurate, at least for builtin targets.
/// The addition of new builtin targets that do not support atomic load/store is
/// being tracked by CI. See `tools/no_atomic.sh` for more.
///
/// In addition to the above cfg, there is `cfg(atomic_memcpy_unsafe_volatile)`
/// to force the use of volatile read/write instead of atomic load/store.
/// Note that the use of `--cfg atomic_memcpy_unsafe_volatile` is
/// undefined behavior in the multi-threaded environment, since volatile
/// read/write does not guarantee anything about data race.
#[cfg(not(any(
target_pointer_width = "16",
all(target_arch = "riscv32", not(target_feature = "a"), target_os = "none"),
atomic_memcpy_unsafe_volatile,
)))]
mod imp {
#[cfg(not(target_pointer_width = "16"))]
use core::sync::atomic::AtomicU32;
#[cfg(not(any(target_pointer_width = "16", target_pointer_width = "32")))]
use core::sync::atomic::AtomicU64;
use core::{
mem::{self, ManuallyDrop, MaybeUninit},
ops::Range,
sync::atomic::{AtomicU16, AtomicUsize, Ordering},
};
#[cfg(target_pointer_width = "32")]
type Half = u16;
#[cfg(target_pointer_width = "32")]
type AtomicHalf = AtomicU16;
#[cfg(target_pointer_width = "64")]
type Half = u32;
#[cfg(target_pointer_width = "64")]
type AtomicHalf = AtomicU32;
mod load {
use core::{
mem,
sync::atomic::{AtomicU8, AtomicUsize, Ordering},
};
// Invariant: `src` and `result` will never change.
// Invariant: Only the `advance` method can advance offset and counter.
pub(super) struct LoadState {
src: *const u8,
// Note: This is a pointer from MaybeUninit.
result: *mut u8,
/// Counter to track remaining bytes in `T`.
remaining: usize,
offset: usize,
}
impl LoadState {
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) fn new<T>(result: *mut T, src: *const T) -> Self {
Self {
src: src as *const u8,
result: result as *mut u8,
remaining: mem::size_of::<T>(),
offset: 0,
}
}
/// Advances pointers by `size` **bytes**.
///
/// # Safety
///
/// - The remaining bytes must be greater than or equal to `size`.
/// - The range of `self.dst..self.dst.add(size)` must be filled.
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
unsafe fn advance(&mut self, size: usize) {
debug_assert!(self.remaining >= size);
self.remaining -= size;
self.offset += size;
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) fn remaining(&self) -> usize {
self.remaining
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
unsafe fn src<T>(&self) -> &T {
// SAFETY: the caller must uphold the safety contract.
unsafe { &*(self.src.add(self.offset) as *const T) }
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
unsafe fn result<T>(&self) -> *mut T {
// SAFETY: the caller must uphold the safety contract.
unsafe { self.result.add(self.offset) as *mut T }
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) fn atomic_load_u8(&mut self, count: usize) {
// This condition is also checked by the caller, so the compiler
// will remove this assertion by optimization.
assert!(self.remaining() >= count);
for _ in 0..count {
// SAFETY:
// - we've checked that the remaining bytes is greater than or equal to `count`
// Therefore, due to `LoadState`'s invariant:
// - `src` is valid to atomic read of `count` of u8.
// - `result` is valid to write of `count` of u8.
unsafe {
let val = self.src::<AtomicU8>().load(Ordering::Relaxed);
self.result::<u8>().write(val);
// SAFETY: we've filled 1 byte.
self.advance(1);
}
}
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) unsafe fn atomic_load_usize(&mut self) {
while self.remaining() >= mem::size_of::<usize>() {
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `usize`.
// - we've checked that the remaining bytes is greater than
// or equal to `size_of::<usize>()`.
// Therefore, due to `LoadState`'s invariant:
// - `src` is valid to atomic read of `usize`.
// - `result` is valid to *unaligned* write of `usize`.
unsafe {
let val = self.src::<AtomicUsize>().load(Ordering::Relaxed);
self.result::<usize>().write_unaligned(val);
// SAFETY: we've filled `size_of::<usize>()` bytes.
self.advance(mem::size_of::<usize>());
}
}
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
#[cfg(not(target_pointer_width = "16"))]
pub(super) unsafe fn atomic_load_half(&mut self) {
use super::{AtomicHalf, Half};
debug_assert!(self.remaining() >= mem::size_of::<Half>());
// SAFETY:
// - the caller must guarantee that both `src` and `dst` are properly aligned for `Half`.
// - the caller must guarantee that the remaining bytes is greater than
// or equal to `size_of::<Half>()`.
// Therefore, due to `LoadState`'s invariant:
// - `src` is valid to atomic read of `Half`.
// - `result` is valid to write of `Half`.
unsafe {
let val = self.src::<AtomicHalf>().load(Ordering::Relaxed);
self.result::<Half>().write(val);
// SAFETY: we've filled `size_of::<Half>()` bytes.
self.advance(mem::size_of::<Half>());
}
}
}
}
/// Byte-wise atomic load.
///
/// # Safety
///
/// See the documentation of [crate root's `atomic_load`](crate::atomic_load) for safety requirements.
/**
# Implementation
It is implemented based on the assumption that atomic operations at a granularity greater than bytes is not a problem, as stated by [p1478r1].
> Note that on standard hardware, it should be OK to actually perform the copy at larger than byte granularity. Copying multiple bytes as part of one operation is indistinguishable from running them so quickly that the intermediate state is not observed. In fact, we expect that existing assembly memcpy implementations will suffice when suffixed with the required fence.
And it turns out that the granularity of the atomic operations is very important for performance.
- Loading/storing all bytes in bytes is very slow at least on x86/x86_64.
- The pointer width atomic operation is the fastest at least on x86/x86_64.
- Atomic operations with a granularity larger than the pointer width are slow at least on x86/x86_64 (cmpxchg8b/cmpxchg16b).
Note that the following additional safety requirements.
- The granularity of the atomic operations in load and store must be the same.
- When performing an atomic operation as a type with alignment greater than 1, the pointer must be properly aligned.
The caller of `atomic_load` guarantees that the `src` is properly aligned.
So, we can avoid calling align_offset or read at a granularity greater than u8 in some cases.
The following is what this implementation is currently `atomic_load` using (Note: `atomic_store` also uses exactly the same way to determine the granularity of atomic operations):
Branch | Granularity of atomic operations | Conditions
------ | -------------------------------- | ----------
1 | u8 ..., usize ..., u8 ... | `size_of::<T>() >= size_of::<usize>() * 4`, `align_of::<T>() < align_of::<usize>()`
2 | usize ... | `align_of::<T>() >= align_of::<usize>()`
3 | u32 ... | `align_of::<T>() >= align_of::<u32>()`, 64-bit or higher
4 | u16 ... | `align_of::<T>() >= align_of::<u16>()`, 32-bit or higher
5 | u8 ... |
- Branch 1: If the alignment of `T` is less than usize, but `T` can be read as at least a few numbers of usize, compute the align offset and read it like `(&[AtomicU8], &[AtomicUsize], &[AtomicU8])`.
- Branch 2: If the alignment of `T` is greater than or equal to usize, we can read it as a chunk of usize from the first byte.
- Branch 3, 4: If the alignment of `T` is greater than 1, we can read it as a chunk of smaller integers (u32 or u16). This is basically the same strategy as Branch 2.
- Branch 5: Otherwise, we read it per byte.
Note that only Branch 1 requires to compute align offset dynamically.
Note that which branch is chosen is evaluated at compile time.
- The fastest is Branch 2, which can read all bytes as a chunk of usize.
- If the size of `T` is not too small, Branch 1 is the next fastest to Branch 2.
- If the size of `T` is small, Branch 3/4/5 can be faster than Branch 1.
Whether to choose Branch 1 or Branch 3/4/5 when `T` is small is currently based on a rough heuristic based on simple benchmarks on x86_64.
[p1478r1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1478r1.html
*/
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(crate) unsafe fn atomic_load<T>(src: *const T) -> MaybeUninit<T> {
// Safety requirements guaranteed by the caller:
// - `src` is valid for atomic reads.
// - `src` is properly aligned for `T`.
// - `src` go through `UnsafeCell::get`.
// - there are no concurrent non-atomic write operations.
// - there are no concurrent atomic write operations of different granularity.
// Note that the safety of the code in this function relies on these guarantees,
// whether or not they are explicitly mentioned in the each safety comment.
debug_assert!(!src.is_null());
debug_assert!(src as usize % mem::align_of::<T>() == 0);
static_assert_atomic_alignment();
let mut result = MaybeUninit::<T>::uninit();
if mem::size_of::<T>() == 0 {
return result;
}
// HACK: Miri cannot track uninitialized bytes on a per byte basis for
// partially initialized scalars: https://github.com/rust-lang/rust/issues/69488
//
// This hack allows miri to properly track the use of uninitialized
// bytes. See also tests/uninit.rs that is a test to check if
// valgrind/sanitizer/miri can properly detect the use of uninitialized
// bytes.
//
// Note: With or without this hack, atomic load/store of integers
// containing uninitialized bytes is technically an undefined behavior.
// The only purpose of this hack is to make sure that Miri errors for
// atomic load/store of integers containing uninitialized bytes
// (which is probably not a problem and uncharted territory at best [1] [2] [3],
// and can be detected by `-Zmiri-check-number-validity` [4]),
// do not mask Miri errors for the use of uninitialized bytes (which is definitely a problem).
// See also tests/padding.rs.
//
// [1] https://github.com/crossbeam-rs/crossbeam/issues/315
// [2] https://github.com/rust-lang/unsafe-code-guidelines/issues/158
// [3] https://github.com/rust-lang/unsafe-code-guidelines/issues/71
// [4] https://github.com/rust-lang/miri/pull/1904
//
// rust-lang/rust#69488 affects only CTFE(compile-time function evaluation)/Miri
// and atomic operations cannot be called in const context, so our code
// is only affected in the case of cfg(miri).
if cfg!(miri) {
let mut state = load::LoadState::new(result.as_mut_ptr(), src);
state.atomic_load_u8(state.remaining());
debug_assert_eq!(state.remaining(), 0);
return result;
}
// Branch 1: If the alignment of `T` is less than usize, but `T` can be read as
// at least one or more usize, compute the align offset and read it
// like `(&[AtomicU8], &[AtomicUsize], &[AtomicU8])`.
if mem::align_of::<T>() < mem::align_of::<AtomicUsize>()
&& mem::size_of::<T>() >= mem::size_of::<usize>() * 4
{
let mut state = load::LoadState::new(result.as_mut_ptr(), src);
#[cfg(not(target_pointer_width = "16"))]
{
// Since the caller guarantees that the pointer is properly aligned,
// if `T` has an alignment of half of usize, there are only two
// patterns: read as usize from the first byte, or read as usize
// after reading `usize / 2` bytes.
//
// on 64-bit:
// | 8 | 8 | 8 | 4 |
// | 4 | 8 | 8 | 8 |
// or
// | 8 | 8 | 8 |
// | 4 | 8 | 8 | 4 |
//
// Handling this case manually can reduce the number of instructions
// significantly compared to using align_offset.
if mem::align_of::<T>() >= mem::align_of::<Half>() {
if src as usize % mem::size_of::<usize>() == 0 {
// SAFETY:
// - we've checked that `src` is properly aligned for `usize`.
// - the remaining bytes is greater than or equal to `size_of::<usize>()`.
unsafe { state.atomic_load_usize() }
} else {
debug_assert_eq!(
src as usize % mem::size_of::<usize>(),
mem::size_of::<Half>()
);
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to `Half`.
// - the remaining bytes is greater than or equal to `size_of::<usize>()`.
unsafe {
state.atomic_load_half();
// SAFETY: we've advanced `size_of::<Half>()` bytes,
// so now `state.src` is definitely aligned.
state.atomic_load_usize();
}
}
// Load remaining bytes.
if state.remaining() != 0 {
debug_assert_eq!(state.remaining(), mem::size_of::<Half>());
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to `Half`.
// - the remaining bytes is equal to `size_of::<Half>()`.
unsafe { state.atomic_load_half() }
}
return result;
}
}
let offset = (src as *const u8).align_offset(mem::align_of::<AtomicUsize>());
// Note: align_offset may returns usize::MAX: https://github.com/rust-lang/rust/issues/62420
if state.remaining() >= offset {
// Load `offset` bytes per byte to align `state.src`.
state.atomic_load_u8(offset);
debug_assert!(state.remaining() >= mem::size_of::<usize>());
// SAFETY:
// - align_offset succeeds and the `offset` bytes have been
// filled, so now `state.src` is definitely aligned.
// - we've checked that the remaining bytes is greater than
// or equal to `size_of::<usize>()`.
//
// In this branch, the pointer to `state.result` is usually
// not properly aligned, so we use `atomic_load_usize_result_unaligned`,
// which has no requirement for alignment of `state.result`.
unsafe { state.atomic_load_usize() }
// Load remaining bytes per byte.
state.atomic_load_u8(state.remaining());
debug_assert_eq!(state.remaining(), 0);
return result;
}
}
// Branch 2: If the alignment of `T` is greater than or equal to usize,
// we can read it as a chunk of usize from the first byte.
if mem::align_of::<T>() >= mem::align_of::<AtomicUsize>() {
let src = src as *const AtomicUsize;
let dst = result.as_mut_ptr() as *mut usize;
for i in range(0..mem::size_of::<T>() / mem::size_of::<usize>()) {
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to usize.
// - the remaining bytes is greater than or equal to `size_of::<usize>()`.
unsafe {
let val: usize = (*src.add(i)).load(Ordering::Relaxed);
dst.add(i).write(val);
}
}
return result;
}
#[cfg(not(target_pointer_width = "16"))]
{
// Branch 3: If the alignment of `T` is greater than or equal to u32,
// we can read it as a chunk of u32 from the first byte.
if mem::size_of::<usize>() > 4 && mem::align_of::<T>() >= mem::align_of::<AtomicU32>() {
let src = src as *const AtomicU32;
let dst = result.as_mut_ptr() as *mut u32;
for i in range(0..mem::size_of::<T>() / mem::size_of::<u32>()) {
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to u32.
// - the remaining bytes is greater than or equal to `size_of::<u32>()`.
unsafe {
let val: u32 = (*src.add(i)).load(Ordering::Relaxed);
dst.add(i).write(val);
}
}
return result;
}
}
// Branch 4: If the alignment of `T` is greater than or equal to u16,
// we can read it as a chunk of u16 from the first byte.
if mem::size_of::<usize>() > 2 && mem::align_of::<T>() >= mem::align_of::<AtomicU16>() {
let src = src as *const AtomicU16;
let dst = result.as_mut_ptr() as *mut u16;
for i in range(0..mem::size_of::<T>() / mem::size_of::<u16>()) {
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to u16.
// - the remaining bytes is greater than or equal to `size_of::<u16>()`.
unsafe {
let val: u16 = (*src.add(i)).load(Ordering::Relaxed);
dst.add(i).write(val);
}
}
return result;
}
// Branch 5: Otherwise, we read it per byte.
let mut state = load::LoadState::new(result.as_mut_ptr(), src);
state.atomic_load_u8(state.remaining());
debug_assert_eq!(state.remaining(), 0);
result
}
mod store {
use core::{
mem,
sync::atomic::{AtomicU8, AtomicUsize, Ordering},
};
// Invariant: `src` and `dst` will never change.
// Invariant: Only the `advance` method can advance offset and counter.
pub(super) struct StoreState {
src: *const u8,
dst: *const u8,
/// Number of remaining bytes in `T`.
remaining: usize,
offset: usize,
}
impl StoreState {
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) fn new<T>(dst: *mut T, src: *const T) -> Self {
Self {
src: src as *const u8,
dst: dst as *mut u8 as *const u8,
remaining: mem::size_of::<T>(),
offset: 0,
}
}
/// Advances pointers by `size` **bytes**.
///
/// # Safety
///
/// - The remaining bytes must be greater than or equal to `size`.
/// - The range of `self.dst..self.dst.add(size)` must be filled.
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
unsafe fn advance(&mut self, size: usize) {
debug_assert!(self.remaining >= size);
self.remaining -= size;
self.offset += size;
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) fn remaining(&self) -> usize {
self.remaining
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
unsafe fn src<T>(&self) -> *const T {
// SAFETY: the caller must uphold the safety contract.
unsafe { self.src.add(self.offset) as *const T }
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
unsafe fn dst<T>(&self) -> &T {
// SAFETY: the caller must uphold the safety contract.
unsafe { &*(self.dst.add(self.offset) as *const T) }
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) fn atomic_store_u8(&mut self, count: usize) {
// This condition is also checked by the caller, so the compiler
// will remove this assertion by optimization.
assert!(self.remaining() >= count);
for _ in 0..count {
// SAFETY:
// - we've checked that the remaining bytes is greater than or equal to `count`
// Therefore, due to `StoreState`'s invariant:
// - `src` is valid to read of `count` of u8.
// - `dst` is valid to atomic write of `count` of u8.
unsafe {
let val = self.src::<u8>().read();
self.dst::<AtomicU8>().store(val, Ordering::Relaxed);
// SAFETY: we've filled 1 byte.
self.advance(1);
}
}
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(super) unsafe fn atomic_store_usize(&mut self) {
while self.remaining() >= mem::size_of::<usize>() {
// SAFETY:
// - the caller must guarantee that `dst` is properly aligned for `usize`.
// - we've checked that the remaining bytes is greater than
// or equal to `size_of::<usize>()`.
// Therefore, due to `StoreState`'s invariant:
// - `src` is valid to *unaligned* read of `usize`.
// - `dst` is valid to atomic write of `usize`.
unsafe {
let val = self.src::<usize>().read_unaligned();
self.dst::<AtomicUsize>().store(val, Ordering::Relaxed);
// SAFETY: we've filled `size_of::<usize>()` bytes.
self.advance(mem::size_of::<usize>());
}
}
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
#[cfg(not(target_pointer_width = "16"))]
pub(super) unsafe fn atomic_store_half(&mut self) {
use super::{AtomicHalf, Half};
debug_assert!(self.remaining() >= mem::size_of::<Half>());
// SAFETY:
// - the caller must guarantee that `dst` is properly aligned for `Half`.
// - we've checked that the remaining bytes is greater than
// or equal to `size_of::<Half>()`.
// Therefore, due to `StoreState`'s invariant:
// - `src` is valid to read of `Half`.
// - `dst` is valid to atomic write of `Half`.
unsafe {
let val = self.src::<Half>().read();
self.dst::<AtomicHalf>().store(val, Ordering::Relaxed);
// SAFETY: we've filled `size_of::<Half>()` bytes.
self.advance(mem::size_of::<Half>());
}
}
}
}
/// Byte-wise atomic store.
///
/// See the [`atomic_load`] function for the detailed implementation comment.
///
/// # Safety
///
/// See the documentation of [crate root's `atomic_store`](crate::atomic_store) for safety requirements.
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(crate) unsafe fn atomic_store<T>(dst: *mut T, val: T) {
// Safety requirements guaranteed by the caller:
// - `dst` is valid for atomic writes.
// - `dst` is properly aligned for `T`.
// - `dst` go through `UnsafeCell::get`.
// - there are no concurrent non-atomic operations.
// - there are no concurrent atomic operations of different granularity.
// - if there are concurrent atomic write operations, `T` is valid for all bit patterns.
// Note that the safety of the code in this function relies on these guarantees,
// whether or not they are explicitly mentioned in the each safety comment.
debug_assert!(!dst.is_null());
debug_assert!(dst as usize % mem::align_of::<T>() == 0);
static_assert_atomic_alignment();
// In atomic_store, the panic *after* the first store operation is unsound
// because dst may become an invalid bit pattern.
//
// Our code is written very carefully so as not to cause panic, but we
// will use additional guards just in case.
//
// Note:
// - If the compiler can understand at compile time that panic will
// never occur, this guard will be removed (as with no-panic).
// - atomic_load does not modify the data, so it does not have this requirement.
// - If an invalid ordering is passed, it will be panic *before* the
// first store operation, so is fine.
let guard = PanicGuard;
let val = ManuallyDrop::new(val); // Do not drop `val`.
if mem::size_of::<T>() == 0 {
mem::forget(guard);
return;
}
// HACK: See the `atomic_load` function for the detailed comment.
if cfg!(miri) {
let mut state = store::StoreState::new(dst, &*val);
state.atomic_store_u8(state.remaining());
debug_assert_eq!(state.remaining(), 0);
mem::forget(guard);
return;
}
// Branch 1: If the alignment of `T` is less than usize, but `T` can be write as
// at least one or more usize, compute the align offset and write it
// like `(&[AtomicU8], &[AtomicUsize], &[AtomicU8])`.
if mem::align_of::<T>() < mem::align_of::<AtomicUsize>()
&& mem::size_of::<T>() >= mem::size_of::<usize>() * 4
{
let mut state = store::StoreState::new(dst, &*val);
#[cfg(not(target_pointer_width = "16"))]
{
// See the `atomic_load` function for the detailed comment.
if mem::align_of::<T>() >= mem::align_of::<Half>() {
if dst as usize % mem::size_of::<usize>() == 0 {
// SAFETY:
// - we've checked that `dst` is properly aligned for `usize`.
// - the remaining bytes is greater than or equal to `size_of::<usize>()`.
unsafe { state.atomic_store_usize() }
} else {
debug_assert_eq!(
dst as usize % mem::size_of::<usize>(),
mem::size_of::<Half>()
);
// SAFETY:
// - the caller must guarantee that `dst` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to `Half`.
// - the remaining bytes is greater than or equal to `size_of::<usize>()`.
unsafe {
state.atomic_store_half();
// SAFETY: we've advanced `size_of::<Half>()` bytes,
// so now `state.dst` is definitely aligned.
state.atomic_store_usize();
}
}
// Store remaining bytes.
if state.remaining() != 0 {
debug_assert_eq!(state.remaining(), mem::size_of::<Half>());
// SAFETY:
// - the caller must guarantee that `src` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to `Half`.
// - the remaining bytes is equal to `size_of::<Half>()`.
unsafe { state.atomic_store_half() }
}
mem::forget(guard);
return;
}
}
let offset = (dst as *mut u8).align_offset(mem::align_of::<AtomicUsize>());
// Note: align_offset may returns usize::MAX: https://github.com/rust-lang/rust/issues/62420
if state.remaining() >= offset {
// Store `offset` bytes per byte to align `state.dst`.
state.atomic_store_u8(offset);
debug_assert!(state.remaining() >= mem::size_of::<usize>());
// SAFETY:
// - align_offset succeeds and the `offset` bytes have been
// filled, so now `state.dst` is definitely aligned.
// - we've checked that the remaining bytes is greater than
// or equal to `size_of::<usize>()`.
//
// In this branch, the pointer to `state.src` is usually
// not properly aligned, so we use `atomic_store_usize_src_unaligned`,
// which has no requirement for alignment of `state.src`.
unsafe {
state.atomic_store_usize();
}
// Store remaining bytes per byte.
state.atomic_store_u8(state.remaining());
debug_assert_eq!(state.remaining(), 0);
mem::forget(guard);
return;
}
}
// Branch 2: If the alignment of `T` is greater than or equal to usize,
// we can write it as a chunk of usize from the first byte.
if mem::align_of::<T>() >= mem::align_of::<AtomicUsize>() {
let src = &*val as *const T as *const usize;
let dst = dst as *const AtomicUsize;
for i in range(0..mem::size_of::<T>() / mem::size_of::<usize>()) {
// SAFETY:
// - the caller must guarantee that `dst` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to usize.
// - the remaining bytes is greater than or equal to `size_of::<usize>()`.
unsafe {
let val: usize = src.add(i).read();
(*dst.add(i)).store(val, Ordering::Relaxed);
}
}
mem::forget(guard);
return;
}
#[cfg(not(target_pointer_width = "16"))]
{
// Branch 3: If the alignment of `T` is greater than or equal to u32,
// we can write it as a chunk of u32 from the first byte.
if mem::size_of::<usize>() > 4 && mem::align_of::<T>() >= mem::align_of::<AtomicU32>() {
let src = &*val as *const T as *const u32;
let dst = dst as *const AtomicU32;
for i in range(0..mem::size_of::<T>() / mem::size_of::<u32>()) {
// SAFETY:
// - the caller must guarantee that `dst` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to u32.
// - the remaining bytes is greater than or equal to `size_of::<u32>()`.
unsafe {
let val: u32 = src.add(i).read();
(*dst.add(i)).store(val, Ordering::Relaxed);
}
}
mem::forget(guard);
return;
}
}
// Branch 4: If the alignment of `T` is greater than or equal to u16,
// we can write it as a chunk of u16 from the first byte.
if mem::size_of::<usize>() > 2 && mem::align_of::<T>() >= mem::align_of::<AtomicU16>() {
let src = &*val as *const T as *const u16;
let dst = dst as *const AtomicU16;
for i in range(0..mem::size_of::<T>() / mem::size_of::<u16>()) {
// SAFETY:
// - the caller must guarantee that `dst` is properly aligned for `T`.
// - `T` has an alignment greater than or equal to u16.
// - the remaining bytes is greater than or equal to `size_of::<u16>()`.
unsafe {
let val: u16 = src.add(i).read();
(*dst.add(i)).store(val, Ordering::Relaxed);
}
}
mem::forget(guard);
return;
}
// Branch 5: Otherwise, we write it per byte.
let mut state = store::StoreState::new(dst, &*val);
state.atomic_store_u8(state.remaining());
debug_assert_eq!(state.remaining(), 0);
mem::forget(guard);
}
// This allows read_volatile and atomic_load to be lowered to exactly the
// same assembly on little endian platforms such as aarch64, riscv64.
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
#[cfg(target_endian = "little")]
fn range<T>(r: Range<T>) -> core::iter::Rev<Range<T>>
where
Range<T>: DoubleEndedIterator,
{
r.rev()
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
#[cfg(target_endian = "big")]
fn range<T>(r: Range<T>) -> Range<T>
where
Range<T>: DoubleEndedIterator,
{
r
}
/// Atomic integers larger than the pointer size often does not have the
/// same alignment as the corresponding integer types.
///
/// ```console
/// $ rustc --print cfg --target x86_64-apple-darwin | grep -E 'target_has_atomic_.*(64|128)'
/// target_has_atomic_equal_alignment="64"
/// target_has_atomic_load_store="128"
/// target_has_atomic_load_store="64"
/// ```
///
/// It's unlikely that the same thing will happen with an atomic type
/// less than or equal to the pointer size, but we'll check just in case.
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
fn static_assert_atomic_alignment() {
let [] = [(); mem::align_of::<usize>() - mem::align_of::<AtomicUsize>()];
#[cfg(not(any(target_pointer_width = "16", target_pointer_width = "32")))]
let [] = [(); mem::align_of::<u64>() - mem::align_of::<AtomicU64>()];
#[cfg(not(target_pointer_width = "16"))]
let [] = [(); mem::align_of::<u32>() - mem::align_of::<AtomicU32>()];
let [] = [(); mem::align_of::<u16>() - mem::align_of::<AtomicU16>()];
}
struct PanicGuard;
impl Drop for PanicGuard {
fn drop(&mut self) {
// This crate supports no-std environment, so we cannot use std::process::abort.
// Instead, it uses the nature of double panics being converted to an abort.
panic!("abort");
}
}
}
#[cfg(any(
target_pointer_width = "16",
all(target_arch = "riscv32", not(target_feature = "a"), target_os = "none"),
atomic_memcpy_unsafe_volatile,
))]
mod imp {
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(crate) unsafe fn atomic_load<T>(src: *const T) -> core::mem::MaybeUninit<T> {
// SAFETY: the user who explicitly specified the `--cfg atomic_memcpy_unsafe_volatile`
// must guarantees that the volatile read would not cause data races.
//
// HACK: Using volatile read/write instead of atomic load/store on single-threaded platforms where
// LLVM does not support atomic is normally considered to be an okay workaround.
// <https://github.com/rust-lang/compiler-builtins/commit/e0187f17dbcbf9dc026d379b2af8d866300596a5>
unsafe { (src as *const core::mem::MaybeUninit<T>).read_volatile() }
}
#[cfg_attr(feature = "inline-always", inline(always))]
#[cfg_attr(not(feature = "inline-always"), inline)]
pub(crate) unsafe fn atomic_store<T>(dst: *mut T, src: T) {
// SAFETY: the user who explicitly specified the `--cfg atomic_memcpy_unsafe_volatile`
// must guarantees that the volatile write would not cause data races.
//
// HACK: Using volatile read/write instead of atomic load/store on single-threaded platforms where
// LLVM does not support atomic is normally considered to be an okay workaround.
// <https://github.com/rust-lang/compiler-builtins/commit/e0187f17dbcbf9dc026d379b2af8d866300596a5>