7zip/C/MtCoder.h

145 lines
2.7 KiB
C
Raw Normal View History

2021-12-27 01:00:00 +01:00
/* MtCoder.h -- Multi-thread Coder
2025-07-05 02:00:00 +02:00
: Igor Pavlov : Public domain */
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
#ifndef ZIP7_INC_MT_CODER_H
#define ZIP7_INC_MT_CODER_H
2021-12-27 01:00:00 +01:00
#include "MtDec.h"
EXTERN_C_BEGIN
/*
2023-06-21 02:00:00 +02:00
if ( defined MTCODER_USE_WRITE_THREAD) : main thread writes all data blocks to output stream
if (not defined MTCODER_USE_WRITE_THREAD) : any coder thread can write data blocks to output stream
2021-12-27 01:00:00 +01:00
*/
2023-06-21 02:00:00 +02:00
/* #define MTCODER_USE_WRITE_THREAD */
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
#ifndef Z7_ST
#define MTCODER_GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1)
2025-07-05 02:00:00 +02:00
#define MTCODER_THREADS_MAX 256
2023-06-21 02:00:00 +02:00
#define MTCODER_BLOCKS_MAX (MTCODER_GET_NUM_BLOCKS_FROM_THREADS(MTCODER_THREADS_MAX) + 3)
2021-12-27 01:00:00 +01:00
#else
2023-06-21 02:00:00 +02:00
#define MTCODER_THREADS_MAX 1
#define MTCODER_BLOCKS_MAX 1
2021-12-27 01:00:00 +01:00
#endif
2023-06-21 02:00:00 +02:00
#ifndef Z7_ST
2021-12-27 01:00:00 +01:00
typedef struct
{
ICompressProgress vt;
CMtProgress *mtProgress;
UInt64 inSize;
UInt64 outSize;
} CMtProgressThunk;
void MtProgressThunk_CreateVTable(CMtProgressThunk *p);
2023-06-21 02:00:00 +02:00
#define MtProgressThunk_INIT(p) { (p)->inSize = 0; (p)->outSize = 0; }
2021-12-27 01:00:00 +01:00
2023-06-21 02:00:00 +02:00
struct CMtCoder_;
2021-12-27 01:00:00 +01:00
typedef struct
{
2023-06-21 02:00:00 +02:00
struct CMtCoder_ *mtCoder;
2021-12-27 01:00:00 +01:00
unsigned index;
int stop;
Byte *inBuf;
CAutoResetEvent startEvent;
CThread thread;
} CMtCoderThread;
typedef struct
{
SRes (*Code)(void *p, unsigned coderIndex, unsigned outBufIndex,
const Byte *src, size_t srcSize, int finished);
SRes (*Write)(void *p, unsigned outBufIndex);
} IMtCoderCallback2;
typedef struct
{
SRes res;
unsigned bufIndex;
BoolInt finished;
} CMtCoderBlock;
2023-06-21 02:00:00 +02:00
typedef struct CMtCoder_
2021-12-27 01:00:00 +01:00
{
/* input variables */
size_t blockSize; /* size of input block */
unsigned numThreadsMax;
2025-07-05 02:00:00 +02:00
unsigned numThreadGroups;
2021-12-27 01:00:00 +01:00
UInt64 expectedDataSize;
2023-06-21 02:00:00 +02:00
ISeqInStreamPtr inStream;
2021-12-27 01:00:00 +01:00
const Byte *inData;
size_t inDataSize;
2023-06-21 02:00:00 +02:00
ICompressProgressPtr progress;
2021-12-27 01:00:00 +01:00
ISzAllocPtr allocBig;
IMtCoderCallback2 *mtCallback;
void *mtCallbackObject;
/* internal variables */
size_t allocatedBufsSize;
CAutoResetEvent readEvent;
CSemaphore blocksSemaphore;
BoolInt stopReading;
SRes readRes;
2023-06-21 02:00:00 +02:00
#ifdef MTCODER_USE_WRITE_THREAD
CAutoResetEvent writeEvents[MTCODER_BLOCKS_MAX];
2021-12-27 01:00:00 +01:00
#else
CAutoResetEvent finishedEvent;
SRes writeRes;
unsigned writeIndex;
2023-06-21 02:00:00 +02:00
Byte ReadyBlocks[MTCODER_BLOCKS_MAX];
2021-12-27 01:00:00 +01:00
LONG numFinishedThreads;
#endif
unsigned numStartedThreadsLimit;
unsigned numStartedThreads;
unsigned numBlocksMax;
unsigned blockIndex;
UInt64 readProcessed;
CCriticalSection cs;
unsigned freeBlockHead;
2023-06-21 02:00:00 +02:00
unsigned freeBlockList[MTCODER_BLOCKS_MAX];
2021-12-27 01:00:00 +01:00
CMtProgress mtProgress;
2023-06-21 02:00:00 +02:00
CMtCoderBlock blocks[MTCODER_BLOCKS_MAX];
CMtCoderThread threads[MTCODER_THREADS_MAX];
2025-07-05 02:00:00 +02:00
CThreadNextGroup nextGroup;
2021-12-27 01:00:00 +01:00
} CMtCoder;
void MtCoder_Construct(CMtCoder *p);
void MtCoder_Destruct(CMtCoder *p);
SRes MtCoder_Code(CMtCoder *p);
#endif
EXTERN_C_END
#endif