summaryrefslogtreecommitdiffstats
path: root/CPP/7zip/Common/VirtThread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CPP/7zip/Common/VirtThread.cpp')
-rwxr-xr-xCPP/7zip/Common/VirtThread.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/CPP/7zip/Common/VirtThread.cpp b/CPP/7zip/Common/VirtThread.cpp
new file mode 100755
index 0000000..c07caba
--- /dev/null
+++ b/CPP/7zip/Common/VirtThread.cpp
@@ -0,0 +1,46 @@
+// VirtThread.cpp
+
+#include "StdAfx.h"
+
+#include "VirtThread.h"
+
+static THREAD_FUNC_DECL CoderThread(void *p)
+{
+ for (;;)
+ {
+ CVirtThread *t = (CVirtThread *)p;
+ t->StartEvent.Lock();
+ if (t->ExitEvent)
+ return 0;
+ t->Execute();
+ t->FinishedEvent.Set();
+ }
+}
+
+WRes CVirtThread::Create()
+{
+ RINOK(StartEvent.CreateIfNotCreated());
+ RINOK(FinishedEvent.CreateIfNotCreated());
+ StartEvent.Reset();
+ FinishedEvent.Reset();
+ ExitEvent = false;
+ if (Thread.IsCreated())
+ return S_OK;
+ return Thread.Create(CoderThread, this);
+}
+
+void CVirtThread::Start()
+{
+ ExitEvent = false;
+ StartEvent.Set();
+}
+
+CVirtThread::~CVirtThread()
+{
+ ExitEvent = true;
+ if (StartEvent.IsCreated())
+ StartEvent.Set();
+ if (Thread.IsCreated())
+ Thread.Wait();
+}
+