Skip to content

Commit aa75a81

Browse files
committed
sort: add wasi_no_threads cfg alias to reduce predicate verbosity
1 parent 3ba572b commit aa75a81

File tree

6 files changed

+72
-78
lines changed

6 files changed

+72
-78
lines changed

src/uu/sort/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn main() {
2+
// Set a short alias for the WASI-without-threads configuration so that
3+
// source files can use `#[cfg(wasi_no_threads)]` instead of the verbose
4+
// `#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]`.
5+
println!("cargo::rustc-check-cfg=cfg(wasi_no_threads)");
6+
7+
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
8+
let has_atomics = std::env::var("CARGO_CFG_TARGET_FEATURE")
9+
.map(|f| f.split(',').any(|feat| feat == "atomics"))
10+
.unwrap_or(false);
11+
12+
if target_os == "wasi" && !has_atomics {
13+
println!("cargo::rustc-cfg=wasi_no_threads");
14+
}
15+
}

src/uu/sort/src/check.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ use crate::{
1111
compare_by, open,
1212
};
1313
use itertools::Itertools;
14-
use std::{
15-
cmp::Ordering,
16-
ffi::OsStr,
17-
io::Read,
18-
iter,
19-
};
20-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
21-
use std::sync::mpsc::{sync_channel, SyncSender, Receiver};
22-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
14+
#[cfg(not(wasi_no_threads))]
15+
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
16+
#[cfg(not(wasi_no_threads))]
2317
use std::thread;
18+
use std::{cmp::Ordering, ffi::OsStr, io::Read, iter};
2419
use uucore::error::UResult;
2520

2621
/// Check if the file at `path` is ordered.
@@ -41,17 +36,17 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
4136
100 * 1024
4237
};
4338

44-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
39+
#[cfg(not(wasi_no_threads))]
4540
{
4641
check_threaded(path, settings, max_allowed_cmp, file, chunk_size)
4742
}
48-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
43+
#[cfg(wasi_no_threads)]
4944
{
5045
check_sync(path, settings, max_allowed_cmp, file, chunk_size)
5146
}
5247
}
5348

