提交 fed708d4 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

scoped_acquire

上级 006c474e
...@@ -34,6 +34,15 @@ mkpair(const A &a, const B &b) ...@@ -34,6 +34,15 @@ mkpair(const A &a, const B &b)
return pair<A, B>(a, b); return pair<A, B>(a, b);
} }
class scoped_acquire {
private:
spinlock *_l;
public:
scoped_acquire(spinlock *l) : _l(l) { acquire(_l); }
~scoped_acquire() { release(_l); }
};
/* C++ runtime */ /* C++ runtime */
void *operator new(unsigned long nbytes); void *operator new(unsigned long nbytes);
void *operator new(unsigned long nbytes, void *buf); void *operator new(unsigned long nbytes, void *buf);
......
...@@ -15,6 +15,7 @@ extern "C" { ...@@ -15,6 +15,7 @@ extern "C" {
#include "vm.hh" #include "vm.hh"
#include "gc.hh" #include "gc.hh"
#include "crange.hh" #include "crange.hh"
#include "cpputil.hh"
enum { vm_debug = 0 }; enum { vm_debug = 0 };
...@@ -356,27 +357,23 @@ vmap::lookup(uptr start, uptr len) ...@@ -356,27 +357,23 @@ vmap::lookup(uptr start, uptr len)
int int
vmap::insert(vmnode *n, uptr va_start) vmap::insert(vmnode *n, uptr va_start)
{ {
acquire(&lock); scoped_acquire sa(&lock);
u64 len = n->npages * PGSIZE; u64 len = n->npages * PGSIZE;
if (lookup(va_start, len)) { if (lookup(va_start, len)) {
cprintf("vmap_insert: overlap\n"); cprintf("vmap_insert: overlap\n");
release(&lock);
return -1; return -1;
} }
vma *e = new vma(); vma *e = new vma();
if (e == 0) { if (e == 0)
release(&lock);
return -1; return -1;
}
e->va_start = va_start; e->va_start = va_start;
e->va_end = va_start + len; e->va_end = va_start + len;
e->n = n; e->n = n;
n->ref++; n->ref++;
cr.add(e->va_start, len, (void *) e); cr.add(e->va_start, len, (void *) e);
release(&lock);
return 0; return 0;
} }
...@@ -387,7 +384,7 @@ vmap_copy(struct vmap *m, int share) ...@@ -387,7 +384,7 @@ vmap_copy(struct vmap *m, int share)
if(nm == 0) if(nm == 0)
return 0; return 0;
acquire(&m->lock); scoped_acquire sa(&m->lock);
for (range *r: m->cr) { for (range *r: m->cr) {
struct vma *e = (struct vma *) r->value; struct vma *e = (struct vma *) r->value;
struct vma *ne = new vma(); struct vma *ne = new vma();
...@@ -418,12 +415,10 @@ vmap_copy(struct vmap *m, int share) ...@@ -418,12 +415,10 @@ vmap_copy(struct vmap *m, int share)
if (share) if (share)
lcr3(v2p(m->pml4)); // Reload hardware page table lcr3(v2p(m->pml4)); // Reload hardware page table
release(&m->lock);
return nm; return nm;
err: err:
delete nm; delete nm;
release(&m->lock);
return 0; return 0;
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论