From 514b3ea2061c3253012cbb5bb0e7ee4330b89db4 Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 9 Jan 2016 19:15:41 +0000 Subject: [PATCH] CoWork::FinLock git-svn-id: svn://ultimatepp.org/upp/trunk@9386 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/CoWork.cpp | 16 ++++++++++++++-- uppsrc/Core/CoWork.h | 6 +++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/uppsrc/Core/CoWork.cpp b/uppsrc/Core/CoWork.cpp index d60a850ee..289082030 100644 --- a/uppsrc/Core/CoWork.cpp +++ b/uppsrc/Core/CoWork.cpp @@ -7,7 +7,7 @@ NAMESPACE_UPP #define LLOG(x) // DLOG(x) #define LDUMP(x) // DDUMP(x) -#define LHITCOUNT(x) // RHITCOUNT(x) +#define LHITCOUNT(x) RHITCOUNT(x) CoWork::Pool& CoWork::pool() { @@ -37,6 +37,14 @@ CoWork::Pool::~Pool() LLOG("Quit ended"); } +thread__ bool CoWork::Pool::finlock; + +void CoWork::FinLock() +{ + Pool::finlock = true; + pool().lock.Enter(); +} + bool CoWork::Pool::DoJob() { Pool& p = pool(); @@ -45,6 +53,7 @@ bool CoWork::Pool::DoJob() LLOG("Quit thread"); return true; } + finlock = false; #ifdef CPP_11 std::function fn = std::move(job.fn); if(fn) { @@ -60,7 +69,8 @@ bool CoWork::Pool::DoJob() p.lock.Leave(); cb(); } - p.lock.Enter(); + if(!finlock) + p.lock.Enter(); if(--job.work->todo == 0) { LLOG("Releasing waitforfinish of (CoWork " << FormatIntHex(job.work) << ")"); job.work->waitforfinish.Release(); @@ -114,6 +124,8 @@ void CoWork::Do(const Callback *cb, void *) else #endif (*cb)(); + if(Pool::finlock) + p.lock.Leave(); return; } MJob& job = p.jobs[p.scheduled++]; diff --git a/uppsrc/Core/CoWork.h b/uppsrc/Core/CoWork.h index 77b65d303..dfe062dbb 100644 --- a/uppsrc/Core/CoWork.h +++ b/uppsrc/Core/CoWork.h @@ -19,11 +19,13 @@ class CoWork : NoCopy { int waiting_threads; Array threads; - CriticalSection lock; + Mutex lock; Semaphore waitforjob; Pool(); ~Pool(); + + static thread__ bool finlock; static bool DoJob(); static void ThreadRun(int tno); @@ -53,6 +55,8 @@ public: CoWork& operator&(const std::function& lambda) { Do(NULL, &lambda); return *this; } #endif + static void FinLock(); + void Finish(); bool IsFinished();