1 /* MtCoder.h -- Multi-thread Coder
2 2009-11-19 : Igor Pavlov : Public domain */
3 
4 #ifndef __MT_CODER_H
5 #define __MT_CODER_H
6 
7 #include "Threads.h"
8 
9 EXTERN_C_BEGIN
10 
11 typedef 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 
23 void LoopThread_Construct(CLoopThread *p);
24 void LoopThread_Close(CLoopThread *p);
25 WRes LoopThread_Create(CLoopThread *p);
26 WRes LoopThread_StopAndWait(CLoopThread *p);
27 WRes LoopThread_StartSubThread(CLoopThread *p);
28 WRes 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 
36 typedef 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 
47 SRes MtProgress_Set(CMtProgress *p, unsigned index, UInt64 inSize, UInt64 outSize);
48 
49 struct _CMtCoder;
50 
51 typedef 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 
67 typedef 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 
73 typedef 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 
92 void MtCoder_Construct(CMtCoder* p);
93 void MtCoder_Destruct(CMtCoder* p);
94 SRes MtCoder_Code(CMtCoder *p);
95 
96 EXTERN_C_END
97 
98 #endif
99