Skip to content

Commit 748bdbf

Browse files
committed
sort: add wasi_no_threads cfg alias to reduce predicate verbosity
1 parent e66160a commit 748bdbf

File tree

6 files changed

+64
-49
lines changed

6 files changed

+64
-49
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use std::{
1717
io::Read,
1818
iter,
1919
};
20-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
20+
#[cfg(not(wasi_no_threads))]
2121
use std::sync::mpsc::{sync_channel, SyncSender, Receiver};
22-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
22+
#[cfg(not(wasi_no_threads))]
2323
use std::thread;
2424
use uucore::error::UResult;
2525

@@ -41,17 +41,17 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
4141
100 * 1024
4242
};
4343

44-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
44+
#[cfg(not(wasi_no_threads))]
4545
{
4646
check_threaded(path, settings, max_allowed_cmp, file, chunk_size)
4747
}
48-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
48+
#[cfg(wasi_no_threads)]
4949
{
5050
check_sync(path, settings, max_allowed_cmp, file, chunk_size)
5151
}
5252
}
5353

54-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
54+
#[cfg(not(wasi_no_threads))]
5555
fn check_threaded(
5656
path: &OsStr,
5757
settings: &GlobalSettings,
@@ -115,7 +115,7 @@ fn check_threaded(
115115
}
116116

117117
/// The function running on the reader thread.
118-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
118+
#[cfg(not(wasi_no_threads))]
119119
fn reader(
120120
mut file: Box<dyn Read + Send>,
121121
receiver: &Receiver<RecycledChunk>,
@@ -142,7 +142,7 @@ fn reader(
142142
}
143143

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

src/uu/sort/src/chunks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
io::{ErrorKind, Read},
1414
ops::Range,
1515
};
16-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
16+
#[cfg(not(wasi_no_threads))]
1717
use std::sync::mpsc::SyncSender;
1818

1919
use memchr::memchr_iter;
@@ -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>,

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

Lines changed: 12 additions & 12 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,7 +159,7 @@ pub fn ext_sort(
159159
}
160160

161161
/// Print a single sorted chunk.
162-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
162+
#[cfg(wasi_no_threads)]
163163
fn print_chunk(
164164
chunk: &Chunk,
165165
settings: &GlobalSettings,
@@ -180,7 +180,7 @@ fn print_chunk(
180180
}
181181

182182
/// Merge two in-memory chunks and print.
183-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
183+
#[cfg(wasi_no_threads)]
184184
fn print_two_chunks(
185185
a: Chunk,
186186
b: Chunk,
@@ -215,7 +215,7 @@ fn print_two_chunks(
215215
/// Two threads cooperate: one reads input and writes temporary chunk files,
216216
/// while the other sorts each chunk in memory. Once all chunks are written,
217217
/// they are merged back together for final output.
218-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
218+
#[cfg(not(wasi_no_threads))]
219219
pub fn ext_sort(
220220
files: &mut impl Iterator<Item = UResult<Box<dyn Read + Send>>>,
221221
settings: &GlobalSettings,
@@ -276,7 +276,7 @@ pub fn ext_sort(
276276
}
277277
}
278278

279-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
279+
#[cfg(not(wasi_no_threads))]
280280
fn reader_writer<
281281
F: Iterator<Item = UResult<Box<dyn Read + Send>>>,
282282
Tmp: WriteableTmpFile + 'static,
@@ -362,7 +362,7 @@ fn reader_writer<
362362
}
363363

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

379379
/// Describes how we read the chunks from the input.
380-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
380+
#[cfg(not(wasi_no_threads))]
381381
enum ReadResult<I: WriteableTmpFile> {
382382
/// The input was empty. Nothing was read.
383383
EmptyInput,
@@ -389,7 +389,7 @@ enum ReadResult<I: WriteableTmpFile> {
389389
WroteChunksToFile { tmp_files: Vec<I::Closed> },
390390
}
391391
/// The function that is executed on the reader/writer thread.
392-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
392+
#[cfg(not(wasi_no_threads))]
393393
fn read_write_loop<I: WriteableTmpFile>(
394394
mut files: impl Iterator<Item = UResult<Box<dyn Read + Send>>>,
395395
tmp_dir: &mut TmpDirWrapper,

src/uu/sort/src/merge.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ use std::{
2121
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
2222
rc::Rc,
2323
};
24-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
24+
#[cfg(not(wasi_no_threads))]
2525
use std::{
2626
sync::mpsc::{Receiver, Sender, SyncSender, channel, sync_channel},
2727
thread::{self, JoinHandle},
2828
};
2929

3030
use compare::Compare;
31-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
31+
#[cfg(not(wasi_no_threads))]
3232
use uucore::error::FromIo;
3333
use uucore::error::UResult;
3434

@@ -108,7 +108,7 @@ pub fn merge(
108108
let files = files
109109
.iter()
110110
.map(|file| open(file).map(|file| PlainMergeInput { inner: file }));
111-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
111+
#[cfg(wasi_no_threads)]
112112
if settings.compress_prog.is_some() {
113113
let _ = writeln!(
114114
std::io::stderr(),
@@ -132,9 +132,9 @@ fn do_merge_to_output<M: MergeInput + 'static>(
132132
settings: &GlobalSettings,
133133
output: Output,
134134
) -> UResult<()> {
135-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
135+
#[cfg(not(wasi_no_threads))]
136136
return merge_without_limit(files, settings)?.write_all(settings, output);
137-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
137+
#[cfg(wasi_no_threads)]
138138
return merge_without_limit_sync(files, settings)?.write_all(settings, output);
139139
}
140140

@@ -144,9 +144,9 @@ fn do_merge_to_writer<M: MergeInput + 'static>(
144144
settings: &GlobalSettings,
145145
out: &mut impl Write,
146146
) -> UResult<()> {
147-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
147+
#[cfg(not(wasi_no_threads))]
148148
return merge_without_limit(files, settings)?.write_all_to(settings, out);
149-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
149+
#[cfg(wasi_no_threads)]
150150
return merge_without_limit_sync(files, settings)?.write_all_to(settings, out);
151151
}
152152

@@ -208,7 +208,7 @@ pub fn merge_with_file_limit<
208208
///
209209
/// It is the responsibility of the caller to ensure that `files` yields only
210210
/// as many files as we are allowed to open concurrently.
211-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
211+
#[cfg(not(wasi_no_threads))]
212212
fn merge_without_limit<M: MergeInput + 'static, F: Iterator<Item = UResult<M>>>(
213213
files: F,
214214
settings: &GlobalSettings,
@@ -273,15 +273,15 @@ fn merge_without_limit<M: MergeInput + 'static, F: Iterator<Item = UResult<M>>>(
273273
})
274274
}
275275
/// The struct on the reader thread representing an input file
276-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
276+
#[cfg(not(wasi_no_threads))]
277277
struct ReaderFile<M: MergeInput> {
278278
file: M,
279279
sender: SyncSender<Chunk>,
280280
carry_over: Vec<u8>,
281281
}
282282

283283
/// The function running on the reader thread.
284-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
284+
#[cfg(not(wasi_no_threads))]
285285
fn reader(
286286
recycled_receiver: &Receiver<(usize, RecycledChunk)>,
287287
files: &mut [Option<ReaderFile<impl MergeInput>>],
@@ -316,7 +316,7 @@ fn reader(
316316
Ok(())
317317
}
318318
/// The struct on the main thread representing an input file
319-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
319+
#[cfg(not(wasi_no_threads))]
320320
pub struct MergeableFile {
321321
current_chunk: Rc<Chunk>,
322322
line_idx: usize,
@@ -330,20 +330,20 @@ pub struct MergeableFile {
330330
struct PreviousLine {
331331
chunk: Rc<Chunk>,
332332
line_idx: usize,
333-
#[cfg_attr(all(target_os = "wasi", not(target_feature = "atomics")), allow(dead_code))]
333+
#[cfg_attr(wasi_no_threads, allow(dead_code))]
334334
file_number: usize,
335335
}
336336

337337
/// Merges files together. This is **not** an iterator because of lifetime problems.
338-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
338+
#[cfg(not(wasi_no_threads))]
339339
struct FileMerger<'a> {
340340
heap: binary_heap_plus::BinaryHeap<MergeableFile, FileComparator<'a>>,
341341
request_sender: Sender<(usize, RecycledChunk)>,
342342
prev: Option<PreviousLine>,
343343
reader_join_handle: JoinHandle<UResult<()>>,
344344
}
345345

346-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
346+
#[cfg(not(wasi_no_threads))]
347347
impl FileMerger<'_> {
348348
/// Write the merged contents to the output file.
349349
fn write_all(self, settings: &GlobalSettings, output: Output) -> UResult<()> {
@@ -425,7 +425,7 @@ struct FileComparator<'a> {
425425
settings: &'a GlobalSettings,
426426
}
427427

428-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
428+
#[cfg(not(wasi_no_threads))]
429429
impl Compare<MergeableFile> for FileComparator<'_> {
430430
fn compare(&self, a: &MergeableFile, b: &MergeableFile) -> Ordering {
431431
let mut cmp = compare_by(
@@ -646,20 +646,20 @@ impl<R: Read + Send> MergeInput for PlainMergeInput<R> {
646646
// Synchronous merge for targets without thread support (e.g. wasm32-wasip1).
647647
// ---------------------------------------------------------------------------
648648

649-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
649+
#[cfg(wasi_no_threads)]
650650
struct SyncReaderFile<M: MergeInput> {
651651
file: M,
652652
carry_over: Vec<u8>,
653653
}
654654

655-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
655+
#[cfg(wasi_no_threads)]
656656
struct SyncMergeableFile {
657657
current_chunk: Rc<Chunk>,
658658
line_idx: usize,
659659
file_number: usize,
660660
}
661661

662-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
662+
#[cfg(wasi_no_threads)]
663663
impl Compare<SyncMergeableFile> for FileComparator<'_> {
664664
fn compare(&self, a: &SyncMergeableFile, b: &SyncMergeableFile) -> Ordering {
665665
let mut cmp = compare_by(
@@ -676,7 +676,7 @@ impl Compare<SyncMergeableFile> for FileComparator<'_> {
676676
}
677677
}
678678

679-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
679+
#[cfg(wasi_no_threads)]
680680
struct SyncFileMerger<'a, M: MergeInput> {
681681
heap: binary_heap_plus::BinaryHeap<SyncMergeableFile, FileComparator<'a>>,
682682
readers: Vec<Option<SyncReaderFile<M>>>,
@@ -685,7 +685,7 @@ struct SyncFileMerger<'a, M: MergeInput> {
685685
settings: &'a GlobalSettings,
686686
}
687687

688-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
688+
#[cfg(wasi_no_threads)]
689689
impl<M: MergeInput> SyncFileMerger<'_, M> {
690690
fn write_all(self, settings: &GlobalSettings, output: Output) -> UResult<()> {
691691
let mut out = output.into_write();
@@ -782,7 +782,7 @@ impl<M: MergeInput> SyncFileMerger<'_, M> {
782782
}
783783
}
784784

785-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
785+
#[cfg(wasi_no_threads)]
786786
fn merge_without_limit_sync<'a, M: MergeInput + 'static, F: Iterator<Item = UResult<M>>>(
787787
files: F,
788788
settings: &'a GlobalSettings,

src/uu/sort/src/sort.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use foldhash::fast::FoldHasher;
2929
use foldhash::{HashMap, SharedSeed};
3030
use numeric_str_cmp::{NumInfo, NumInfoParseSettings, human_numeric_str_cmp, numeric_str_cmp};
3131
use rand::{RngExt as _, rng};
32-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
32+
#[cfg(not(wasi_no_threads))]
3333
use rayon::slice::ParallelSliceMut;
3434
use std::cmp::Ordering;
3535
use std::env;
@@ -38,7 +38,7 @@ use std::fs::{File, OpenOptions};
3838
use std::hash::{Hash, Hasher};
3939
use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout};
4040
use std::num::IntErrorKind;
41-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
41+
#[cfg(not(wasi_no_threads))]
4242
use std::num::NonZero;
4343
use std::ops::Range;
4444
#[cfg(unix)]
@@ -2126,7 +2126,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
21262126
settings.threads = matches
21272127
.get_one::<String>(options::PARALLEL)
21282128
.map_or_else(|| "0".to_string(), String::from);
2129-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
2129+
#[cfg(not(wasi_no_threads))]
21302130
{
21312131
let num_threads = match settings.threads.parse::<usize>() {
21322132
Ok(0) | Err(_) => std::thread::available_parallelism().map_or(1, NonZero::get),
@@ -2620,14 +2620,14 @@ fn sort_by<'a>(unsorted: &mut Vec<Line<'a>>, settings: &GlobalSettings, line_dat
26202620
// WASI does not support threads, so use non-parallel sort to avoid
26212621
// rayon's thread pool which triggers an unreachable trap.
26222622
if settings.stable || settings.unique {
2623-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
2623+
#[cfg(not(wasi_no_threads))]
26242624
unsorted.par_sort_by(cmp);
2625-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
2625+
#[cfg(wasi_no_threads)]
26262626
unsorted.sort_by(cmp);
26272627
} else {
2628-
#[cfg(not(all(target_os = "wasi", not(target_feature = "atomics"))))]
2628+
#[cfg(not(wasi_no_threads))]
26292629
unsorted.par_sort_unstable_by(cmp);
2630-
#[cfg(all(target_os = "wasi", not(target_feature = "atomics")))]
2630+
#[cfg(wasi_no_threads)]
26312631
unsorted.sort_unstable_by(cmp);
26322632
}
26332633
}

0 commit comments

Comments
 (0)