Skip to content

Commit 158cd35

Browse files
veluca93Virv12
authored andcommitted
Use thingbuf instead of a custom "m"psc.
1 parent accab42 commit 158cd35

7 files changed

Lines changed: 53 additions & 103 deletions

File tree

pixie-shared/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub type ChunkHash = [u8; OUT_LEN];
3939
pub type Offset = usize;
4040

4141
/// Describes one chunk from a disk.
42-
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
42+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
4343
pub struct Chunk {
4444
pub hash: ChunkHash,
4545
pub start: Offset,

pixie-uefi/Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pixie-uefi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ postcard = { version = "1.1.3", default-features = false, features = ["alloc"] }
2727
rand = { version = "0.8.5", default-features = false }
2828
rand_xoshiro = { version = "0.6.0", default-features = false }
2929
smoltcp = { version = "0.12.0", default-features = false, features = ["alloc", "proto-ipv4", "medium-ethernet", "socket-udp", "socket-tcp", "socket-dhcpv4", "async", "socket-tcp-cubic"] }
30+
thingbuf = { version = "0.1.6", default-features = false, features = ["alloc"] }
3031
uefi = { version = "0.33.0", features = ["alloc", "global_allocator", "panic_handler"] }
3132

3233
[dependencies.pixie-shared]

pixie-uefi/src/flash.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
os::{
33
error::{Error, Result},
4-
mpsc, TcpStream, UefiOS, PACKET_SIZE,
4+
TcpStream, UefiOS, PACKET_SIZE,
55
},
66
MIN_MEMORY,
77
};
@@ -162,10 +162,10 @@ pub async fn flash(os: UefiOS, server_addr: SocketAddrV4) -> Result<()> {
162162

163163
let mut received = BTreeMap::new();
164164

165-
let (tx, mut rx) = mpsc::channel(128);
165+
let (tx, rx) = thingbuf::mpsc::channel(128);
166166

167167
let task1 = async {
168-
let mut tx = tx;
168+
let tx = tx;
169169
let mut last_seen = Vec::new();
170170
let total_mem = os.get_total_mem();
171171
let max_chunks = (total_mem.saturating_sub(MIN_MEMORY) as usize / MAX_CHUNK_SIZE).max(128);
@@ -184,7 +184,7 @@ pub async fn flash(os: UefiOS, server_addr: SocketAddrV4) -> Result<()> {
184184
let chunk =
185185
handle_packet(buf, &mut chunks_info, &mut received, &mut last_seen)?;
186186
if let Some((pos, data)) = chunk {
187-
tx.send((pos, data)).await;
187+
tx.send((pos, data)).await.expect("receiver was dropped");
188188
}
189189

190190
assert_eq!(last_seen.len(), received.len());

pixie-uefi/src/os/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ mod boot_options;
4848
pub mod disk;
4949
pub mod error;
5050
mod executor;
51-
pub mod mpsc;
5251
mod net;
5352
mod rng;
5453
mod sync;

pixie-uefi/src/os/mpsc.rs

Lines changed: 0 additions & 84 deletions
This file was deleted.

pixie-uefi/src/store.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
os::{
33
error::{Error, Result},
4-
mpsc, TcpStream, UefiOS,
4+
TcpStream, UefiOS,
55
},
66
parse_disk, MIN_MEMORY,
77
};
@@ -92,13 +92,13 @@ pub async fn store(os: UefiOS, server_address: SocketAddrV4) -> Result<()> {
9292
BytesFmt(total_mem)
9393
);
9494

95-
let (tx1, mut rx1) = mpsc::channel(channel_size);
96-
let (tx2, mut rx2) = mpsc::channel(channel_size);
97-
let (tx3, mut rx3) = mpsc::channel(channel_size);
98-
let (tx4, mut rx4) = mpsc::channel(channel_size);
95+
let (tx1, rx1) = thingbuf::mpsc::channel(channel_size);
96+
let (tx2, rx2) = thingbuf::mpsc::channel(channel_size);
97+
let (tx3, rx3) = thingbuf::mpsc::channel(channel_size);
98+
let (tx4, rx4) = thingbuf::mpsc::channel(channel_size);
9999

100100
let task1 = async {
101-
let mut tx1 = tx1;
101+
let tx1 = tx1;
102102
for chunk_info in chunks {
103103
let mut data = vec![0; chunk_info.size];
104104
disk.read(chunk_info.start as u64, &mut data).await?;
@@ -110,45 +110,49 @@ pub async fn store(os: UefiOS, server_address: SocketAddrV4) -> Result<()> {
110110
size: chunk_info.size,
111111
csize: cdata.len(),
112112
};
113-
tx1.send((chunk, cdata)).await;
113+
tx1.send((chunk, cdata)).await.expect("receiver dropped");
114114
}
115115
Ok::<_, Error>(())
116116
};
117117

118118
let task2 = async {
119-
let mut tx2 = tx2;
119+
let tx2 = tx2;
120120
while let Some((chunk, cdata)) = rx1.recv().await {
121121
let req = TcpRequest::HasChunk(chunk.hash);
122122
let buf = postcard::to_allocvec(&req)?;
123123
stream_get_csize.send_u64_le(buf.len() as u64).await?;
124124
stream_get_csize.send(&buf).await?;
125-
tx2.send((chunk, cdata)).await;
125+
tx2.send((chunk, cdata)).await.expect("receiver dropped");
126126
}
127127
Ok(())
128128
};
129129

130130
let task3 = async {
131-
let mut tx3 = tx3;
131+
let tx3 = tx3;
132132
while let Some((chunk, cdata)) = rx2.recv().await {
133133
let len = stream_get_csize.recv_u64_le().await?;
134134
let mut buf = vec![0; len as usize];
135135
stream_get_csize.recv(&mut buf).await?;
136136
let has_chunk: bool = postcard::from_bytes(&buf)?;
137-
tx3.send((chunk, cdata, has_chunk)).await;
137+
tx3.send((chunk, cdata, has_chunk))
138+
.await
139+
.expect("receiver dropped");
138140
}
139141
Ok(())
140142
};
141143

142144
let task4 = async {
143-
let mut tx4 = tx4;
145+
let tx4 = tx4;
144146
while let Some((chunk, cdata, has_chunk)) = rx3.recv().await {
145147
if !has_chunk {
146148
let req = TcpRequest::UploadChunk(cdata);
147149
let buf = postcard::to_allocvec(&req)?;
148150
stream_upload_chunk.send_u64_le(buf.len() as u64).await?;
149151
stream_upload_chunk.send(&buf).await?;
150152
}
151-
tx4.send((chunk, has_chunk)).await;
153+
tx4.send((chunk, has_chunk))
154+
.await
155+
.expect("receiver dropped");
152156
}
153157
Ok(())
154158
};

0 commit comments

Comments
 (0)