Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MT.cpp « MT « LZ « Compress « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0ac456d8f90e8312cd57ce92f6a665e6f538d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// MT.cpp

#include "StdAfx.h"

#include "../../../../Common/Alloc.h"

#include "MT.h"

static const UInt32 kBlockSize = (1 << 14);

static DWORD WINAPI MFThread(void *threadCoderInfo)
{
  return ((CMatchFinderMT *)threadCoderInfo)->ThreadFunc();
}

CMatchFinderMT::CMatchFinderMT():
  m_Buffer(0),
  m_NeedStart(true)
{
  m_BlockIndex = kNumMTBlocks - 1;
  m_CS[m_BlockIndex].Enter();
  if (!m_Thread.Create(MFThread, this))
    throw 271826;
}

CMatchFinderMT::~CMatchFinderMT() 
{
  m_Exit = true;
  m_CS[m_BlockIndex].Leave();
  m_CanChangeBufferPos.Set();
  if (m_NeedStart)
    m_MtCanStart.Set();
  m_Thread.Wait();
  FreeMem();
}

void CMatchFinderMT::FreeMem()
{
  ::MyFree(m_Buffer);
  m_Buffer = 0;
}

STDMETHODIMP CMatchFinderMT::Create(UInt32 sizeHistory, UInt32 keepAddBufferBefore, 
    UInt32 matchMaxLen, UInt32 keepAddBufferAfter)
{ 
  FreeMem();
  m_MatchMaxLen = matchMaxLen;
  if (kBlockSize <= matchMaxLen * 4)
    return E_INVALIDARG;
  UInt32 bufferSize = kBlockSize * kNumMTBlocks;
  m_Buffer = (UInt32 *)::MyAlloc(bufferSize * sizeof(UInt32));
  if (m_Buffer == 0)
    return E_OUTOFMEMORY;
  keepAddBufferBefore += bufferSize;
  keepAddBufferAfter += (kBlockSize + 1);
  return m_MatchFinder->Create(sizeHistory, keepAddBufferBefore, matchMaxLen, keepAddBufferAfter); 
}

// UInt32 blockSizeMult = 800
HRESULT CMatchFinderMT::SetMatchFinder(IMatchFinder *matchFinder)
{
  m_MatchFinder = matchFinder;
  return S_OK;
}

STDMETHODIMP CMatchFinderMT::SetStream(ISequentialInStream *s)
{ 
  return m_MatchFinder->SetStream(s);
}

// Call it after ReleaseStream / SetStream
STDMETHODIMP CMatchFinderMT::Init()
{ 
  m_NeedStart = true;
  m_Pos = 0;
  m_PosLimit = 0;

  HRESULT result = m_MatchFinder->Init();
  if (result == S_OK)
    m_DataCurrentPos = m_MatchFinder->GetPointerToCurrentPos();
  m_NumAvailableBytes = m_MatchFinder->GetNumAvailableBytes();
  return result; 
}

// ReleaseStream is required to finish multithreading
STDMETHODIMP_(void) CMatchFinderMT::ReleaseStream()
{ 
  m_StopWriting = true;
  m_CS[m_BlockIndex].Leave();
  if (!m_NeedStart)
  {
    m_CanChangeBufferPos.Set();
    m_MtWasStopped.Lock();
    m_NeedStart = true;
  }
  m_MatchFinder->ReleaseStream();
  m_BlockIndex = kNumMTBlocks - 1;
  m_CS[m_BlockIndex].Enter();
}

STDMETHODIMP_(Byte) CMatchFinderMT::GetIndexByte(Int32 index)
{ 
  return m_DataCurrentPos[index]; 
}

STDMETHODIMP_(UInt32) CMatchFinderMT::GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
{ 
  if ((Int32)(index + limit) > m_NumAvailableBytes)
    limit = m_NumAvailableBytes - (index);
  distance++;
  const Byte *pby = m_DataCurrentPos + index;
  UInt32 i;
  for(i = 0; i < limit && pby[i] == pby[(size_t)i - distance]; i++);
  return i;
}

STDMETHODIMP_(const Byte *) CMatchFinderMT::GetPointerToCurrentPos()
{
  return m_DataCurrentPos;
}

STDMETHODIMP_(UInt32) CMatchFinderMT::GetNumAvailableBytes()
{ 
  return m_NumAvailableBytes;
}
  
