We're using nachos 3.4 and 4.0, because we're looking the differences in the code and we are complementing nachos 3.4 with 4.0 code.
Nachos 4.0 already has a basic structure of semaphores and locks.
Nachos 4.0 already has a basic structure of semaphores and locks.
We had several problems compiling, and probably broke nachos many times but finally we accomplished our goal (with a little help of the code already complemented in the Nachos 4.0 and Max, Emmanuel and Adan's Blog
Here we show you the code we have in the Synch.cc part where we implemented locks
Lock::Lock(const char* debugName) {
//parte agregada para candado
semaphore = new Semaphore("lock", 1);
lockHolder = NULL;
}
//Destructor
Lock::~Lock() {
delete semaphore;
}
void Lock::Acquire() {
semaphore->P();
lockHolder = currentThread;
}
//checar esta parte que fue agregada
bool
Lock::IsHeldByCurrentThread(){
return lockHolder == currentThread;
}
void Lock::Release() {
ASSERT(IsHeldByCurrentThread());
lockHolder = NULL;
semaphore->V();
}
bool Lock::tryAcquire(){
if(lockHolder == NULL){
semaphore->P();
lockHolder = currentThread;
return true;
}
else{
return false;
}
}
Condition::Condition(const char* debugName, Lock* conditionLock) {
name = debugName;
waitQueue = new List <Semaphore *>;
}
Condition::~Condition() {
delete waitQueue;
//}
void Condition::Wait(Lock *conditionLock) {
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
waiter = new Semaphore("condition", 0);
waitQueue->Append(waiter);
conditionLock->Release();
waiter->P();
conditionLock->Acquire();
delete waiter;
}
void Condition::Signal(Lock *conditionLock) {
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
if(!waitQueue->IsEmpty()){
waiter = waitQueue->Remove();
waiter->V();
}
}
void Condition::Broadcast(Lock *conditionLock) {
while(!waitQueue->IsEmpty()){
Signal(conditionLock);
}
}
Now we show you the code we have in the Synch.h part, where we also implemented locks
//En esta parte es donde se agregan los candados y condition variable!!
//Constructor
Lock::Lock(const char* debugName) {
//parte agregada para candado
semaphore = new Semaphore("lock", 1);
lockHolder = NULL;
}
//Destructor
Lock::~Lock() {
delete semaphore;
}
void Lock::Acquire() {
semaphore->P();
lockHolder = currentThread;
}
//checar esta parte que fue agregada
bool
Lock::IsHeldByCurrentThread(){
return lockHolder == currentThread;
}
void Lock::Release() {
ASSERT(IsHeldByCurrentThread());
lockHolder = NULL;
semaphore->V();
}
bool Lock::tryAcquire(){
if(lockHolder == NULL){
semaphore->P();
lockHolder = currentThread;
return true;
}
else{
return false;
}
}
Condition::Condition(const char* debugName, Lock* conditionLock) {
name = debugName;
waitQueue = new List <Semaphore *>;
}
//Condition::~Condition() {
//delete waitQueue;
//}
void Condition::Wait(Lock *conditionLock) {
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
waiter = new Semaphore("condition", 0);
waitQueue->Append(waiter);
conditionLock->Release();
waiter->P();
conditionLock->Acquire();
delete waiter;
}
void Condition::Signal(Lock *conditionLock) {
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
if(!waitQueue->IsEmpty()){
waiter = waitQueue->Remove();
waiter->V();
}
}
void Condition::Broadcast(Lock *conditionLock) {
while(!waitQueue->IsEmpty()){
Signal(conditionLock);
}
}
We did a lot of modifications in the nachos code trying to implement the producer consumer problem but we broke it several times.
(Screenshots)
(Code)
(Video)
Now we show you the code we have in the Synch.h part, where we also implemented locks
//En esta parte es donde se agregan los candados y condition variable!!
//Constructor
Lock::Lock(const char* debugName) {
//parte agregada para candado
semaphore = new Semaphore("lock", 1);
lockHolder = NULL;
}
//Destructor
Lock::~Lock() {
delete semaphore;
}
void Lock::Acquire() {
semaphore->P();
lockHolder = currentThread;
}
//checar esta parte que fue agregada
bool
Lock::IsHeldByCurrentThread(){
return lockHolder == currentThread;
}
void Lock::Release() {
ASSERT(IsHeldByCurrentThread());
lockHolder = NULL;
semaphore->V();
}
bool Lock::tryAcquire(){
if(lockHolder == NULL){
semaphore->P();
lockHolder = currentThread;
return true;
}
else{
return false;
}
}
Condition::Condition(const char* debugName, Lock* conditionLock) {
name = debugName;
waitQueue = new List <Semaphore *>;
}
//Condition::~Condition() {
//delete waitQueue;
//}
void Condition::Wait(Lock *conditionLock) {
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
waiter = new Semaphore("condition", 0);
waitQueue->Append(waiter);
conditionLock->Release();
waiter->P();
conditionLock->Acquire();
delete waiter;
}
void Condition::Signal(Lock *conditionLock) {
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
if(!waitQueue->IsEmpty()){
waiter = waitQueue->Remove();
waiter->V();
}
}
void Condition::Broadcast(Lock *conditionLock) {
while(!waitQueue->IsEmpty()){
Signal(conditionLock);
}
}
We did a lot of modifications in the nachos code trying to implement the producer consumer problem but we broke it several times.
(Screenshots)
(Code)
(Video)