mirror of
https://github.com/tildearrow/furnace.git
synced 2024-11-23 13:05:11 +00:00
workPool: improvements
This commit is contained in:
parent
d42b503e81
commit
eb18b28928
1 changed files with 13 additions and 8 deletions
|
@ -64,10 +64,8 @@ bool DivWorkThread::assign(const std::function<void(void*)>& what, void* arg) {
|
||||||
}
|
}
|
||||||
tasks.push(DivPendingTask(what,arg));
|
tasks.push(DivPendingTask(what,arg));
|
||||||
parent->busyCount++;
|
parent->busyCount++;
|
||||||
parent->notify.notify_one();
|
|
||||||
isBusy=true;
|
isBusy=true;
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
notify.notify_one();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,21 +91,19 @@ void DivWorkThread::init(DivWorkPool* p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DivWorkPool::push(const std::function<void(void*)>& what, void* arg) {
|
void DivWorkPool::push(const std::function<void(void*)>& what, void* arg) {
|
||||||
//logV("submitting work");
|
|
||||||
// if no work threads, just execute
|
// if no work threads, just execute
|
||||||
if (!threaded) {
|
if (!threaded) {
|
||||||
what(arg);
|
what(arg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos>=count) pos=0;
|
|
||||||
|
|
||||||
for (unsigned int tryCount=0; tryCount<count; tryCount++) {
|
for (unsigned int tryCount=0; tryCount<count; tryCount++) {
|
||||||
|
if (pos>=count) pos=0;
|
||||||
if (workThreads[pos++].assign(what,arg)) return;
|
if (workThreads[pos++].assign(what,arg)) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// all threads are busy
|
// all threads are busy
|
||||||
logV("all busy");
|
logW("DivWorkPool: all work threads busy!");
|
||||||
what(arg);
|
what(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,8 +118,17 @@ bool DivWorkPool::busy() {
|
||||||
void DivWorkPool::wait() {
|
void DivWorkPool::wait() {
|
||||||
if (!threaded) return;
|
if (!threaded) return;
|
||||||
std::unique_lock<std::mutex> unique(selfLock);
|
std::unique_lock<std::mutex> unique(selfLock);
|
||||||
|
|
||||||
|
// start running
|
||||||
|
for (unsigned int i=0; i<count; i++) {
|
||||||
|
workThreads[i].notify.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait
|
||||||
while (busyCount!=0) {
|
while (busyCount!=0) {
|
||||||
notify.wait_for(unique,std::chrono::milliseconds(100));
|
if (notify.wait_for(unique,std::chrono::milliseconds(100))==std::cv_status::timeout) {
|
||||||
|
logW("DivWorkPool: wait() timed out!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,4 +154,4 @@ DivWorkPool::~DivWorkPool() {
|
||||||
}
|
}
|
||||||
delete[] workThreads;
|
delete[] workThreads;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue