Lines Matching refs:coder

79 	/// the main mutex (coder->mutex).
83 /// mutex (coder->mutex) when updating outbuf->pos and (2) when
85 struct lzma_stream_coder *coder;
105 /// (coder->mutex). Since the main thread will only read output
239 /// (coder->block_decoder). In threaded mode this is 0.
355 thr->coder->mem_in_use -= thr->in_size;
358 thr->coder->mem_in_use -= thr->mem_filters;
359 thr->coder->mem_cached += thr->mem_filters;
362 thr->next = thr->coder->threads_free;
363 thr->coder->threads_free = thr;
365 mythread_cond_signal(&thr->coder->cond);
404 mythread_sync(thr->coder->mutex) {
441 thr->block_decoder.coder, thr->allocator,
464 mythread_sync(thr->coder->mutex) {
467 mythread_cond_signal(&thr->coder->cond);
476 // case requires updating thr->coder->thread_error.
485 // it later to update thr->coder->mem_in_use.
494 mythread_sync(thr->coder->mutex) {
496 thr->coder->progress_in += thr->in_pos;
497 thr->coder->progress_out += thr->out_pos;
510 && thr->coder->thread_error == LZMA_OK)
511 thr->coder->thread_error = ret;
522 threads_end(struct lzma_stream_coder *coder, const lzma_allocator *allocator)
524 for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
525 mythread_sync(coder->threads[i].mutex) {
526 coder->threads[i].state = THR_EXIT;
527 mythread_cond_signal(&coder->threads[i].cond);
531 for (uint32_t i = 0; i < coder->threads_initialized; ++i)
532 mythread_join(coder->threads[i].thread_id);
534 lzma_free(coder->threads, allocator);
535 coder->threads_initialized = 0;
536 coder->threads = NULL;
537 coder->threads_free = NULL;
540 coder->mem_in_use = 0;
541 coder->mem_cached = 0;
548 threads_stop(struct lzma_stream_coder *coder)
550 for (uint32_t i = 0; i < coder->threads_initialized; ++i) {
551 mythread_sync(coder->threads[i].mutex) {
554 if (coder->threads[i].state != THR_IDLE) {
555 coder->threads[i].state = THR_STOP;
556 mythread_cond_signal(&coder->threads[i].cond);
567 initialize_new_thread(struct lzma_stream_coder *coder,
570 // Allocate the coder->threads array if needed. It's done here instead
572 // use the direct mode (we may even free coder->threads in the middle
574 if (coder->threads == NULL) {
575 coder->threads = lzma_alloc(
576 coder->threads_max * sizeof(struct worker_thread),
579 if (coder->threads == NULL)
584 assert(coder->threads_initialized < coder->threads_max);
586 = &coder->threads[coder->threads_initialized];
598 thr->coder = coder;
606 ++coder->threads_initialized;
607 coder->thr = thr;
623 get_thread(struct lzma_stream_coder *coder, const lzma_allocator *allocator)
626 mythread_sync(coder->mutex) {
627 if (coder->threads_free != NULL) {
628 coder->thr = coder->threads_free;
629 coder->threads_free = coder->threads_free->next;
636 coder->mem_cached -= coder->thr->mem_filters;
640 if (coder->thr == NULL) {
641 assert(coder->threads_initialized < coder->threads_max);
644 return_if_error(initialize_new_thread(coder, allocator));
647 coder->thr->in_filled = 0;
648 coder->thr->in_pos = 0;
649 coder->thr->out_pos = 0;
651 coder->thr->progress_in = 0;
652 coder->thr->progress_out = 0;
654 coder->thr->partial_update = PARTIAL_DISABLED;
661 read_output_and_wait(struct lzma_stream_coder *coder,
671 mythread_sync(coder->mutex) {
677 ret = lzma_outq_read(&coder->outq, allocator,
687 &coder->outq,
709 coder->out_was_filled = true;
712 if (coder->thread_error != LZMA_OK) {
715 if (coder->fail_fast) {
716 ret = coder->thread_error;
735 coder->pending_error = LZMA_PROG_ERROR;
752 && coder->memlimit_threading
753 - coder->mem_in_use
754 - coder->outq.mem_in_use
755 >= coder->mem_next_block
756 && lzma_outq_has_buf(&coder->outq)
757 && (coder->threads_initialized
758 < coder->threads_max
759 || coder->threads_free
773 if (lzma_outq_is_empty(&coder->outq)) {
793 if (lzma_outq_is_readable(&coder->outq)) {
828 // coder->mutex which we are already holding.
829 if (coder->thr != NULL && coder->thr->partial_update
832 assert(coder->thr->outbuf == coder->outq.head);
833 assert(coder->thr->outbuf == coder->outq.tail);
835 if (coder->thr->outbuf->decoder_in_pos
836 == coder->thr->in_filled)
841 if (coder->timeout != 0) {
852 &coder->cond,
853 coder->timeout);
856 if (mythread_cond_timedwait(&coder->cond,
857 &coder->mutex,
863 mythread_cond_wait(&coder->cond,
864 &coder->mutex);
873 threads_stop(coder);
880 decode_block_header(struct lzma_stream_coder *coder,
887 if (coder->pos == 0) {
895 coder->block_options.header_size
901 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
902 coder->block_options.header_size);
905 if (coder->pos < coder->block_options.header_size)
908 coder->pos = 0;
911 coder->block_options.version = 1;
915 coder->block_options.filters = coder->filters;
918 return_if_error(lzma_block_header_decode(&coder->block_options,
919 allocator, coder->buffer));
924 coder->block_options.ignore_check = coder->ignore_check;
926 // coder->block_options is ready now.
933 comp_blk_size(const struct lzma_stream_coder *coder)
935 return vli_ceil4(coder->block_options.compressed_size)
936 + lzma_check_size(coder->stream_flags.check);
952 stream_decoder_reset(struct lzma_stream_coder *coder,
956 coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
957 if (coder->index_hash == NULL)
961 coder->sequence = SEQ_STREAM_HEADER;
962 coder->pos = 0;
975 struct lzma_stream_coder *coder = coder_ptr;
1029 || (*in_pos == in_size && !coder->out_was_filled);
1030 coder->out_was_filled = false;
1033 switch (coder->sequence) {
1037 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
1039 coder->progress_in += *in_pos - in_old;
1042 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
1045 coder->pos = 0;
1049 &coder->stream_flags, coder->buffer);
1051 return ret == LZMA_FORMAT_ERROR && !coder->first_stream
1057 coder->first_stream = false;
1061 coder->block_options.check = coder->stream_flags.check;
1065 coder->sequence = SEQ_BLOCK_HEADER;
1069 if (coder->tell_no_check && coder->stream_flags.check
1073 if (coder->tell_unsupported_check
1075 coder->stream_flags.check))
1078 if (coder->tell_any_check)
1086 const lzma_ret ret = decode_block_header(coder, allocator,
1088 coder->progress_in += *in_pos - in_old;
1125 // (coder->timeout is completely ignored).
1159 if (action == LZMA_FINISH && coder->fail_fast) {
1163 threads_stop(coder);
1169 return_if_error(read_output_and_wait(coder, allocator,
1174 if (coder->pending_error != LZMA_OK) {
1175 coder->sequence = SEQ_ERROR;
1183 coder->sequence = SEQ_INDEX_WAIT_OUTPUT;
1200 coder->pending_error = ret;
1201 coder->sequence = SEQ_ERROR;
1206 coder->mem_next_filters = lzma_raw_decoder_memusage(
1207 coder->filters);
1209 if (coder->mem_next_filters == UINT64_MAX) {
1211 coder->pending_error = LZMA_OPTIONS_ERROR;
1212 coder->sequence = SEQ_ERROR;
1216 coder->sequence = SEQ_BLOCK_INIT;
1228 if (coder->mem_next_filters > coder->memlimit_stop) {
1233 return_if_error(read_output_and_wait(coder, allocator,
1237 if (!lzma_outq_is_empty(&coder->outq))
1248 if (is_direct_mode_needed(coder->block_options.compressed_size)
1250 coder->block_options.uncompressed_size)) {
1251 coder->sequence = SEQ_BLOCK_DIRECT_INIT;
1260 coder->mem_next_in = comp_blk_size(coder);
1261 const uint64_t mem_buffers = coder->mem_next_in
1263 coder->block_options.uncompressed_size);
1267 if (UINT64_MAX - mem_buffers < coder->mem_next_filters) {
1272 coder->sequence = SEQ_BLOCK_DIRECT_INIT;
1278 coder->mem_next_block = coder->mem_next_filters + mem_buffers;
1282 if (coder->mem_next_block > coder->memlimit_threading) {
1283 coder->sequence = SEQ_BLOCK_DIRECT_INIT;
1289 lzma_next_end(&coder->block_decoder, allocator);
1290 coder->mem_direct_mode = 0;
1295 const lzma_ret ret = lzma_index_hash_append(coder->index_hash,
1297 &coder->block_options),
1298 coder->block_options.uncompressed_size);
1300 coder->pending_error = ret;
1301 coder->sequence = SEQ_ERROR;
1305 coder->sequence = SEQ_BLOCK_THR_INIT;
1329 return_if_error(read_output_and_wait(coder, allocator,
1334 if (coder->pending_error != LZMA_OK) {
1335 coder->sequence = SEQ_ERROR;
1346 assert(!lzma_outq_is_empty(&coder->outq));
1366 mythread_sync(coder->mutex) {
1367 mem_in_use = coder->mem_in_use;
1368 mem_cached = coder->mem_cached;
1369 thr = coder->threads_free;
1375 const uint64_t mem_max = coder->memlimit_threading
1376 - coder->mem_next_block;
1385 if (mem_in_use + mem_cached + coder->outq.mem_allocated
1391 lzma_outq_clear_cache2(&coder->outq, allocator,
1392 coder->block_options.uncompressed_size);
1408 + coder->outq.mem_in_use > mem_max) {
1415 if (thr->mem_filters <= coder->mem_next_filters)
1426 // Update the memory usage counters. Note that coder->mem_*
1429 mythread_sync(coder->mutex) {
1430 coder->mem_cached -= mem_freed;
1436 // NOTE: After this, coder->mem_in_use +
1437 // coder->mem_cached might count the same thing twice.
1439 // a worker_thread is picked from coder->free_threads
1441 coder->mem_in_use += coder->mem_next_in
1442 + coder->mem_next_filters;
1447 &coder->outq, allocator,
1448 coder->block_options.uncompressed_size);
1450 threads_stop(coder);
1454 // Set up coder->thr.
1455 ret = get_thread(coder, allocator);
1457 threads_stop(coder);
1462 // coder->mem_in_use. Store it in the thread too.
1463 coder->thr->mem_filters = coder->mem_next_filters;
1466 coder->thr->block_options = coder->block_options;
1468 &coder->thr->block_decoder, allocator,
1469 &coder->thr->block_options);
1473 lzma_filters_free(coder->filters, allocator);
1474 coder->thr->block_options.filters = NULL;
1479 coder->pending_error = ret;
1480 coder->sequence = SEQ_ERROR;
1485 coder->thr->in_size = coder->mem_next_in;
1486 coder->thr->in = lzma_alloc(coder->thr->in_size, allocator);
1487 if (coder->thr->in == NULL) {
1488 threads_stop(coder);
1493 coder->thr->outbuf = lzma_outq_get_buf(
1494 &coder->outq, coder->thr);
1497 mythread_sync(coder->thr->mutex) {
1498 assert(coder->thr->state == THR_IDLE);
1499 coder->thr->state = THR_RUN;
1500 mythread_cond_signal(&coder->thr->cond);
1505 mythread_sync(coder->mutex) {
1506 lzma_outq_enable_partial_output(&coder->outq,
1510 coder->sequence = SEQ_BLOCK_THR_RUN;
1516 if (action == LZMA_FINISH && coder->fail_fast) {
1523 const size_t in_needed = coder->thr->in_size
1524 - coder->thr->in_filled;
1526 threads_stop(coder);
1532 size_t cur_in_filled = coder->thr->in_filled;
1533 lzma_bufcpy(in, in_pos, in_size, coder->thr->in,
1534 &cur_in_filled, coder->thr->in_size);
1537 mythread_sync(coder->thr->mutex) {
1538 coder->thr->in_filled = cur_in_filled;
1546 mythread_cond_signal(&coder->thr->cond);
1553 return_if_error(read_output_and_wait(coder, allocator,
1558 if (coder->pending_error != LZMA_OK) {
1559 coder->sequence = SEQ_ERROR;
1564 if (coder->thr->in_filled < coder->thr->in_size) {
1571 coder->thr = NULL;
1572 coder->sequence = SEQ_BLOCK_HEADER;
1581 // NOTE: No need to check for coder->pending_error as
1585 return_if_error(read_output_and_wait(coder, allocator,
1588 if (!lzma_outq_is_empty(&coder->outq))
1592 lzma_outq_clear_cache(&coder->outq, allocator);
1594 // Get rid of the worker threads, including the coder->threads
1596 threads_end(coder, allocator);
1600 &coder->block_decoder, allocator,
1601 &coder->block_options);
1605 lzma_filters_free(coder->filters, allocator);
1606 coder->block_options.filters = NULL;
1613 coder->mem_direct_mode = coder->mem_next_filters;
1615 coder->sequence = SEQ_BLOCK_DIRECT_RUN;
1623 const lzma_ret ret = coder->block_decoder.code(
1624 coder->block_decoder.coder, allocator,
1627 coder->progress_in += *in_pos - in_old;
1628 coder->progress_out += *out_pos - out_old;
1635 return_if_error(lzma_index_hash_append(coder->index_hash,
1637 &coder->block_options),
1638 coder->block_options.uncompressed_size));
1640 coder->sequence = SEQ_BLOCK_HEADER;
1647 return_if_error(read_output_and_wait(coder, allocator,
1651 if (!lzma_outq_is_empty(&coder->outq))
1654 coder->sequence = SEQ_INDEX_DECODE;
1668 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
1670 coder->progress_in += *in_pos - in_old;
1674 coder->sequence = SEQ_STREAM_FOOTER;
1682 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
1684 coder->progress_in += *in_pos - in_old;
1687 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
1690 coder->pos = 0;
1697 &footer_flags, coder->buffer);
1704 if (lzma_index_hash_size(coder->index_hash)
1711 &coder->stream_flags, &footer_flags));
1713 if (!coder->concatenated)
1716 coder->sequence = SEQ_STREAM_PADDING;
1722 assert(coder->concatenated);
1734 return coder->pos == 0
1745 ++coder->progress_in;
1746 coder->pos = (coder->pos + 1) & 3;
1751 if (coder->pos != 0) {
1753 ++coder->progress_in;
1758 return_if_error(stream_decoder_reset(coder, allocator));
1762 if (!coder->fail_fast) {
1771 return_if_error(read_output_and_wait(coder, allocator,
1777 if (!lzma_outq_is_empty(&coder->outq))
1784 return coder->pending_error;
1798 struct lzma_stream_coder *coder = coder_ptr;
1800 threads_end(coder, allocator);
1801 lzma_outq_end(&coder->outq, allocator);
1803 lzma_next_end(&coder->block_decoder, allocator);
1804 lzma_filters_free(coder->filters, allocator);
1805 lzma_index_hash_end(coder->index_hash, allocator);
1807 lzma_free(coder, allocator);
1815 const struct lzma_stream_coder *coder = coder_ptr;
1816 return coder->stream_flags.check;
1840 struct lzma_stream_coder *coder = coder_ptr;
1842 mythread_sync(coder->mutex) {
1843 *memusage = coder->mem_direct_mode
1844 + coder->mem_in_use
1845 + coder->mem_cached
1846 + coder->outq.mem_allocated;
1854 *old_memlimit = coder->memlimit_stop;
1860 coder->memlimit_stop = new_memlimit;
1871 struct lzma_stream_coder *coder = coder_ptr;
1873 // Lock coder->mutex to prevent finishing threads from moving their
1875 mythread_sync(coder->mutex) {
1876 *progress_in = coder->progress_in;
1877 *progress_out = coder->progress_out;
1879 for (size_t i = 0; i < coder->threads_initialized; ++i) {
1880 mythread_sync(coder->threads[i].mutex) {
1881 *progress_in += coder->threads[i].progress_in;
1882 *progress_out += coder->threads[i]
1896 struct lzma_stream_coder *coder;
1906 coder = next->coder;
1907 if (!coder) {
1908 coder = lzma_alloc(sizeof(struct lzma_stream_coder), allocator);
1909 if (coder == NULL)
1912 next->coder = coder;
1914 if (mythread_mutex_init(&coder->mutex)) {
1915 lzma_free(coder, allocator);
1919 if (mythread_cond_init(&coder->cond)) {
1920 mythread_mutex_destroy(&coder->mutex);
1921 lzma_free(coder, allocator);
1931 coder->filters[0].id = LZMA_VLI_UNKNOWN;
1932 memzero(&coder->outq, sizeof(coder->outq));
1934 coder->block_decoder = LZMA_NEXT_CODER_INIT;
1935 coder->mem_direct_mode = 0;
1937 coder->index_hash = NULL;
1938 coder->threads = NULL;
1939 coder->threads_free = NULL;
1940 coder->threads_initialized = 0;
1945 lzma_filters_free(coder->filters, allocator);
1960 threads_end(coder, allocator);
1968 coder->mem_in_use = 0;
1969 coder->mem_cached = 0;
1970 coder->mem_next_block = 0;
1972 coder->progress_in = 0;
1973 coder->progress_out = 0;
1975 coder->sequence = SEQ_STREAM_HEADER;
1976 coder->thread_error = LZMA_OK;
1977 coder->pending_error = LZMA_OK;
1978 coder->thr = NULL;
1980 coder->timeout = options->timeout;
1982 coder->memlimit_threading = my_max(1, options->memlimit_threading);
1983 coder->memlimit_stop = my_max(1, options->memlimit_stop);
1984 if (coder->memlimit_threading > coder->memlimit_stop)
1985 coder->memlimit_threading = coder->memlimit_stop;
1987 coder->tell_no_check = (options->flags & LZMA_TELL_NO_CHECK) != 0;
1988 coder->tell_unsupported_check
1990 coder->tell_any_check = (options->flags & LZMA_TELL_ANY_CHECK) != 0;
1991 coder->ignore_check = (options->flags & LZMA_IGNORE_CHECK) != 0;
1992 coder->concatenated = (options->flags & LZMA_CONCATENATED) != 0;
1993 coder->fail_fast = (options->flags & LZMA_FAIL_FAST) != 0;
1995 coder->first_stream = true;
1996 coder->out_was_filled = false;
1997 coder->pos = 0;
1999 coder->threads_max = options->threads;
2001 return_if_error(lzma_outq_init(&coder->outq, allocator,
2002 coder->threads_max));
2004 return stream_decoder_reset(coder, allocator);