1 /* MtCoder.h -- Multi-thread Coder
2 2018-02-21 : Igor Pavlov : Public domain */
12 if ( defined MTCODER__USE_WRITE_THREAD) : main thread writes all data blocks to output stream
13 if (not defined MTCODER__USE_WRITE_THREAD) : any coder thread can write data blocks to output stream
15 /* #define MTCODER__USE_WRITE_THREAD */
18 #define MTCODER__GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1)
19 #define MTCODER__THREADS_MAX 64
20 #define MTCODER__BLOCKS_MAX (MTCODER__GET_NUM_BLOCKS_FROM_THREADS(MTCODER__THREADS_MAX) + 3)
22 #define MTCODER__THREADS_MAX 1
23 #define MTCODER__BLOCKS_MAX 1
33 CMtProgress
*mtProgress
;
38 void MtProgressThunk_CreateVTable(CMtProgressThunk
*p
);
40 #define MtProgressThunk_Init(p) { (p)->inSize = 0; (p)->outSize = 0; }
48 struct _CMtCoder
*mtCoder
;
53 CAutoResetEvent startEvent
;
60 SRes (*Code
)(void *p
, unsigned coderIndex
, unsigned outBufIndex
,
61 const Byte
*src
, size_t srcSize
, int finished
);
62 SRes (*Write
)(void *p
, unsigned outBufIndex
);
74 typedef struct _CMtCoder
78 size_t blockSize
; /* size of input block */
79 unsigned numThreadsMax
;
80 UInt64 expectedDataSize
;
82 ISeqInStream
*inStream
;
86 ICompressProgress
*progress
;
89 IMtCoderCallback2
*mtCallback
;
90 void *mtCallbackObject
;
93 /* internal variables */
95 size_t allocatedBufsSize
;
97 CAutoResetEvent readEvent
;
98 CSemaphore blocksSemaphore
;
103 #ifdef MTCODER__USE_WRITE_THREAD
104 CAutoResetEvent writeEvents
[MTCODER__BLOCKS_MAX
];
106 CAutoResetEvent finishedEvent
;
109 Byte ReadyBlocks
[MTCODER__BLOCKS_MAX
];
110 LONG numFinishedThreads
;
113 unsigned numStartedThreadsLimit
;
114 unsigned numStartedThreads
;
116 unsigned numBlocksMax
;
118 UInt64 readProcessed
;
122 unsigned freeBlockHead
;
123 unsigned freeBlockList
[MTCODER__BLOCKS_MAX
];
125 CMtProgress mtProgress
;
126 CMtCoderBlock blocks
[MTCODER__BLOCKS_MAX
];
127 CMtCoderThread threads
[MTCODER__THREADS_MAX
];
131 void MtCoder_Construct(CMtCoder
*p
);
132 void MtCoder_Destruct(CMtCoder
*p
);
133 SRes
MtCoder_Code(CMtCoder
*p
);