MtCoder.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* MtCoder.h -- Multi-thread Coder
  2. 2018-07-04 : Igor Pavlov : Public domain */
  3. #ifndef __MT_CODER_H
  4. #define __MT_CODER_H
  5. #include "MtDec.h"
  6. EXTERN_C_BEGIN
  7. /*
  8. if ( defined MTCODER__USE_WRITE_THREAD) : main thread writes all data blocks to output stream
  9. if (not defined MTCODER__USE_WRITE_THREAD) : any coder thread can write data blocks to output stream
  10. */
  11. /* #define MTCODER__USE_WRITE_THREAD */
  12. #ifndef _7ZIP_ST
  13. #define MTCODER__GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1)
  14. #define MTCODER__THREADS_MAX 64
  15. #define MTCODER__BLOCKS_MAX (MTCODER__GET_NUM_BLOCKS_FROM_THREADS(MTCODER__THREADS_MAX) + 3)
  16. #else
  17. #define MTCODER__THREADS_MAX 1
  18. #define MTCODER__BLOCKS_MAX 1
  19. #endif
  20. #ifndef _7ZIP_ST
  21. typedef struct
  22. {
  23. ICompressProgress vt;
  24. CMtProgress *mtProgress;
  25. UInt64 inSize;
  26. UInt64 outSize;
  27. } CMtProgressThunk;
  28. void MtProgressThunk_CreateVTable(CMtProgressThunk *p);
  29. #define MtProgressThunk_Init(p) { (p)->inSize = 0; (p)->outSize = 0; }
  30. struct _CMtCoder;
  31. typedef struct
  32. {
  33. struct _CMtCoder *mtCoder;
  34. unsigned index;
  35. int stop;
  36. Byte *inBuf;
  37. CAutoResetEvent startEvent;
  38. CThread thread;
  39. } CMtCoderThread;
  40. typedef struct
  41. {
  42. SRes (*Code)(void *p, unsigned coderIndex, unsigned outBufIndex,
  43. const Byte *src, size_t srcSize, int finished);
  44. SRes (*Write)(void *p, unsigned outBufIndex);
  45. } IMtCoderCallback2;
  46. typedef struct
  47. {
  48. SRes res;
  49. unsigned bufIndex;
  50. BoolInt finished;
  51. } CMtCoderBlock;
  52. typedef struct _CMtCoder
  53. {
  54. /* input variables */
  55. size_t blockSize; /* size of input block */
  56. unsigned numThreadsMax;
  57. UInt64 expectedDataSize;
  58. ISeqInStream *inStream;
  59. const Byte *inData;
  60. size_t inDataSize;
  61. ICompressProgress *progress;
  62. ISzAllocPtr allocBig;
  63. IMtCoderCallback2 *mtCallback;
  64. void *mtCallbackObject;
  65. /* internal variables */
  66. size_t allocatedBufsSize;
  67. CAutoResetEvent readEvent;
  68. CSemaphore blocksSemaphore;
  69. BoolInt stopReading;
  70. SRes readRes;
  71. #ifdef MTCODER__USE_WRITE_THREAD
  72. CAutoResetEvent writeEvents[MTCODER__BLOCKS_MAX];
  73. #else
  74. CAutoResetEvent finishedEvent;
  75. SRes writeRes;
  76. unsigned writeIndex;
  77. Byte ReadyBlocks[MTCODER__BLOCKS_MAX];
  78. LONG numFinishedThreads;
  79. #endif
  80. unsigned numStartedThreadsLimit;
  81. unsigned numStartedThreads;
  82. unsigned numBlocksMax;
  83. unsigned blockIndex;
  84. UInt64 readProcessed;
  85. CCriticalSection cs;
  86. unsigned freeBlockHead;
  87. unsigned freeBlockList[MTCODER__BLOCKS_MAX];
  88. CMtProgress mtProgress;
  89. CMtCoderBlock blocks[MTCODER__BLOCKS_MAX];
  90. CMtCoderThread threads[MTCODER__THREADS_MAX];
  91. } CMtCoder;
  92. void MtCoder_Construct(CMtCoder *p);
  93. void MtCoder_Destruct(CMtCoder *p);
  94. SRes MtCoder_Code(CMtCoder *p);
  95. #endif
  96. EXTERN_C_END
  97. #endif