MtCoder.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* MtCoder.h -- Multi-thread Coder
  2. : Igor Pavlov : Public domain */
  3. #ifndef ZIP7_INC_MT_CODER_H
  4. #define ZIP7_INC_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 Z7_ST
  13. #define MTCODER_GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1)
  14. #define MTCODER_THREADS_MAX 256
  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 Z7_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. unsigned numThreadGroups;
  58. UInt64 expectedDataSize;
  59. ISeqInStreamPtr inStream;
  60. const Byte *inData;
  61. size_t inDataSize;
  62. ICompressProgressPtr progress;
  63. ISzAllocPtr allocBig;
  64. IMtCoderCallback2 *mtCallback;
  65. void *mtCallbackObject;
  66. /* internal variables */
  67. size_t allocatedBufsSize;
  68. CAutoResetEvent readEvent;
  69. CSemaphore blocksSemaphore;
  70. BoolInt stopReading;
  71. SRes readRes;
  72. #ifdef MTCODER_USE_WRITE_THREAD
  73. CAutoResetEvent writeEvents[MTCODER_BLOCKS_MAX];
  74. #else
  75. CAutoResetEvent finishedEvent;
  76. SRes writeRes;
  77. unsigned writeIndex;
  78. Byte ReadyBlocks[MTCODER_BLOCKS_MAX];
  79. LONG numFinishedThreads;
  80. #endif
  81. unsigned numStartedThreadsLimit;
  82. unsigned numStartedThreads;
  83. unsigned numBlocksMax;
  84. unsigned blockIndex;
  85. UInt64 readProcessed;
  86. CCriticalSection cs;
  87. unsigned freeBlockHead;
  88. unsigned freeBlockList[MTCODER_BLOCKS_MAX];
  89. CMtProgress mtProgress;
  90. CMtCoderBlock blocks[MTCODER_BLOCKS_MAX];
  91. CMtCoderThread threads[MTCODER_THREADS_MAX];
  92. CThreadNextGroup nextGroup;
  93. } CMtCoder;
  94. void MtCoder_Construct(CMtCoder *p);
  95. void MtCoder_Destruct(CMtCoder *p);
  96. SRes MtCoder_Code(CMtCoder *p);
  97. #endif
  98. EXTERN_C_END
  99. #endif