1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// VirtThread.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "VirtThread.h"
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic THREAD_FUNC_DECL CoderThread(void *p)
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (;;)
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    CVirtThread *t = (CVirtThread *)p;
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    t->StartEvent.Lock();
13cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    if (t->Exit)
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return 0;
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    t->Execute();
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    t->FinishedEvent.Set();
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
20baa3858d3f5d128a5c8466b700098109edcad5f2repo syncWRes CVirtThread::Create()
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RINOK(StartEvent.CreateIfNotCreated());
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  RINOK(FinishedEvent.CreateIfNotCreated());
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  StartEvent.Reset();
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  FinishedEvent.Reset();
26cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  Exit = false;
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (Thread.IsCreated())
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return S_OK;
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Thread.Create(CoderThread, this);
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
32baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CVirtThread::Start()
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
34cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  Exit = false;
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  StartEvent.Set();
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
38cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbeckyvoid CVirtThread::WaitThreadFinish()
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
40cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  Exit = true;
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (StartEvent.IsCreated())
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    StartEvent.Set();
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (Thread.IsCreated())
44cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  {
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Thread.Wait();
46cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky    Thread.Close();
47cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky  }
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
49