Commit e760de81 for xz
commit e760de811aaf0ddac117a8ef0faa82051b01fd03
Author: Lasse Collin <lasse.collin@tukaani.org>
Date: Sat Oct 17 18:37:39 2026 +0300
liblzma: mt enc: Fix threads_stop(coder, true)
threads_stop(coder, true) waited for all workers to be in the THR_IDLE
state. However, worker threads self-reset to THR_IDLE before they had
done their final cleanup steps. This could result in data corruption
or wrong progress indicator information if threaded encoder was
reinitialized when lzma_code() hadn't returned LZMA_STREAM_END.
Change threads_stop(coder, true) to wait until all initialized threads
are in the stack of free threads. (An alternative would have been to
set THR_IDLE when both thr->coder->mutex and thr->mutex are locked.)
Note that after LZMA_STREAM_END, the threads are already on the stack
of free threads.
Fixes: de678e0c924a ("liblzma: Add lzma_stream_encoder_mt() for threaded compression.")
diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c
index 67a93bd3..af6243ca 100644
--- a/src/liblzma/common/stream_encoder_mt.c
+++ b/src/liblzma/common/stream_encoder_mt.c
@@ -178,6 +178,9 @@ struct lzma_stream_coder_s {
/// are created only when actually needed.
worker_thread *threads_free;
+ /// Number of threads in threads_free.
+ uint32_t threads_free_count;
+
/// The most recent worker thread to which the main thread writes
/// the new input from the application.
worker_thread *thr;
@@ -385,13 +388,11 @@ worker_start(void *thr_ptr)
break;
// Mark the thread as idle unless the main thread has
- // told us to exit. Signal is needed for the case
- // where the main thread is waiting for the threads to stop.
+ // told us to exit. This is the only place where
+ // a worker thread modifies thr->state.
mythread_sync(thr->mutex) {
- if (thr->state != THR_EXIT) {
+ if (thr->state != THR_EXIT)
thr->state = THR_IDLE;
- mythread_cond_signal(&thr->cond);
- }
}
mythread_sync(thr->coder->mutex) {
@@ -412,6 +413,7 @@ worker_start(void *thr_ptr)
// Return this thread to the stack of free threads.
thr->next = thr->coder->threads_free;
thr->coder->threads_free = thr;
+ thr->coder->threads_free_count++;
mythread_cond_signal(&thr->coder->cond);
}
@@ -449,13 +451,10 @@ threads_stop(lzma_stream_coder *coder, bool wait_for_threads)
if (!wait_for_threads)
return;
- // Wait for the threads to settle in the idle state.
- for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
- mythread_sync(coder->threads[i].mutex) {
- while (coder->threads[i].state != THR_IDLE)
- mythread_cond_wait(&coder->threads[i].cond,
- &coder->threads[i].mutex);
- }
+ // Wait until all threads are in the stack of free threads.
+ mythread_sync(coder->mutex) {
+ while (coder->threads_free_count < coder->threads_initialized)
+ mythread_cond_wait(&coder->cond, &coder->mutex);
}
return;
@@ -555,6 +554,7 @@ get_thread(lzma_stream_coder *coder, const lzma_allocator *allocator)
if (coder->threads_free != NULL) {
coder->thr = coder->threads_free;
coder->threads_free = coder->threads_free->next;
+ coder->threads_free_count--;
}
}
@@ -1119,6 +1119,7 @@ stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
coder->threads_initialized = 0;
coder->threads_free = NULL;
+ coder->threads_free_count = 0;
coder->threads = lzma_alloc(
options->threads * sizeof(worker_thread),