Skip to content

Commit 29b5c16

Browse files
committed
Use thingbuf instead of a custom "m"psc.
1 parent 98ae4ae commit 29b5c16

7 files changed

Lines changed: 46 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: 3 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,9 @@ 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;
169168
let mut last_seen = Vec::new();
170169
let total_mem = os.get_total_mem();
171170
let max_chunks = (total_mem.saturating_sub(MIN_MEMORY) as usize / MAX_CHUNK_SIZE).max(128);
@@ -184,7 +183,7 @@ pub async fn flash(os: UefiOS, server_addr: SocketAddrV4) -> Result<()> {
184183
let chunk =
185184
handle_packet(buf, &mut chunks_info, &mut received, &mut last_seen)?;
186185
if let Some((pos, data)) = chunk {
187-
tx.send((pos, data)).await;
186+
tx.send((pos, data)).await.expect("receiver was dropped");
188187
}
189188

190189
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: 11 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,12 @@ 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;
102101
for chunk_info in chunks {
103102
let mut data = vec![0; chunk_info.size];
104103
disk.read(chunk_info.start as u64, &mut data).await?;
@@ -110,45 +109,44 @@ pub async fn store(os: UefiOS, server_address: SocketAddrV4) -> Result<()> {
110109
size: chunk_info.size,
111110
csize: cdata.len(),
112111
};
113-
tx1.send((chunk, cdata)).await;
112+
tx1.send((chunk, cdata)).await.expect("sender dropped");
114113
}
115114
Ok::<_, Error>(())
116115
};
117116

118117
let task2 = async {
119-
let mut tx2 = tx2;
120118
while let Some((chunk, cdata)) = rx1.recv().await {
121119
let req = TcpRequest::HasChunk(chunk.hash);
122120
let buf = postcard::to_allocvec(&req)?;
123121
stream_get_csize.send_u64_le(buf.len() as u64).await?;
124122
stream_get_csize.send(&buf).await?;
125-
tx2.send((chunk, cdata)).await;
123+
tx2.send((chunk, cdata)).await.expect("sender dropped");
126124
}
127125
Ok(())
128126
};
129127

130128
let task3 = async {
131-
let mut tx3 = tx3;
132129
while let Some((chunk, cdata)) = rx2.recv().await {
133130
let len = stream_get_csize.recv_u64_le().await?;
134131
let mut buf = vec![0; len as usize];
135132
stream_get_csize.recv(&mut buf).await?;
136133
let has_chunk: bool = postcard::from_bytes(&buf)?;
137-
tx3.send((chunk, cdata, has_chunk)).await;
134+
tx3.send((chunk, cdata, has_chunk))
135+
.await
136+
.expect("sender dropped");
138137
}
139138
Ok(())
140139
};
141140

142141
let task4 = async {
143-
let mut tx4 = tx4;
144142
while let Some((chunk, cdata, has_chunk)) = rx3.recv().await {
145143
if !has_chunk {
146144
let req = TcpRequest::UploadChunk(cdata);
147145
let buf = postcard::to_allocvec(&req)?;
148146
stream_upload_chunk.send_u64_le(buf.len() as u64).await?;
149147
stream_upload_chunk.send(&buf).await?;
150148
}
151-
tx4.send((chunk, has_chunk)).await;
149+
tx4.send((chunk, has_chunk)).await.expect("sender dropped");
152150
}
153151
Ok(())
154152
};

0 commit comments

Comments
 (0)