From bd468abba7b586474830baac2eb3bb58e08cd22d Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Wed, 30 Dec 2020 16:18:49 +0100 Subject: [PATCH] add fine_list with implemented add_function --- Assignment_2/FineList.cpp | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Assignment_2/FineList.cpp diff --git a/Assignment_2/FineList.cpp b/Assignment_2/FineList.cpp new file mode 100644 index 0000000..e3bb480 --- /dev/null +++ b/Assignment_2/FineList.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include + +// TODO: implement fine grained locking + +class Node { +public: + int data; + Node* next; + std::mutex mut; + public: + void lock(); + void unlock(); +}; +void Node::lock() { + this->mut.lock(); +} +void Node::unlock() { + this->mut.unlock(); +} + +class Set { + Node* first; + public: + Set(); + bool add(int element); + bool rmv(int element); + bool ctn(int element); +}; + +Set::Set(){ + first = new Node(); // dummy node; + Node* last = new Node(); + last->data = INT_MAX; // end node; + first->next = last; +} + +bool Set::add(int element) { + this->first->lock(); + Node* p = this->first; + Node* c = p->next; + c->lock(); + while (c->data < element) { + p->unlock(); + p = c; + c = c->next; + c->lock(); + + if (c->data == element) { + c->unlock(); + p->unlock(); + return false; + } + } + + Node* n = new Node(); + n->data = element; + n->next = c; + p->next = n; + + c->unlock(); + p->unlock(); + return true; + } + +bool Set::rmv(int element) { + return false; +} +bool Set::ctn(int element) { + return false; +} \ No newline at end of file