1370b324cSopenharmony_ci// VirtThread.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "VirtThread.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_cistatic THREAD_FUNC_DECL CoderThread(void *p)
8370b324cSopenharmony_ci{
9370b324cSopenharmony_ci  for (;;)
10370b324cSopenharmony_ci  {
11370b324cSopenharmony_ci    CVirtThread *t = (CVirtThread *)p;
12370b324cSopenharmony_ci    t->StartEvent.Lock();
13370b324cSopenharmony_ci    if (t->Exit)
14370b324cSopenharmony_ci      return THREAD_FUNC_RET_ZERO;
15370b324cSopenharmony_ci    t->Execute();
16370b324cSopenharmony_ci    t->FinishedEvent.Set();
17370b324cSopenharmony_ci  }
18370b324cSopenharmony_ci}
19370b324cSopenharmony_ci
20370b324cSopenharmony_ciWRes CVirtThread::Create()
21370b324cSopenharmony_ci{
22370b324cSopenharmony_ci  RINOK_WRes(StartEvent.CreateIfNotCreated_Reset())
23370b324cSopenharmony_ci  RINOK_WRes(FinishedEvent.CreateIfNotCreated_Reset())
24370b324cSopenharmony_ci  // StartEvent.Reset();
25370b324cSopenharmony_ci  // FinishedEvent.Reset();
26370b324cSopenharmony_ci  Exit = false;
27370b324cSopenharmony_ci  if (Thread.IsCreated())
28370b324cSopenharmony_ci    return S_OK;
29370b324cSopenharmony_ci  return Thread.Create(CoderThread, this);
30370b324cSopenharmony_ci}
31370b324cSopenharmony_ci
32370b324cSopenharmony_ciWRes CVirtThread::Start()
33370b324cSopenharmony_ci{
34370b324cSopenharmony_ci  Exit = false;
35370b324cSopenharmony_ci  return StartEvent.Set();
36370b324cSopenharmony_ci}
37370b324cSopenharmony_ci
38370b324cSopenharmony_civoid CVirtThread::WaitThreadFinish()
39370b324cSopenharmony_ci{
40370b324cSopenharmony_ci  Exit = true;
41370b324cSopenharmony_ci  if (StartEvent.IsCreated())
42370b324cSopenharmony_ci    StartEvent.Set();
43370b324cSopenharmony_ci  if (Thread.IsCreated())
44370b324cSopenharmony_ci  {
45370b324cSopenharmony_ci    Thread.Wait_Close();
46370b324cSopenharmony_ci  }
47370b324cSopenharmony_ci}
48