simplify mappedMonitor() and eliminate System::ReadWriteLock

This commit is contained in:
Joel Dice 2007-07-01 22:03:44 -06:00
parent bb16d8e62b
commit db529925e2
2 changed files with 13 additions and 150 deletions

View File

@ -27,92 +27,6 @@ class System {
virtual void dispose() = 0;
};
class ReadWriteLock {
public:
ReadWriteLock(System* s): s(s), m(0), readers(0), writer(0) {
if (not s->success(s->make(&m))) {
s->abort();
}
}
~ReadWriteLock() {
if (readers or writer) {
s->abort();
}
m->dispose();
}
bool tryAcquireRead(void* context) {
bool result;
m->acquire(context);
if (writer) {
result = false;
} else {
result = true;
++ readers;
}
m->release(context);
return result;
}
void acquireRead(void* context) {
m->acquire(context);
while (writer) {
m->wait(context, 0);
}
++ readers;
m->release(context);
}
void releaseRead(void* context) {
m->acquire(context);
if (-- readers == 0) {
m->notify(context);
}
m->release(context);
}
bool tryAcquireWrite(void* context) {
bool result;
m->acquire(context);
if (readers or writer) {
result = false;
} else {
result = true;
writer = context;
}
m->release(context);
return result;
}
void acquireWrite(void* context) {
m->acquire(context);
while (readers or writer) {
m->wait(context, 0);
}
writer = context;
m->release(context);
}
void releaseWrite(void* context) {
if (writer != context) {
s->abort();
}
m->acquire(context);
writer = 0;
m->notifyAll(context);
m->release(context);
}
private:
System* s;
Monitor* m;
unsigned readers;
void* writer;
};
class Library {
public:
virtual ~Library() { }

View File

@ -14,10 +14,6 @@
#define ACQUIRE_RAW(t, x) RawMonitorResource MAKE_NAME(monitorResource_) (t, x)
#define ACQUIRE_READ(t, x) ReadResource MAKE_NAME(readResource_) (t, x)
#define ACQUIRE_WRITE(t, x) WriteResource MAKE_NAME(writeResource_) (t, x)
#define ENTER(t, state) StateResource MAKE_NAME(stateResource_) (t, state)
using namespace vm;
@ -85,7 +81,7 @@ class Machine {
System::Monitor* stateLock;
System::Monitor* heapLock;
System::Monitor* classLock;
System::ReadWriteLock monitorMapLock;
System::Monitor* monitorMapLock;
System::Library* libraries;
object classMap;
object bootstrapClassMap;
@ -195,38 +191,6 @@ class StateResource {
Thread::State oldState;
};
class ReadResource {
public:
ReadResource(Thread* t, System::ReadWriteLock& lock): t(t), lock(lock) {
if (not lock.tryAcquireRead(t)) {
ENTER(t, Thread::IdleState);
lock.acquireRead(t);
}
}
~ReadResource() { lock.releaseRead(t); }
private:
Thread* t;
System::ReadWriteLock& lock;
};
class WriteResource {
public:
WriteResource(Thread* t, System::ReadWriteLock& lock): t(t), lock(lock) {
if (not lock.tryAcquireWrite(t)) {
ENTER(t, Thread::IdleState);
lock.acquireWrite(t);
}
}
~WriteResource() { lock.releaseWrite(t); }
private:
Thread* t;
System::ReadWriteLock& lock;
};
class MonitorResource {
public:
MonitorResource(Thread* t, System::Monitor* m): t(t), m(m) {
@ -304,12 +268,6 @@ baseSize(Thread* t, object o, object class_)
BytesPerWord);
}
inline unsigned
baseSize(Thread* t, object o)
{
return baseSize(t, o, objectClass(t, o));
}
unsigned
extendedSize(Thread* t, object o, unsigned baseSize)
{
@ -325,12 +283,6 @@ extendedSize(Thread* t, object o, unsigned baseSize)
return n;
}
inline unsigned
extendedSize(Thread* t, object o)
{
return extendedSize(t, o, baseSize(t, o));
}
inline bool
monitorAttached(Thread* t, object o, unsigned baseSize)
{
@ -368,7 +320,7 @@ inline uint32_t
objectHash(Thread* t, object o)
{
if (objectExtended(t, o)) {
return extendedWord(t, o, baseSize(t, o)) & PointerMask;
return extendedWord(t, o, baseSize(t, o, objectClass(o))) & PointerMask;
} else {
markHashTaken(t, o);
return takeHash(t, o);
@ -966,24 +918,20 @@ mappedMonitor(Thread* t, object o)
{
PROTECT(t, o);
System::Monitor* m = 0;
{ ACQUIRE_READ(t, t->vm->monitorMapLock);
object p = hashMapFind(t, t->vm->monitorMap, o, objectHash, objectEqual);
if (p) {
m = static_cast<System::Monitor*>(pointerValue(t, p));
}
}
ACQUIRE(t, t->vm->monitorMapLock);
if (m == 0) {
object p = hashMapFind(t, t->vm->monitorMap, o, objectHash, objectEqual);
if (p) {
return static_cast<System::Monitor*>(pointerValue(t, p));
} else {
System::Status s = t->vm->system->make(&m);
expect(t, t->vm->system->success(s));
object pointer = makePointer(t, m);
PROTECT(t, pointer);
ACQUIRE_WRITE(t, t->vm->monitorMapLock);
hashMapInsert(t, t->vm->monitorMap, o, pointer, objectHash);
return m;
}
}
@ -2654,7 +2602,7 @@ Machine::Machine(System* system, Heap* heap, ClassFinder* classFinder):
stateLock(0),
heapLock(0),
classLock(0),
monitorMapLock(system),
monitorMapLock(0),
libraries(0),
classMap(0),
bootstrapClassMap(0),
@ -2671,7 +2619,8 @@ Machine::Machine(System* system, Heap* heap, ClassFinder* classFinder):
if (not system->success(system->make(&stateLock)) or
not system->success(system->make(&heapLock)) or
not system->success(system->make(&classLock)))
not system->success(system->make(&classLock)) or
not system->success(system->make(&monitorMapLock)))
{
system->abort();
}