diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-06-22 16:43:42 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-06-22 16:43:42 +0200 |
commit | 239f248456ad00f894dc26dceefa968898738c9d (patch) | |
tree | 14d41af555b12c88401d8bb46bae9f4319497b62 | |
parent | 021afcb8e4aa4fbc7905f527089c415108c86c75 (diff) | |
download | betriebssysteme-239f248456ad00f894dc26dceefa968898738c9d.tar.gz betriebssysteme-239f248456ad00f894dc26dceefa968898738c9d.zip |
Getting make to run
-rw-r--r-- | 04_exercise/CMakeLists.txt | 15 | ||||
-rw-r--r-- | 04_exercise/arena/arena_list.c | 164 | ||||
-rw-r--r-- | 04_exercise/arena/arena_list.h | 57 | ||||
-rw-r--r-- | 04_exercise/arena/arena_test.c | 30 | ||||
-rw-r--r-- | 04_exercise/ppmlib.h (renamed from 04_exercise/ppmlib/ppmlib.h) | 0 | ||||
-rw-r--r-- | 04_exercise/rwlock.c (renamed from 04_exercise/rwlock/rwlock.c) | 0 | ||||
-rw-r--r-- | 04_exercise/rwlock.h (renamed from 04_exercise/rwlock/rwlock.h) | 0 | ||||
-rw-r--r-- | 04_exercise/slotmap.c (renamed from 04_exercise/slotmap/slotmap.c) | 0 | ||||
-rw-r--r-- | 04_exercise/slotmap.h (renamed from 04_exercise/slotmap/slotmap.h) | 1 | ||||
-rw-r--r-- | 04_exercise/slotmap/slomap_test.c | 19 | ||||
-rw-r--r-- | 04_exercise/threadpool.c | 2 | ||||
-rw-r--r-- | 04_exercise/threadpool.h | 2 |
12 files changed, 5 insertions, 285 deletions
diff --git a/04_exercise/CMakeLists.txt b/04_exercise/CMakeLists.txt index 4e89060..06364c2 100644 --- a/04_exercise/CMakeLists.txt +++ b/04_exercise/CMakeLists.txt @@ -14,24 +14,13 @@ add_executable(fibonacci main.c) target_link_libraries(fibonacci PRIVATE threadpool) target_link_libraries(fibonacci INTERFACE warnings) -add_library(arena_list arena/arena_list.c) -target_include_directories(arena_list PUBLIC arena) -target_link_libraries(arena_list INTERFACE warnings) -target_link_libraries(arena_list PRIVATE rwlock) - -add_executable(arena_test arena/arena_test.c) -target_link_libraries(arena_test PRIVATE arena_list) - -add_library(rwlock rwlock/rwlock.c) +add_library(rwlock rwlock.c) target_include_directories(rwlock PUBLIC rwlock) target_link_libraries(rwlock INTERFACE warnings) add_library(ppmlib INTERFACE) target_include_directories(ppmlib INTERFACE ppmlib) -add_library(slotmap slotmap/slotmap.c) +add_library(slotmap slotmap.c) target_include_directories(slotmap PUBLIC slotmap) target_link_libraries(slotmap INTERFACE warnings) - -add_executable(slotmap_test slotmap/slomap_test.c) -target_link_libraries(slotmap_test PRIVATE slotmap) diff --git a/04_exercise/arena/arena_list.c b/04_exercise/arena/arena_list.c deleted file mode 100644 index 1453297..0000000 --- a/04_exercise/arena/arena_list.c +++ /dev/null @@ -1,164 +0,0 @@ -// -// Created by stefan on 10.06.20. -// -#include "arena_list.h" -#include <pthread.h> -#include <stdio.h> -#include <unistd.h> - -#include <rwlock.h> -static const bool DEBUG = false; - -bool listContains(List const * const list ,Node const * const needle) { - for(Node * current = list ->first; current != NULL; current = current->next) { - if(current == needle) { - return true; - } - } - return false; -} -Node *listPopFront(List *list) { - Node *front = list->first; - if (front == NULL) { - return NULL; - } - - list->first = front->next; - if (front == list->last) { - // Only Node in the list - list->last = NULL; - return front; - } - - list->first->prev = NULL; - front->next = NULL; - - return front; -} - -Node *listPopBack(List *list) { - Node *back = list->last; - if (back == NULL) { - return NULL; - } - - list->last = back->prev; - if (back == list->first) { - // Only Node in the list - list->last = NULL; - return back; - } - - list->last->next = NULL; - back->prev = NULL; - - return back; -} - -void listPushFront(List *list, Node *new) { - if (list->first == NULL) { - // List was empty - list->first = 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) {} - -AtomicArenaList alInit(Node *arena, size_t size) { - AtomicArenaList al = {.activeList = (List){.first = NULL, .last = NULL}, - .freeList = (List){.first = arena, .last = &arena[size - 1]}, - .arena = arena, - .size = size}; - atomic_init(&al.lock, RW_UNLOCKED); - 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]}; - } - arena[size - 1] = (Node){.value = NULL, .prev = &arena[size - 2], .next = NULL}; - return al; -} - -int alPush(AtomicArenaList *al, void *value) { - if(DEBUG) - fprintf(stderr, "Thread %lu: Pushing \n", pthread_self() % 1000); - rwLockWrite(&al->lock); - Node *current = listPopFront(&al->freeList); - // List is empty - if (current == NULL) { - rwUnlockWrite(&al->lock); - return -1; - } - current->value = value; - listPushFront(&al->activeList, current); - rwUnlockWrite(&al->lock); - return 0; -} - -int alRemoveElem(AtomicArenaList *al, void *value) { - if(DEBUG) - fprintf(stderr, "Thread %lu: Removing element \n", pthread_self() % 1000); - rwLockWrite(&al->lock); - size_t i = 0; - for (; i < al->size; ++i) { - if (al->arena[i].value != value) { - continue; - } - } - Node *node = &al->arena[i]; - // Irgendwie removen wir das Element zwei Mal und ich verstehe nicht warum - if(i == al->size || listContains(&al->activeList,node)) { - rwUnlockWrite(&al->lock); - return -1; - } - - if (node == al->activeList.first) { - listPopFront(&al->activeList); - listPushFront(&al->freeList, node); - atomic_char *a = &al->lock; - rwUnlockWrite(a); - return 0; - } - if (node == al->activeList.last) { - listPopBack(&al->activeList); - listPushFront(&al->freeList, node); - rwUnlockWrite(&al->lock); - 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); - rwUnlockWrite(&al->lock); - return 0; -} - -void *alFindLastElem(AtomicArenaList *al, SearchFunction f) { - if(DEBUG) - fprintf(stderr, "Thread %lu: Finding last element \n", pthread_self() % 1000); - - rwLockRead(&al->lock); - - List const *const actList = &al->activeList; - if (actList->last == NULL) { - rwUnlockRead(&al->lock); - return NULL; - } - for (Node *current = actList->last; current != NULL; current = current->prev) { - if (f(current->value)) { - rwUnlockRead(&al->lock); - return current; - } - } - rwUnlockRead(&al->lock); - return NULL; -} diff --git a/04_exercise/arena/arena_list.h b/04_exercise/arena/arena_list.h deleted file mode 100644 index 304209c..0000000 --- a/04_exercise/arena/arena_list.h +++ /dev/null @@ -1,57 +0,0 @@ -// -// Created by stefan on 10.06.20. -// - -#ifndef BETRIEBSYSTEME_ARENA_LIST_H -#define BETRIEBSYSTEME_ARENA_LIST_H - -#include <stdlib.h> -#include <stdbool.h> -#include <stdatomic.h> - -typedef struct Node { - void * value; - struct Node * next; - struct Node * prev; -} Node; - -typedef struct List { - Node * first; - Node * last; -} List; - - -// Should we have a free list or just a bit set relative to the size of the slab? -/** This structure manages an arena / memory slab to be used - * This structure is thread-safe as it locks internally - * It will spin until a request is safe to access the items - * This also means this structure shouldn't be read externally - * unless the thread doing so managed to aquire the atomic_flag - */ -typedef struct ArenaList { - List freeList; - List activeList; - Node * arena; - size_t size; - atomic_char lock; -} AtomicArenaList; - -typedef bool (*SearchFunction)(void const *); -//Initializes the node array -AtomicArenaList alInit(Node * arena, size_t size); -// Return -1 if the List is full -int alPush(AtomicArenaList * al, void * value); - -// Return -1 if the Node was already deleted -int alRemoveElem(AtomicArenaList * al, void * value); - -/** - * Searches the activeList for an element - * for which the search function returns true - * The function will be passed the value pointer - * of a given Node - * @return the Element pointed or NULL if nothing matched - */ -void *alFindLastElem(AtomicArenaList *al, SearchFunction f); - -#endif // BETRIEBSYSTEME_ARENA_LIST_H diff --git a/04_exercise/arena/arena_test.c b/04_exercise/arena/arena_test.c deleted file mode 100644 index 51a9b0c..0000000 --- a/04_exercise/arena/arena_test.c +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by stefan on 10.06.20. -// -#include "arena_list.h" -#include <stdio.h> -#include <assert.h> -bool isEqualTo3(void const *data) { - int *value = (void *)data; - return *value == 3; -} -int main() { - Node arena[5]; - AtomicArenaList al = alInit(arena, 5); - int data[5] = {1, 2, 3, 4, 5}; - for (int i = 0; i < 5; ++i) { - alPush(&al, &data[4 - i]); - } - - for (Node *cur = al.activeList.first; cur != NULL; cur = cur->next) { - printf("Got digit %d \n", *(int *)cur->value); - } - Node const * node = alFindLastElem(&al, &isEqualTo3); - int * value = (int *) node->value; - printf("The value was actually %d \n", *value); - assert(*value == 3); - for (int i = 0; i < 5; ++i) { - alRemoveElem(&al, &data[4 - i]); - } - -} diff --git a/04_exercise/ppmlib/ppmlib.h b/04_exercise/ppmlib.h index 7a7f3db..7a7f3db 100644 --- a/04_exercise/ppmlib/ppmlib.h +++ b/04_exercise/ppmlib.h diff --git a/04_exercise/rwlock/rwlock.c b/04_exercise/rwlock.c index 941d176..941d176 100644 --- a/04_exercise/rwlock/rwlock.c +++ b/04_exercise/rwlock.c diff --git a/04_exercise/rwlock/rwlock.h b/04_exercise/rwlock.h index aa27f09..aa27f09 100644 --- a/04_exercise/rwlock/rwlock.h +++ b/04_exercise/rwlock.h diff --git a/04_exercise/slotmap/slotmap.c b/04_exercise/slotmap.c index 57c4ef5..57c4ef5 100644 --- a/04_exercise/slotmap/slotmap.c +++ b/04_exercise/slotmap.c diff --git a/04_exercise/slotmap/slotmap.h b/04_exercise/slotmap.h index 1d687fc..d4044d4 100644 --- a/04_exercise/slotmap/slotmap.h +++ b/04_exercise/slotmap.h @@ -7,6 +7,7 @@ #include <stdatomic.h> #include <stdlib.h> #include <stdbool.h> +#include <stdint.h> typedef struct smNode { atomic_intptr_t value; diff --git a/04_exercise/slotmap/slomap_test.c b/04_exercise/slotmap/slomap_test.c deleted file mode 100644 index 930bfa8..0000000 --- a/04_exercise/slotmap/slomap_test.c +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by stefan on 18.06.20. -// -#include <assert.h> -#include <slotmap.h> -#include <stdbool.h> - -bool find1(void const * ptr) { - return * (int const *) ptr == 1; -} -int main() { - smEntry slab [10]; - int data [6] = {0, 1, 2,3,4,5}; - smHeader header = smInit((smEntry *)&slab, 10); - smInsert(&header, &data[1]); - smEntry * entry = smFindEntry(&header, &find1); - assert(*(int *)entry->value == 1); - return 0; -} diff --git a/04_exercise/threadpool.c b/04_exercise/threadpool.c index 9d73013..d40b1da 100644 --- a/04_exercise/threadpool.c +++ b/04_exercise/threadpool.c @@ -1,8 +1,8 @@ #include "threadpool.h" #include "array.h" +#include "slotmap.h" #include <pthread.h> -#include <slotmap.h> #include <stdatomic.h> #include <stdbool.h> #include <stdio.h> diff --git a/04_exercise/threadpool.h b/04_exercise/threadpool.h index 745c4dd..6b5945b 100644 --- a/04_exercise/threadpool.h +++ b/04_exercise/threadpool.h @@ -1,9 +1,9 @@ #ifndef THREADPOOL_H_INCLUDED #define THREADPOOL_H_INCLUDED +#include "ppmlib.h" #include <stdatomic.h> #include <stddef.h> -#include <ppmlib.h> /**@brief Funktionszeiger auf eine asynchron auszuführende Funktion. * |