@@ -11,11 +11,10 @@ use core::{
11
11
ptr:: NonNull ,
12
12
result:: Result as CoreResult ,
13
13
slice:: from_raw_parts_mut,
14
- sync:: atomic:: {
15
- AtomicBool , AtomicUsize ,
16
- Ordering :: { AcqRel , Acquire , Release } ,
17
- } ,
14
+ sync:: atomic:: Ordering :: { AcqRel , Acquire , Release } ,
18
15
} ;
16
+ use portable_atomic:: { AtomicBool , AtomicUsize } ;
17
+
19
18
#[ derive( Debug ) ]
20
19
/// A backing structure for a BBQueue. Can be used to create either
21
20
/// a BBQueue or a split Producer/Consumer pair
@@ -63,9 +62,6 @@ impl<'a, const N: usize> BBBuffer<N> {
63
62
/// is placed at `static` scope within the `.bss` region, the explicit initialization
64
63
/// will be elided (as it is already performed as part of memory initialization)
65
64
///
66
- /// NOTE: If the `cortex-m` feature is selected, this function takes a short critical section
67
- /// while splitting.
68
- ///
69
65
/// ```rust
70
66
/// # // bbqueue test shim!
71
67
/// # fn bbqtest() {
@@ -81,12 +77,11 @@ impl<'a, const N: usize> BBBuffer<N> {
81
77
/// # }
82
78
/// #
83
79
/// # fn main() {
84
- /// # #[cfg(not(feature = "cortex-m"))]
85
80
/// # bbqtest();
86
81
/// # }
87
82
/// ```
88
83
pub fn try_split ( & ' a self ) -> Result < ( Producer < ' a , N > , Consumer < ' a , N > ) > {
89
- if atomic :: swap ( & self . already_split , true , AcqRel ) {
84
+ if self . already_split . swap ( true , AcqRel ) {
90
85
return Err ( Error :: AlreadySplit ) ;
91
86
}
92
87
@@ -122,9 +117,6 @@ impl<'a, const N: usize> BBBuffer<N> {
122
117
/// of the buffer. This is necessary to prevent undefined behavior. If the buffer
123
118
/// is placed at `static` scope within the `.bss` region, the explicit initialization
124
119
/// will be elided (as it is already performed as part of memory initialization)
125
- ///
126
- /// NOTE: If the `cortex-m` feature is selected, this function takes a short critical
127
- /// section while splitting.
128
120
pub fn try_split_framed ( & ' a self ) -> Result < ( FrameProducer < ' a , N > , FrameConsumer < ' a , N > ) > {
129
121
let ( producer, consumer) = self . try_split ( ) ?;
130
122
Ok ( ( FrameProducer { producer } , FrameConsumer { consumer } ) )
@@ -159,7 +151,6 @@ impl<'a, const N: usize> BBBuffer<N> {
159
151
/// # }
160
152
/// #
161
153
/// # fn main() {
162
- /// # #[cfg(not(feature = "cortex-m"))]
163
154
/// # bbqtest();
164
155
/// # }
165
156
/// ```
@@ -345,14 +336,13 @@ impl<'a, const N: usize> Producer<'a, N> {
345
336
/// # }
346
337
/// #
347
338
/// # fn main() {
348
- /// # #[cfg(not(feature = "cortex-m"))]
349
339
/// # bbqtest();
350
340
/// # }
351
341
/// ```
352
342
pub fn grant_exact ( & mut self , sz : usize ) -> Result < GrantW < ' a , N > > {
353
343
let inner = unsafe { & self . bbq . as_ref ( ) } ;
354
344
355
- if atomic :: swap ( & inner. write_in_progress , true , AcqRel ) {
345
+ if inner. write_in_progress . swap ( true , AcqRel ) {
356
346
return Err ( Error :: GrantInProgress ) ;
357
347
}
358
348
@@ -443,14 +433,13 @@ impl<'a, const N: usize> Producer<'a, N> {
443
433
/// # }
444
434
/// #
445
435
/// # fn main() {
446
- /// # #[cfg(not(feature = "cortex-m"))]
447
436
/// # bbqtest();
448
437
/// # }
449
438
/// ```
450
439
pub fn grant_max_remaining ( & mut self , mut sz : usize ) -> Result < GrantW < ' a , N > > {
451
440
let inner = unsafe { & self . bbq . as_ref ( ) } ;
452
441
453
- if atomic :: swap ( & inner. write_in_progress , true , AcqRel ) {
442
+ if inner. write_in_progress . swap ( true , AcqRel ) {
454
443
return Err ( Error :: GrantInProgress ) ;
455
444
}
456
445
@@ -548,14 +537,13 @@ impl<'a, const N: usize> Consumer<'a, N> {
548
537
/// # }
549
538
/// #
550
539
/// # fn main() {
551
- /// # #[cfg(not(feature = "cortex-m"))]
552
540
/// # bbqtest();
553
541
/// # }
554
542
/// ```
555
543
pub fn read ( & mut self ) -> Result < GrantR < ' a , N > > {
556
544
let inner = unsafe { & self . bbq . as_ref ( ) } ;
557
545
558
- if atomic :: swap ( & inner. read_in_progress , true , AcqRel ) {
546
+ if inner. read_in_progress . swap ( true , AcqRel ) {
559
547
return Err ( Error :: GrantInProgress ) ;
560
548
}
561
549
@@ -607,7 +595,7 @@ impl<'a, const N: usize> Consumer<'a, N> {
607
595
pub fn split_read ( & mut self ) -> Result < SplitGrantR < ' a , N > > {
608
596
let inner = unsafe { & self . bbq . as_ref ( ) } ;
609
597
610
- if atomic :: swap ( & inner. read_in_progress , true , AcqRel ) {
598
+ if inner. read_in_progress . swap ( true , AcqRel ) {
611
599
return Err ( Error :: GrantInProgress ) ;
612
600
}
613
601
@@ -674,7 +662,6 @@ impl<const N: usize> BBBuffer<N> {
674
662
/// # }
675
663
/// #
676
664
/// # fn main() {
677
- /// # #[cfg(not(feature = "cortex-m"))]
678
665
/// # bbqtest();
679
666
/// # }
680
667
/// ```
@@ -690,9 +677,6 @@ impl<const N: usize> BBBuffer<N> {
690
677
/// the contents, or by setting a the number of bytes to
691
678
/// automatically be committed with `to_commit()`, then no bytes
692
679
/// will be comitted for writing.
693
- ///
694
- /// If the `cortex-m` feature is selected, dropping the grant
695
- /// without committing it takes a short critical section,
696
680
#[ derive( Debug , PartialEq ) ]
697
681
pub struct GrantW < ' a , const N : usize > {
698
682
pub ( crate ) buf : & ' a mut [ u8 ] ,
@@ -710,10 +694,6 @@ unsafe impl<'a, const N: usize> Send for GrantW<'a, N> {}
710
694
/// the contents, or by setting the number of bytes to automatically
711
695
/// be released with `to_release()`, then no bytes will be released
712
696
/// as read.
713
- ///
714
- ///
715
- /// If the `cortex-m` feature is selected, dropping the grant
716
- /// without releasing it takes a short critical section,
717
697
#[ derive( Debug , PartialEq ) ]
718
698
pub struct GrantR < ' a , const N : usize > {
719
699
pub ( crate ) buf : & ' a mut [ u8 ] ,
@@ -743,9 +723,6 @@ impl<'a, const N: usize> GrantW<'a, N> {
743
723
///
744
724
/// If `used` is larger than the given grant, the maximum amount will
745
725
/// be commited
746
- ///
747
- /// NOTE: If the `cortex-m` feature is selected, this function takes a short critical
748
- /// section while committing.
749
726
pub fn commit ( mut self , used : usize ) {
750
727
self . commit_inner ( used) ;
751
728
forget ( self ) ;
@@ -770,7 +747,6 @@ impl<'a, const N: usize> GrantW<'a, N> {
770
747
/// # }
771
748
/// #
772
749
/// # fn main() {
773
- /// # #[cfg(not(feature = "cortex-m"))]
774
750
/// # bbqtest();
775
751
/// # }
776
752
/// ```
@@ -814,7 +790,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
814
790
let used = min ( len, used) ;
815
791
816
792
let write = inner. write . load ( Acquire ) ;
817
- atomic :: fetch_sub ( & inner. reserve , len - used, AcqRel ) ;
793
+ inner. reserve . fetch_sub ( len - used, AcqRel ) ;
818
794
819
795
let max = N ;
820
796
let last = inner. last . load ( Acquire ) ;
@@ -860,9 +836,6 @@ impl<'a, const N: usize> GrantR<'a, N> {
860
836
///
861
837
/// If `used` is larger than the given grant, the full grant will
862
838
/// be released.
863
- ///
864
- /// NOTE: If the `cortex-m` feature is selected, this function takes a short critical
865
- /// section while releasing.
866
839
pub fn release ( mut self , used : usize ) {
867
840
// Saturate the grant release
868
841
let used = min ( self . buf . len ( ) , used) ;
@@ -903,7 +876,6 @@ impl<'a, const N: usize> GrantR<'a, N> {
903
876
/// # }
904
877
/// #
905
878
/// # fn main() {
906
- /// # #[cfg(not(feature = "cortex-m"))]
907
879
/// # bbqtest();
908
880
/// # }
909
881
/// ```
@@ -951,7 +923,7 @@ impl<'a, const N: usize> GrantR<'a, N> {
951
923
debug_assert ! ( used <= self . buf. len( ) ) ;
952
924
953
925
// This should be fine, purely incrementing
954
- let _ = atomic :: fetch_add ( & inner. read , used, Release ) ;
926
+ let _ = inner. read . fetch_add ( used, Release ) ;
955
927
956
928
inner. read_in_progress . store ( false , Release ) ;
957
929
}
@@ -968,9 +940,6 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
968
940
///
969
941
/// If `used` is larger than the given grant, the full grant will
970
942
/// be released.
971
- ///
972
- /// NOTE: If the `cortex-m` feature is selected, this function takes a short critical
973
- /// section while releasing.
974
943
pub fn release ( mut self , used : usize ) {
975
944
// Saturate the grant release
976
945
let used = min ( self . combined_len ( ) , used) ;
@@ -1004,7 +973,6 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
1004
973
/// # }
1005
974
/// #
1006
975
/// # fn main() {
1007
- /// # #[cfg(not(feature = "cortex-m"))]
1008
976
/// # bbqtest();
1009
977
/// # }
1010
978
/// ```
@@ -1036,7 +1004,7 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
1036
1004
1037
1005
if used <= self . buf1 . len ( ) {
1038
1006
// This should be fine, purely incrementing
1039
- let _ = atomic :: fetch_add ( & inner. read , used, Release ) ;
1007
+ let _ = inner. read . fetch_add ( used, Release ) ;
1040
1008
} else {
1041
1009
// Also release parts of the second buffer
1042
1010
inner. read . store ( used - self . buf1 . len ( ) , Release ) ;
@@ -1101,59 +1069,3 @@ impl<'a, const N: usize> DerefMut for GrantR<'a, N> {
1101
1069
self . buf
1102
1070
}
1103
1071
}
1104
-
1105
- #[ cfg( feature = "cortex-m" ) ]
1106
- mod atomic {
1107
- use core:: sync:: atomic:: {
1108
- AtomicBool , AtomicUsize ,
1109
- Ordering :: { self , Acquire , Release } ,
1110
- } ;
1111
- use cortex_m:: interrupt:: free;
1112
-
1113
- #[ inline( always) ]
1114
- pub fn fetch_add ( atomic : & AtomicUsize , val : usize , _order : Ordering ) -> usize {
1115
- free ( |_| {
1116
- let prev = atomic. load ( Acquire ) ;
1117
- atomic. store ( prev. wrapping_add ( val) , Release ) ;
1118
- prev
1119
- } )
1120
- }
1121
-
1122
- #[ inline( always) ]
1123
- pub fn fetch_sub ( atomic : & AtomicUsize , val : usize , _order : Ordering ) -> usize {
1124
- free ( |_| {
1125
- let prev = atomic. load ( Acquire ) ;
1126
- atomic. store ( prev. wrapping_sub ( val) , Release ) ;
1127
- prev
1128
- } )
1129
- }
1130
-
1131
- #[ inline( always) ]
1132
- pub fn swap ( atomic : & AtomicBool , val : bool , _order : Ordering ) -> bool {
1133
- free ( |_| {
1134
- let prev = atomic. load ( Acquire ) ;
1135
- atomic. store ( val, Release ) ;
1136
- prev
1137
- } )
1138
- }
1139
- }
1140
-
1141
- #[ cfg( not( feature = "cortex-m" ) ) ]
1142
- mod atomic {
1143
- use core:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
1144
-
1145
- #[ inline( always) ]
1146
- pub fn fetch_add ( atomic : & AtomicUsize , val : usize , order : Ordering ) -> usize {
1147
- atomic. fetch_add ( val, order)
1148
- }
1149
-
1150
- #[ inline( always) ]
1151
- pub fn fetch_sub ( atomic : & AtomicUsize , val : usize , order : Ordering ) -> usize {
1152
- atomic. fetch_sub ( val, order)
1153
- }
1154
-
1155
- #[ inline( always) ]
1156
- pub fn swap ( atomic : & AtomicBool , val : bool , order : Ordering ) -> bool {
1157
- atomic. swap ( val, order)
1158
- }
1159
- }
0 commit comments