Skip to content
Snippets Groups Projects
Commit 26f8e73d authored by Paul Heinzlreiter's avatar Paul Heinzlreiter
Browse files

* moved files to one diorectory

parent f6fdcbb8
Branches
No related merge requests found
Showing
with 169 additions and 0 deletions
File moved
File moved
File moved
File moved
File moved
File moved
#include "PthreadSimulation.hpp"
namespace nbody {
PthreadSimulation::PthreadSimulation() {
this->correctState = -1;
}
PthreadSimulation::~PthreadSimulation() {
}
void PthreadSimulation::cleanup() {
}
int PthreadSimulation::getNumberOfProcesses() {
return this->threads.size();
}
int PthreadSimulation::getProcessId() {
vector<pthread_t>::iterator it = this->threads.begin();
int id = 0;
while (*it != pthread_self() && it != this->threads.end()) {
id++;
it++;
}
if (it != this->threads.end()) {
return id;
} else {
return -1;
}
}
bool PthreadSimulation::readInputData(string filename) {
this->bodies = Tree::dubinskiParse(filename);
return !this->bodies.empty();
}
void PthreadSimulation::runStep() {
}
}
#ifndef PTHREAD_SIMULATION_HPP
#define PTHREAD_SIMULATION_HPP
#include "../datastructures/BarnesHutTree.hpp"
#include <vector>
#include <pthread.h>
namespace nbody {
class PthreadSimulation {
protected:
int correctState;
vector<Body> bodies;
BarnesHutTree tree;
vector<pthread_t> threads;
public:
PthreadSimulation();
virtual ~PthreadSimulation();
virtual void cleanup() = 0;
virtual int getNumberOfProcesses() = 0;
virtual int getProcessId() = 0;
virtual bool readInputData(string filename) = 0;
virtual void runStep() = 0;
};
}
#endif
#include "ReadWriteLock.hpp"
namespace nbody {
ReadWriteLock::ReadWriteLock() {
pthread_mutex_init(&this->mutex, NULL);
pthread_rwlock_init(&this->rwlock, NULL);
this->owner = NULL;
}
ReadWriteLock::~ReadWriteLock() {
pthread_mutex_destroy(&this->mutex);
pthread_rwlock_destroy(&this->rwlock);
}
bool ReadWriteLock::readLock(pthread_t* myThread) {
bool result;
pthread_mutex_lock(&this->mutex);
result = (pthread_rwlock_tryrdlock(&this->rwlock) == 0);
if (result) {
this->owner = myThread;
}
pthread_mutex_unlock(&this->mutex);
return result;
}
bool ReadWriteLock::writeLock(pthread_t* myThread) {
bool result;
pthread_mutex_lock(&this->mutex);
result = (pthread_rwlock_trywrlock(&this->rwlock) == 0);
if (result) {
this->owner = myThread;
}
pthread_mutex_unlock(&this->mutex);
return result;
}
bool ReadWriteLock::upgradeToWriteLock(pthread_t* myThread) {
bool result = false;
pthread_mutex_lock(&this->mutex);
if (this->owner == myThread) {
pthread_rwlock_unlock(&this->rwlock);
pthread_rwlock_wrlock(&this->rwlock);
result = true;
}
pthread_mutex_unlock(&this->mutex);
return result;
}
bool ReadWriteLock::downgradeToReadLock(pthread_t* myThread) {
bool result = false;
pthread_mutex_lock(&this->mutex);
if (this->owner == myThread) {
pthread_rwlock_unlock(&this->rwlock);
pthread_rwlock_rdlock(&this->rwlock);
result = true;
}
pthread_mutex_unlock(&this->mutex);
return result;
}
bool ReadWriteLock::unLock(pthread_t* myThread) {
bool result = false;
pthread_mutex_lock(&this->mutex);
if (this->owner == myThread) {
pthread_rwlock_unlock(&this->rwlock);
this->owner = NULL;
result = true;
}
pthread_mutex_unlock(&this->mutex);
return result;
}
}
#ifndef READ_WRITE_LOCK
#define READ_WRITE_LOCK
#include <pthread.h>
namespace nbody {
class ReadWriteLock {
protected:
pthread_mutex_t mutex;
pthread_rwlock_t rwlock;
pthread_t* owner;
public:
ReadWriteLock();
virtual ~ReadWriteLock();
virtual bool readLock(pthread_t* myThread);
virtual bool writeLock(pthread_t* myThread);
virtual bool upgradeToWriteLock(pthread_t* myThread);
virtual bool downgradeToReadLock(pthread_t* myThread);
virtual bool unLock(pthread_t* myThread);
};
}
#endif
File moved
File moved
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment