summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-06-11 18:50:47 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-06-11 18:50:47 +0200
commit7ecbcce58aa7a33915a150ad3f48924c1158779d (patch)
tree021a8b56587ba44ab779ee5d1e647eb0232b3568
parentddb7d43143ff664948f27cd2cd4e18bdfcce6118 (diff)
downloadbetriebssysteme-7ecbcce58aa7a33915a150ad3f48924c1158779d.tar.gz
betriebssysteme-7ecbcce58aa7a33915a150ad3f48924c1158779d.zip
Basic Implementation
-rw-r--r--04_exercise/arena/arena_list.c44
-rw-r--r--04_exercise/arena/arena_list.h3
-rw-r--r--04_exercise/threadpool.c19
3 files changed, 60 insertions, 6 deletions
diff --git a/04_exercise/arena/arena_list.c b/04_exercise/arena/arena_list.c
index f4b4eb3..822f8f0 100644
--- a/04_exercise/arena/arena_list.c
+++ b/04_exercise/arena/arena_list.c
@@ -23,7 +23,7 @@ Node *listPopFront(List *list) {
return front;
}
-Node *listPopBack(List list) { return NULL; }
+Node *listPopBack(List* list) { return NULL; }
void listPushFront(List *list, Node *new) {
if (list->first == NULL) {
@@ -32,17 +32,20 @@ void listPushFront(List *list, Node *new) {
list->last = new;
return;
}
+ new->prev = NULL;
new->next = list->first;
new->next->prev = new;
list->first = new;
}
-void listPushBack(List list, Node *value) {}
+void listPushBack(List* list, Node *value) {}
ArenaList alInit(Node *arena, size_t size) {
ArenaList al;
al.activeList = (List){.first = NULL, .last = NULL};
al.freeList = (List){.first = arena, .last = &arena[size - 1]};
+ al.arena = arena;
+ al.size = size;
arena[0] = (Node){.value = NULL, .prev = NULL, .next = &arena[1]};
for (size_t i = 1; i < size - 1; ++i) {
arena[i] = (Node){.value = NULL, .prev = &arena[i - 1], .next = &arena[i + 1]};
@@ -60,4 +63,39 @@ int alPush(ArenaList* al, void *value) {
current->value = value;
listPushFront(&al->activeList, current);
return 0;
-} \ No newline at end of file
+}
+
+
+
+int alRemoveElem(ArenaList* al, void * value){
+ for(size_t i = 0; i < al->size; ++i) {
+ if(al->arena[i].value == value) {
+ return alRemove(al, &al->arena[i]);
+ }
+ }
+ return -1;
+}
+
+int alRemove(ArenaList* al, Node * node) {
+ //TODO Should we check that the node is actually in the active list
+ //Maybe as an assert that gets optimized out
+ if(node == al->activeList.first) {
+ listPopFront(&al->activeList);
+ listPushFront(&al->freeList, node);
+ return 0;
+ }
+ if(node == al->activeList.last) {
+ listPopBack(&al->activeList);
+ listPushFront(&al->freeList, node);
+ return 0;
+ }
+ // The node is somewhere in the middle
+ Node * prev = node ->prev;
+ Node * next = node ->next;
+
+ next->prev = prev;
+ prev->next = next;
+
+ listPushFront(&al->freeList, node);
+ return 0;
+}
diff --git a/04_exercise/arena/arena_list.h b/04_exercise/arena/arena_list.h
index 86bf805..387f881 100644
--- a/04_exercise/arena/arena_list.h
+++ b/04_exercise/arena/arena_list.h
@@ -17,9 +17,12 @@ typedef struct List {
Node * last;
} List;
// This structure manages an arena / memory slab to be used
+// Should we have a free list or just a bit set relative to the size of the slab?
typedef struct ArenaList {
List freeList;
List activeList;
+ Node * arena;
+ size_t size;
} ArenaList;
//Initializes the node array
diff --git a/04_exercise/threadpool.c b/04_exercise/threadpool.c
index 510e83e..93b8807 100644
--- a/04_exercise/threadpool.c
+++ b/04_exercise/threadpool.c
@@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <stdatomic.h>
#define MAX_FUTURES 2048
typedef struct Thread {
@@ -28,7 +29,7 @@ ThreadPool threadPool;
/* TODO: interne Hilfsfunktionen hinzufügen */
void * poolWorkerFunc(void * v_index) {
size_t index = * (size_t *) v_index;
- printf("Thread %zu started", index);
+ printf("Thread %zu started \n", index);
return NULL;
}
@@ -51,8 +52,7 @@ int tpInit(size_t size) {
void tpRelease(void) {
for (size_t i = 0; i < threadPool.size; ++i) {
if (pthread_cancel(threadPool.threads[i].pthread) != 0) {
- perror("Thread doesn't exist anymore");
- exit(-1);
+ fprintf(stderr, "The thread %zu had already exited. \n", threadPool.threads[i].index);
}
}
for (size_t i = 0; i < threadPool.size; ++i) {
@@ -66,6 +66,19 @@ void tpRelease(void) {
void tpAsync(Future *future) {
alPush(&threadPool.al, (void *) future);
+ char expected = FUT_WAITING;
+ if(!atomic_compare_exchange_strong(&future->status, &expected, FUT_IN_PROGRESS )) {
+ perror("The future has been scheduled before");
+ exit(-1);
+ }
+
+ future->fn(future);
+ expected = FUT_IN_PROGRESS;
+ if(!atomic_compare_exchange_strong(&future->status, &expected, FUT_IN_PROGRESS)) {
+ perror("The future got marked as done by another thread");
+ exit(-1);
+ }
+
}
void tpAwait(Future *future) {