Try to correctly restart existing workers

上级 4df40e62
...@@ -38,7 +38,6 @@ private: ...@@ -38,7 +38,6 @@ private:
~uwq(); ~uwq();
uwq& operator=(const uwq&); uwq& operator=(const uwq&);
uwq(const uwq& x); uwq(const uwq& x);
proc* getworker();
proc* allocworker(); proc* allocworker();
void finishworkers(); void finishworkers();
void finish(); void finish();
......
...@@ -169,18 +169,52 @@ uwq::haswork(void) ...@@ -169,18 +169,52 @@ uwq::haswork(void)
bool bool
uwq::trywork(void) uwq::trywork(void)
{ {
struct proc* p; scoped_acquire lock0(&lock_);
p = getworker(); if (ref() == 0)
if (p == nullptr)
return false; return false;
int slot = -1;
for (int i = 0; i < NCPU; i++) {
if (worker_[i].running)
continue;
else if (worker_[i].proc != nullptr) {
scoped_acquire lock1(&worker_[i].lock);
proc* p = worker_[i].proc;
panic("uwq::trywork: untested");
acquire(&p->lock);
p->cpu_pin = 1;
p->cpuid = mycpuid(); p->cpuid = mycpuid();
release(&p->lock);
worker_[i].running = true;
cv_wakeup(&worker_[i].cv);
return true;
} else if (slot == -1) {
slot = i;
}
}
if (slot != -1) {
proc* p = allocworker();
if (p != nullptr) {
++uref_;
p->worker = &worker_[slot];
worker_[slot].proc = p;
worker_[slot].running = true;
acquire(&p->lock); acquire(&p->lock);
p->cpu_pin = 1;
p->cpuid = mycpuid();
addrun(p); addrun(p);
release(&p->lock); release(&p->lock);
return true; return true;
}
}
return nullptr;
} }
void void
...@@ -264,39 +298,3 @@ uwq::allocworker(void) ...@@ -264,39 +298,3 @@ uwq::allocworker(void)
return p; return p;
} }
proc*
uwq::getworker(void)
{
int slot = -1;
scoped_acquire lockx(&lock_);
if (ref() == 0)
return nullptr;
for (int i = 0; i < NCPU; i++) {
if (worker_[i].running)
continue;
if (worker_[i].proc != nullptr) {
panic("uwq::getworker: oops");
worker_[i].running = true;
return worker_[i].proc;
} else if (slot == -1) {
slot = i;
}
}
if (slot != -1) {
proc* p = allocworker();
if (p != nullptr) {
++uref_;
p->worker = &worker_[slot];
worker_[slot].proc = p;
worker_[slot].running = true;
return worker_[slot].proc;
}
}
return nullptr;
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论