1/* MtCoder.h -- Multi-thread Coder
22009-11-19 : Igor Pavlov : Public domain */
3
4#ifndef __MT_CODER_H
5#define __MT_CODER_H
6
7#include "Threads.h"
8
9EXTERN_C_BEGIN
10
11typedef struct
12{
13  CThread thread;
14  CAutoResetEvent startEvent;
15  CAutoResetEvent finishedEvent;
16  int stop;
17
18  THREAD_FUNC_TYPE func;
19  LPVOID param;
20  THREAD_FUNC_RET_TYPE res;
21} CLoopThread;
22
23void LoopThread_Construct(CLoopThread *p);
24void LoopThread_Close(CLoopThread *p);
25WRes LoopThread_Create(CLoopThread *p);
26WRes LoopThread_StopAndWait(CLoopThread *p);
27WRes LoopThread_StartSubThread(CLoopThread *p);
28WRes LoopThread_WaitSubThread(CLoopThread *p);
29
30#ifndef _7ZIP_ST
31#define NUM_MT_CODER_THREADS_MAX 32
32#else
33#define NUM_MT_CODER_THREADS_MAX 1
34#endif
35
36typedef struct
37{
38  UInt64 totalInSize;
39  UInt64 totalOutSize;
40  ICompressProgress *progress;
41  SRes res;
42  CCriticalSection cs;
43  UInt64 inSizes[NUM_MT_CODER_THREADS_MAX];
44  UInt64 outSizes[NUM_MT_CODER_THREADS_MAX];
45} CMtProgress;
46
47SRes MtProgress_Set(CMtProgress *p, unsigned index, UInt64 inSize, UInt64 outSize);
48
49struct _CMtCoder;
50
51typedef struct
52{
53  struct _CMtCoder *mtCoder;
54  Byte *outBuf;
55  size_t outBufSize;
56  Byte *inBuf;
57  size_t inBufSize;
58  unsigned index;
59  CLoopThread thread;
60
61  Bool stopReading;
62  Bool stopWriting;
63  CAutoResetEvent canRead;
64  CAutoResetEvent canWrite;
65} CMtThread;
66
67typedef struct
68{
69  SRes (*Code)(void *p, unsigned index, Byte *dest, size_t *destSize,
70      const Byte *src, size_t srcSize, int finished);
71} IMtCoderCallback;
72
73typedef struct _CMtCoder
74{
75  size_t blockSize;
76  size_t destBlockSize;
77  unsigned numThreads;
78
79  ISeqInStream *inStream;
80  ISeqOutStream *outStream;
81  ICompressProgress *progress;
82  ISzAlloc *alloc;
83
84  IMtCoderCallback *mtCallback;
85  CCriticalSection cs;
86  SRes res;
87
88  CMtProgress mtProgress;
89  CMtThread threads[NUM_MT_CODER_THREADS_MAX];
90} CMtCoder;
91
92void MtCoder_Construct(CMtCoder* p);
93void MtCoder_Destruct(CMtCoder* p);
94SRes MtCoder_Code(CMtCoder *p);
95
96EXTERN_C_END
97
98#endif
99