void CMatchFinderMT::GetNextBlock()
{
  if (m_NeedStart)
  {
    m_NeedStart = false;
    for (UInt32 i = 0; i < kNumMTBlocks; i++)
      m_StopReading[i] = false;
    m_StopWriting = false;
    m_Exit = false;
    m_MtWasStarted.Reset();
    m_MtWasStopped.Reset();
    m_CanChangeBufferPos.Reset();
    m_BufferPosWasChanged.Reset();
    m_MtCanStart.Set();
    m_MtWasStarted.Lock();
    m_Result = S_OK;
  }
  for (;;)
  {
    UInt32 nextIndex = (m_BlockIndex == kNumMTBlocks - 1) ? 0 : m_BlockIndex + 1;
    m_CS[nextIndex].Enter();
    if (!m_StopReading[nextIndex])
    {
      m_CS[m_BlockIndex].Leave();
      m_BlockIndex = nextIndex;
      break;
    }
    m_StopReading[nextIndex] = false;
    m_CS[nextIndex].Leave();
    m_CanChangeBufferPos.Set();
    m_BufferPosWasChanged.Lock();
    m_CS[nextIndex].Enter();
    m_CS[m_BlockIndex].Leave();
    m_BlockIndex = nextIndex;
  }
  m_Pos = m_BlockIndex * kBlockSize;
  m_PosLimit = m_Buffer[m_Pos++];
  m_NumAvailableBytes = m_Buffer[m_Pos++];
  m_Result = m_Results[m_BlockIndex];
}

STDMETHODIMP CMatchFinderMT::GetMatches(UInt32 *distances)
{ 
  if (m_Pos == m_PosLimit)
    GetNextBlock();

  if (m_Result != S_OK)
    return m_Result;
  m_NumAvailableBytes--;
  m_DataCurrentPos++;

  const UInt32 *buffer = m_Buffer + m_Pos;
  UInt32 len = *buffer++;
  *distances++ = len;
  m_Pos += 1 + len;
  for (UInt32 i = 0; i != len; i += 2)
  {
    distances[i] = buffer[i];
    distances[i + 1] = buffer[i + 1];
  }
  return S_OK; 
}

STDMETHODIMP CMatchFinderMT::Skip(UInt32 num)
{ 
  do
  {
    if (m_Pos == m_PosLimit)
      GetNextBlock();
    
    if (m_Result != S_OK)
      return m_Result;
    m_NumAvailableBytes--;
    m_DataCurrentPos++;
    
    UInt32 len = m_Buffer[m_Pos++];
    m_Pos += len;
  }
  while(--num != 0);
  return S_OK; 
}

STDMETHODIMP_(Int32) CMatchFinderMT::NeedChangeBufferPos(UInt32 /* numCheckBytes */)
{
  throw 1;
}

STDMETHODIMP_(void) CMatchFinderMT::ChangeBufferPos()
{
  throw 1;
}


DWORD CMatchFinderMT::ThreadFunc()
{
  for (;;)
  {
    bool needStartEvent = true;
    m_MtCanStart.Lock();
    HRESULT result = S_OK;
    UInt32 blockIndex = 0;
    for (;;)
    {
      m_CS[blockIndex].Enter();
      if (needStartEvent)
      {
        m_MtWasStarted.Set();
        needStartEvent = false;
      }
      else
        m_CS[(blockIndex == 0) ? kNumMTBlocks - 1 : blockIndex - 1].Leave();
      if (m_Exit)
        return 0;
      if (m_StopWriting)
      {
        m_MtWasStopped.Set();
        m_CS[blockIndex].Leave();
        break;
      }
      if (result == S_OK)
      {
        IMatchFinder *mf = m_MatchFinder;
        if (mf->NeedChangeBufferPos(kBlockSize) != 0)
        {
          // m_AskChangeBufferPos.Set();
          m_StopReading[blockIndex] = true;
          m_CS[blockIndex].Leave();
          m_CanChangeBufferPos.Lock();
          m_CS[blockIndex].Enter();
          const Byte *bufferPosBefore = mf->GetPointerToCurrentPos();
          mf->ChangeBufferPos();
          m_DataCurrentPos += mf->GetPointerToCurrentPos() - bufferPosBefore;
          m_BufferPosWasChanged.Set();
        }
        else
        {
          UInt32 curPos = blockIndex * kBlockSize;
          UInt32 limit = curPos + kBlockSize - m_MatchMaxLen - m_MatchMaxLen - 1;
          UInt32 *buffer = m_Buffer;
          m_Results[blockIndex] = S_OK;
          curPos++;
          UInt32 numAvailableBytes = mf->GetNumAvailableBytes();
          buffer[curPos++] = numAvailableBytes;
          
          while (numAvailableBytes-- != 0 && curPos < limit)
          {
            result = mf->GetMatches(buffer + curPos);
            if (result != S_OK)
            {
              m_Results[blockIndex] = result;
              break;
            }
            curPos += buffer[curPos] + 1;
          }
          buffer[blockIndex * kBlockSize] = curPos;
        }
      }
      else
      {
        UInt32 curPos = blockIndex * kBlockSize;
        m_Buffer[curPos] = curPos + 2; // size of buffer
        m_Buffer[curPos + 1] = 0;      // NumAvailableBytes
        m_Results[blockIndex] = result; // error
      }
      if (++blockIndex == kNumMTBlocks)
        blockIndex = 0;
    }
  }
}