MtCoder.h 1.9 KB

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