]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
xz: Add support --threads=+N so that -T+1 gives threaded mode.
authorLasse Collin <lasse.collin@tukaani.org>
Sat, 19 Nov 2022 17:06:13 +0000 (19:06 +0200)
committerLasse Collin <lasse.collin@tukaani.org>
Sat, 19 Nov 2022 17:06:13 +0000 (19:06 +0200)
src/xz/args.c
src/xz/hardware.c
src/xz/hardware.h
src/xz/xz.1

index 639c811a9c47a4c346d1eb275938a3e8a75e60da..8953aa59cefc3504ed76f4375946847d44478da2 100644 (file)
@@ -258,11 +258,23 @@ parse_real(args_info *args, int argc, char **argv)
                        suffix_set(optarg);
                        break;
 
-               case 'T':
+               case 'T': {
+                       // Since xz 5.4.0: Ignore leading '+' first.
+                       const char *s = optarg;
+                       if (optarg[0] == '+')
+                               ++s;
+
                        // The max is from src/liblzma/common/common.h.
-                       hardware_threads_set(str_to_uint64("threads",
-                                       optarg, 0, 16384));
+                       uint32_t t = str_to_uint64("threads", s, 0, 16384);
+
+                       // If leading '+' was used then use multi-threaded
+                       // mode even if exactly one thread was specified.
+                       if (t == 1 && optarg[0] == '+')
+                               t = UINT32_MAX;
+
+                       hardware_threads_set(t);
                        break;
+               }
 
                // --version
                case 'V':
index 1fdecea1dc8936d2767e347b54e6d4595bf955d3..97f77f80f43a559a6db6ed6bfb8dac7d1e08cd73 100644 (file)
@@ -21,6 +21,10 @@ static uint32_t threads_max = 1;
 /// on the available hardware threads.
 static bool threads_are_automatic = false;
 
+/// If true, then try to use multi-threaded mode (if memlimit allows)
+/// even if only one thread was requested explicitly (-T+1).
+static bool use_mt_mode_with_one_thread = false;
+
 /// Memory usage limit for compression
 static uint64_t memlimit_compress = 0;
 
@@ -57,9 +61,16 @@ static uint64_t total_ram;
 extern void
 hardware_threads_set(uint32_t n)
 {
+       // Reset these to false first and set them to true when appropriate.
+       threads_are_automatic = false;
+       use_mt_mode_with_one_thread = false;
+
        if (n == 0) {
                // Automatic number of threads was requested.
+               // If there is only one hardware thread, multi-threaded
+               // mode will still be used if memory limit allows.
                threads_are_automatic = true;
+               use_mt_mode_with_one_thread = true;
 
                // If threading support was enabled at build time,
                // use the number of available CPU cores. Otherwise
@@ -72,9 +83,11 @@ hardware_threads_set(uint32_t n)
 #else
                threads_max = 1;
 #endif
+       } else if (n == UINT32_MAX) {
+               use_mt_mode_with_one_thread = true;
+               threads_max = 1;
        } else {
                threads_max = n;
-               threads_are_automatic = false;
        }
 
        return;
@@ -92,7 +105,7 @@ extern bool
 hardware_threads_is_mt(void)
 {
 #ifdef MYTHREAD_ENABLED
-       return threads_max > 1 || threads_are_automatic;
+       return threads_max > 1 || use_mt_mode_with_one_thread;
 #else
        return false;
 #endif
index 2cd6aa235ee76a4bfe9fdc38c3775ba88aaf6086..2bb3d7ba949343eb3084ba84b4e940f76d733b9f 100644 (file)
@@ -16,6 +16,7 @@ extern void hardware_init(void);
 
 
 /// Set the maximum number of worker threads.
+/// A special value of UINT32_MAX sets one thread in multi-threaded mode.
 extern void hardware_threads_set(uint32_t threadlimit);
 
 /// Get the maximum number of worker threads.
index 62bab50728f95fcda06f4e300d33057c1845a052..1ff110e8a29da72d7e2a78075b7b715a051efc34 100644 (file)
@@ -5,7 +5,7 @@
 .\" This file has been put into the public domain.
 .\" You can do whatever you want with this file.
 .\"
-.TH XZ 1 "2022-11-09" "Tukaani" "XZ Utils"
+.TH XZ 1 "2022-11-19" "Tukaani" "XZ Utils"
 .
 .SH NAME
 xz, unxz, xzcat, lzma, unlzma, lzcat \- Compress or decompress .xz and .lzma files
@@ -1214,6 +1214,25 @@ even if the system supports only one hardware thread.
 5.2.x
 used single-threaded mode in this situation.)
 .IP ""
+To use multi-threaded mode with only one thread, set
+.I threads
+to
+.BR +1 .
+The
+.B +
+prefix has no effect with values other than
+.BR 1 .
+A memory usage limit can still make
+.B xz
+switch to single-threaded mode unless
+.B \-\-no\-adjust
+is used.
+Support for the
+.B +
+prefix was added in
+.B xz
+5.4.0.
+.IP ""
 If an automatic number of threads has been requested and
 no memory usage limit has been specified,
 then a system-specific default soft limit will be used to possibly