diff options
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/watch.rs | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/embassy-sync/src/watch.rs b/embassy-sync/src/watch.rs index e0f5e14f9..90dcf97ef 100644 --- a/embassy-sync/src/watch.rs +++ b/embassy-sync/src/watch.rs | |||
| @@ -31,7 +31,7 @@ use crate::waitqueue::MultiWakerRegistration; | |||
| 31 | /// | 31 | /// |
| 32 | /// let f = async { | 32 | /// let f = async { |
| 33 | /// | 33 | /// |
| 34 | /// static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 34 | /// static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 35 | /// | 35 | /// |
| 36 | /// // Obtain receivers and sender | 36 | /// // Obtain receivers and sender |
| 37 | /// let mut rcv0 = WATCH.receiver().unwrap(); | 37 | /// let mut rcv0 = WATCH.receiver().unwrap(); |
| @@ -299,7 +299,19 @@ impl<M: RawMutex, T: Clone, const N: usize> WatchBehavior<T> for Watch<M, T, N> | |||
| 299 | 299 | ||
| 300 | impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> { | 300 | impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> { |
| 301 | /// Create a new `Watch` channel. | 301 | /// Create a new `Watch` channel. |
| 302 | pub const fn new(data: Option<T>) -> Self { | 302 | pub const fn new() -> Self { |
| 303 | Self { | ||
| 304 | mutex: Mutex::new(RefCell::new(WatchState { | ||
| 305 | data: None, | ||
| 306 | current_id: 0, | ||
| 307 | wakers: MultiWakerRegistration::new(), | ||
| 308 | receiver_count: 0, | ||
| 309 | })), | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | /// Create a new `Watch` channel. | ||
| 314 | pub const fn new_with(data: Option<T>) -> Self { | ||
| 303 | Self { | 315 | Self { |
| 304 | mutex: Mutex::new(RefCell::new(WatchState { | 316 | mutex: Mutex::new(RefCell::new(WatchState { |
| 305 | data, | 317 | data, |
| @@ -775,7 +787,7 @@ mod tests { | |||
| 775 | #[test] | 787 | #[test] |
| 776 | fn multiple_sends() { | 788 | fn multiple_sends() { |
| 777 | let f = async { | 789 | let f = async { |
| 778 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None); | 790 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); |
| 779 | 791 | ||
| 780 | // Obtain receiver and sender | 792 | // Obtain receiver and sender |
| 781 | let mut rcv = WATCH.receiver().unwrap(); | 793 | let mut rcv = WATCH.receiver().unwrap(); |
| @@ -801,7 +813,7 @@ mod tests { | |||
| 801 | #[test] | 813 | #[test] |
| 802 | fn all_try_get() { | 814 | fn all_try_get() { |
| 803 | let f = async { | 815 | let f = async { |
| 804 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None); | 816 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); |
| 805 | 817 | ||
| 806 | // Obtain receiver and sender | 818 | // Obtain receiver and sender |
| 807 | let mut rcv = WATCH.receiver().unwrap(); | 819 | let mut rcv = WATCH.receiver().unwrap(); |
| @@ -835,7 +847,7 @@ mod tests { | |||
| 835 | static CONFIG0: u8 = 10; | 847 | static CONFIG0: u8 = 10; |
| 836 | static CONFIG1: u8 = 20; | 848 | static CONFIG1: u8 = 20; |
| 837 | 849 | ||
| 838 | static WATCH: Watch<CriticalSectionRawMutex, &'static u8, 1> = Watch::new(None); | 850 | static WATCH: Watch<CriticalSectionRawMutex, &'static u8, 1> = Watch::new(); |
| 839 | 851 | ||
| 840 | // Obtain receiver and sender | 852 | // Obtain receiver and sender |
| 841 | let mut rcv = WATCH.receiver().unwrap(); | 853 | let mut rcv = WATCH.receiver().unwrap(); |
| @@ -867,7 +879,7 @@ mod tests { | |||
| 867 | #[test] | 879 | #[test] |
| 868 | fn sender_modify() { | 880 | fn sender_modify() { |
| 869 | let f = async { | 881 | let f = async { |
| 870 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None); | 882 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); |
| 871 | 883 | ||
| 872 | // Obtain receiver and sender | 884 | // Obtain receiver and sender |
| 873 | let mut rcv = WATCH.receiver().unwrap(); | 885 | let mut rcv = WATCH.receiver().unwrap(); |
| @@ -894,7 +906,7 @@ mod tests { | |||
| 894 | #[test] | 906 | #[test] |
| 895 | fn predicate_fn() { | 907 | fn predicate_fn() { |
| 896 | let f = async { | 908 | let f = async { |
| 897 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None); | 909 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); |
| 898 | 910 | ||
| 899 | // Obtain receiver and sender | 911 | // Obtain receiver and sender |
| 900 | let mut rcv = WATCH.receiver().unwrap(); | 912 | let mut rcv = WATCH.receiver().unwrap(); |
| @@ -923,7 +935,7 @@ mod tests { | |||
| 923 | #[test] | 935 | #[test] |
| 924 | fn receive_after_create() { | 936 | fn receive_after_create() { |
| 925 | let f = async { | 937 | let f = async { |
| 926 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None); | 938 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); |
| 927 | 939 | ||
| 928 | // Obtain sender and send value | 940 | // Obtain sender and send value |
| 929 | let snd = WATCH.sender(); | 941 | let snd = WATCH.sender(); |
| @@ -939,7 +951,7 @@ mod tests { | |||
| 939 | #[test] | 951 | #[test] |
| 940 | fn max_receivers_drop() { | 952 | fn max_receivers_drop() { |
| 941 | let f = async { | 953 | let f = async { |
| 942 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 954 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 943 | 955 | ||
| 944 | // Try to create 3 receivers (only 2 can exist at once) | 956 | // Try to create 3 receivers (only 2 can exist at once) |
| 945 | let rcv0 = WATCH.receiver(); | 957 | let rcv0 = WATCH.receiver(); |
| @@ -964,7 +976,7 @@ mod tests { | |||
| 964 | #[test] | 976 | #[test] |
| 965 | fn multiple_receivers() { | 977 | fn multiple_receivers() { |
| 966 | let f = async { | 978 | let f = async { |
| 967 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 979 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 968 | 980 | ||
| 969 | // Obtain receivers and sender | 981 | // Obtain receivers and sender |
| 970 | let mut rcv0 = WATCH.receiver().unwrap(); | 982 | let mut rcv0 = WATCH.receiver().unwrap(); |
| @@ -989,7 +1001,7 @@ mod tests { | |||
| 989 | fn clone_senders() { | 1001 | fn clone_senders() { |
| 990 | let f = async { | 1002 | let f = async { |
| 991 | // Obtain different ways to send | 1003 | // Obtain different ways to send |
| 992 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None); | 1004 | static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); |
| 993 | let snd0 = WATCH.sender(); | 1005 | let snd0 = WATCH.sender(); |
| 994 | let snd1 = snd0.clone(); | 1006 | let snd1 = snd0.clone(); |
| 995 | 1007 | ||
| @@ -1010,7 +1022,7 @@ mod tests { | |||
| 1010 | #[test] | 1022 | #[test] |
| 1011 | fn use_dynamics() { | 1023 | fn use_dynamics() { |
| 1012 | let f = async { | 1024 | let f = async { |
| 1013 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 1025 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 1014 | 1026 | ||
| 1015 | // Obtain receiver and sender | 1027 | // Obtain receiver and sender |
| 1016 | let mut anon_rcv = WATCH.dyn_anon_receiver(); | 1028 | let mut anon_rcv = WATCH.dyn_anon_receiver(); |
| @@ -1031,7 +1043,7 @@ mod tests { | |||
| 1031 | #[test] | 1043 | #[test] |
| 1032 | fn convert_to_dyn() { | 1044 | fn convert_to_dyn() { |
| 1033 | let f = async { | 1045 | let f = async { |
| 1034 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 1046 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 1035 | 1047 | ||
| 1036 | // Obtain receiver and sender | 1048 | // Obtain receiver and sender |
| 1037 | let anon_rcv = WATCH.anon_receiver(); | 1049 | let anon_rcv = WATCH.anon_receiver(); |
| @@ -1057,7 +1069,7 @@ mod tests { | |||
| 1057 | #[test] | 1069 | #[test] |
| 1058 | fn dynamic_receiver_count() { | 1070 | fn dynamic_receiver_count() { |
| 1059 | let f = async { | 1071 | let f = async { |
| 1060 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 1072 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 1061 | 1073 | ||
| 1062 | // Obtain receiver and sender | 1074 | // Obtain receiver and sender |
| 1063 | let rcv0 = WATCH.receiver(); | 1075 | let rcv0 = WATCH.receiver(); |
| @@ -1087,7 +1099,7 @@ mod tests { | |||
| 1087 | #[test] | 1099 | #[test] |
| 1088 | fn contains_value() { | 1100 | fn contains_value() { |
| 1089 | let f = async { | 1101 | let f = async { |
| 1090 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None); | 1102 | static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); |
| 1091 | 1103 | ||
| 1092 | // Obtain receiver and sender | 1104 | // Obtain receiver and sender |
| 1093 | let rcv = WATCH.receiver().unwrap(); | 1105 | let rcv = WATCH.receiver().unwrap(); |
