diff src/win32/7zip/7z/CPP/7zip/Common/VirtThread.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Common/VirtThread.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,45 @@
     1.4 +// VirtThread.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "VirtThread.h"
     1.9 +
    1.10 +static THREAD_FUNC_DECL CoderThread(void *p)
    1.11 +{
    1.12 +  for (;;)
    1.13 +  {
    1.14 +    CVirtThread *t = (CVirtThread *)p;
    1.15 +    t->StartEvent.Lock();
    1.16 +    if (t->ExitEvent)
    1.17 +      return 0;
    1.18 +    t->Execute();
    1.19 +    t->FinishedEvent.Set();
    1.20 +  }
    1.21 +}
    1.22 +
    1.23 +WRes CVirtThread::Create()
    1.24 +{
    1.25 +  RINOK(StartEvent.CreateIfNotCreated());
    1.26 +  RINOK(FinishedEvent.CreateIfNotCreated());
    1.27 +  StartEvent.Reset();
    1.28 +  FinishedEvent.Reset();
    1.29 +  ExitEvent = false;
    1.30 +  if (Thread.IsCreated())
    1.31 +    return S_OK;
    1.32 +  return Thread.Create(CoderThread, this);
    1.33 +}
    1.34 +
    1.35 +void CVirtThread::Start()
    1.36 +{
    1.37 +  ExitEvent = false;
    1.38 +  StartEvent.Set();
    1.39 +}
    1.40 +
    1.41 +CVirtThread::~CVirtThread()
    1.42 +{
    1.43 +  ExitEvent = true;
    1.44 +  if (StartEvent.IsCreated())
    1.45 +    StartEvent.Set();
    1.46 +  Thread.Wait();
    1.47 +}
    1.48 +