Start of a thread pool for uwq

上级 352865e7
...@@ -10,11 +10,23 @@ struct uwq { ...@@ -10,11 +10,23 @@ struct uwq {
uwq(padded_length *len); uwq(padded_length *len);
~uwq(); ~uwq();
bool haswork(); bool haswork();
int trywork();
void* buffer(); void* buffer();
private: private:
uwq& operator=(const uwq&);
uwq(const uwq& x); uwq(const uwq& x);
proc* getworker();
struct spinlock lock_;
padded_length* len_; padded_length* len_;
struct worker {
int running;
proc *proc;
};
worker worker_[NCPU];
}; };
int uwq_trywork(void); int uwq_trywork(void);
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
#include "amd64.h" #include "amd64.h"
#include "kernel.hh" #include "kernel.hh"
#include "cpu.hh" #include "cpu.hh"
#include "uwq.hh"
#include "gc.hh" #include "gc.hh"
#include "percpu.hh" #include "percpu.hh"
#include "spinlock.h" #include "spinlock.h"
#include "condvar.h" #include "condvar.h"
#include "uwq.hh"
#include "proc.hh" #include "proc.hh"
#include "vm.hh" #include "vm.hh"
#include "kalloc.hh" #include "kalloc.hh"
...@@ -25,13 +25,15 @@ uwq_trywork(void) ...@@ -25,13 +25,15 @@ uwq_trywork(void)
continue; continue;
struct cpu *c = &cpus[j]; struct cpu *c = &cpus[j];
scoped_gc_epoch(); scoped_gc_epoch gcx();
barrier();
struct proc *p = c->proc; struct proc *p = c->proc;
if (p == nullptr || p->vmap == nullptr) if (p == nullptr || p->vmap == nullptr)
continue; continue;
uwq* uwq = &p->vmap->uwq_; uwq* uwq = &p->vmap->uwq_;
if (uwq->haswork()) { if (uwq->haswork()) {
uwq->trywork();
// XXX(sbw) start a worker thread.. // XXX(sbw) start a worker thread..
break; break;
} }
...@@ -52,6 +54,9 @@ uwq::uwq(padded_length *len) ...@@ -52,6 +54,9 @@ uwq::uwq(padded_length *len)
} else { } else {
cprintf("uwq::uwq: nullptr len\n"); cprintf("uwq::uwq: nullptr len\n");
} }
initlock(&lock_, "uwq_lock", 0);
memset(worker_, 0, sizeof(worker_));
} }
uwq::~uwq(void) uwq::~uwq(void)
...@@ -74,8 +79,44 @@ uwq::haswork(void) ...@@ -74,8 +79,44 @@ uwq::haswork(void)
return false; return false;
} }
int
uwq::trywork(void)
{
struct proc* p;
p = getworker();
if (p == nullptr)
return -1;
return 0;
}
void* void*
uwq::buffer(void) uwq::buffer(void)
{ {
return (void*)len_; return (void*)len_;
} }
proc*
uwq::getworker(void)
{
int slot = -1;
scoped_acquire lockx(&lock_);
for (int i = 0; i < NCPU; i++) {
if (worker_[i].proc != nullptr) {
worker_[i].running = 1;
return worker_[i].proc;
} else if (slot == -1) {
slot = i;
}
}
if (slot != -1) {
panic("XXX");
}
return nullptr;
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论