54-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
49+
#[cfg(not(wasi_no_threads))]
5550
fn check_threaded(
5651
path: &OsStr,
5752
settings: &GlobalSettings,
@@ -115,7 +110,7 @@ fn check_threaded(
115110
}
116111

117112
/// The function running on the reader thread.
118-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
113+
#[cfg(not(wasi_no_threads))]
119114
fn reader(
120115
mut file: Box<dyn Read + Send>,
121116
receiver: &Receiver<RecycledChunk>,
@@ -142,7 +137,7 @@ fn reader(
142137
}
143138

144139
/// Synchronous check for targets without thread support.
145-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
140+
#[cfg(wasi_no_threads)]
146141
fn check_sync(
147142
path: &OsStr,
148143
settings: &GlobalSettings,

src/uu/sort/src/chunks.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#![allow(dead_code)]
1010
// Ignores non-used warning for `borrow_buffer` in `Chunk`
1111

12+
#[cfg(not(wasi_no_threads))]
13+
use std::sync::mpsc::SyncSender;
1214
use std::{
1315
io::{ErrorKind, Read},
1416
ops::Range,
1517
};
16-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
17-
use std::sync::mpsc::SyncSender;
1818

1919
use memchr::memchr_iter;
2020
use self_cell::self_cell;
@@ -246,7 +246,7 @@ pub fn read_to_chunk<T: Read>(
246246
/// Read a chunk, parse lines and send them via channel.
247247
///
248248
/// Wrapper around [`read_to_chunk`] for the threaded code path.
249-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
249+
#[cfg(not(wasi_no_threads))]
250250
#[allow(clippy::too_many_arguments)]
251251
pub fn read<T: Read>(
252252
sender: &SyncSender<Chunk>,
@@ -445,4 +445,3 @@ fn read_to_buffer<T: Read>(
445445
}
446446
}
447447
}
448-

src/uu/sort/src/ext_sort/threaded.rs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ use std::cmp::Ordering;
1010
use std::fs::File;
1111
use std::io::{Read, Write, stderr};
1212
use std::path::PathBuf;
13-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
13+
#[cfg(not(wasi_no_threads))]
1414
use std::sync::mpsc::{Receiver, SyncSender};
15-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
15+
#[cfg(not(wasi_no_threads))]
1616
use std::thread;
1717

1818
use itertools::Itertools;
1919
use uucore::error::UResult;
20-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
20+
#[cfg(not(wasi_no_threads))]
2121
use uucore::error::strip_errno;
2222

2323
use crate::Output;
2424
use crate::chunks::RecycledChunk;
25-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
25+
#[cfg(not(wasi_no_threads))]
2626
use crate::merge::WriteableCompressedTmpFile;
2727
use crate::merge::WriteablePlainTmpFile;
2828
use crate::merge::WriteableTmpFile;
@@ -41,7 +41,7 @@ const DEFAULT_BUF_SIZE: usize = 8 * 1024;
4141
///
4242
/// Uses the same chunked sort-write-merge strategy as the threaded version,
4343
/// but reads and sorts each chunk sequentially on the calling thread.
44-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
44+
#[cfg(wasi_no_threads)]
4545
pub fn ext_sort(
4646
files: &mut impl Iterator<Item = UResult<Box<dyn Read + Send>>>,
4747
settings: &GlobalSettings,
@@ -159,17 +159,12 @@ pub fn ext_sort(
159159
}
160160

161161
/// Print a single sorted chunk.
162-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
163-
fn print_chunk(
164-
chunk: &Chunk,
165-
settings: &GlobalSettings,
166-
output: Output,
167-
) -> UResult<()> {
162+
#[cfg(wasi_no_threads)]
163+
fn print_chunk(chunk: &Chunk, settings: &GlobalSettings, output: Output) -> UResult<()> {
168164
if settings.unique {
169165
print_sorted(
170166
chunk.lines().iter().dedup_by(|a, b| {
171-
compare_by(a, b, settings, chunk.line_data(), chunk.line_data())
172-
== Ordering::Equal
167+
compare_by(a, b, settings, chunk.line_data(), chunk.line_data()) == Ordering::Equal
173168
}),
174169
settings,
175170
output,
@@ -180,18 +175,12 @@ fn print_chunk(
180175
}
181176

182177
/// Merge two in-memory chunks and print.
183-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
184-
fn print_two_chunks(
185-
a: Chunk,
186-
b: Chunk,
187-
settings: &GlobalSettings,
188-
output: Output,
189-
) -> UResult<()> {
178+
#[cfg(wasi_no_threads)]
179+
fn print_two_chunks(a: Chunk, b: Chunk, settings: &GlobalSettings, output: Output) -> UResult<()> {
190180
let merged_iter = a.lines().iter().map(|line| (line, &a)).merge_by(
191181
b.lines().iter().map(|line| (line, &b)),
192182
|(line_a, a), (line_b, b)| {
193-
compare_by(line_a, line_b, settings, a.line_data(), b.line_data())
194-
!= Ordering::Greater
183+
compare_by(line_a, line_b, settings, a.line_data(), b.line_data()) != Ordering::Greater
195184
},
196185
);
197186
if settings.unique {
@@ -215,7 +204,7 @@ fn print_two_chunks(
215204
/// Two threads cooperate: one reads input and writes temporary chunk files,
216205
/// while the other sorts each chunk in memory. Once all chunks are written,
217206
/// they are merged back together for final output.
218-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
207+
#[cfg(not(wasi_no_threads))]
219208
pub fn ext_sort(
220209
files: &mut impl Iterator<Item = UResult<Box<dyn Read + Send>>>,
221210
settings: &GlobalSettings,
@@ -276,7 +265,7 @@ pub fn ext_sort(
276265
}
277266
}
278267

279-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
268+
#[cfg(not(wasi_no_threads))]
280269
fn reader_writer<
281270
F: Iterator<Item = UResult<Box<dyn Read + Send>>>,
282271
Tmp: WriteableTmpFile + 'static,
@@ -362,7 +351,7 @@ fn reader_writer<
362351
}
363352

364353
/// The function that is executed on the sorter thread.
365-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
354+
#[cfg(not(wasi_no_threads))]
366355
fn sorter(receiver: &Receiver<Chunk>, sender: &SyncSender<Chunk>, settings: &GlobalSettings) {
367356
while let Ok(mut payload) = receiver.recv() {
368357
payload.with_dependent_mut(|_, contents| {
@@ -377,7 +366,7 @@ fn sorter(receiver: &Receiver<Chunk>, sender: &SyncSender<Chunk>, settings: &Glo
377366
}
378367

379368
/// Describes how we read the chunks from the input.
380-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
369+
#[cfg(not(wasi_no_threads))]
381370
enum ReadResult<I: WriteableTmpFile> {
382371
/// The input was empty. Nothing was read.
383372
EmptyInput,
@@ -389,7 +378,7 @@ enum ReadResult<I: WriteableTmpFile> {
389378
WroteChunksToFile { tmp_files: Vec<I::Closed> },
390379
}
391380
/// The function that is executed on the reader/writer thread.
392-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
381+
#[cfg(not(wasi_no_threads))]
393382
fn read_write_loop<I: WriteableTmpFile>(
394383
mut files: impl Iterator<Item = UResult<Box<dyn Read + Send>>>,
395384
tmp_dir: &mut TmpDirWrapper,

0 commit comments

Comments
 (0)