summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-06-22 16:30:10 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-06-22 16:30:10 +0200
commitddb7d1e6db9d4e84e55c54c094ccd0fd34bc3a3f (patch)
treeed667185d9443e2b6dc6b12aae392f5f30b9b86a
parent021afcb8e4aa4fbc7905f527089c415108c86c75 (diff)
downloadbetriebssysteme-hardcodedqs.tar.gz
betriebssysteme-hardcodedqs.zip
-rw-r--r--04_exercise/main.c2
-rw-r--r--04_exercise/quicksort.c16
-rw-r--r--04_exercise/slotmap/slotmap.c13
-rw-r--r--04_exercise/threadpool.h27
4 files changed, 47 insertions, 11 deletions
diff --git a/04_exercise/main.c b/04_exercise/main.c
index 8c7b8db..98c0580 100644
--- a/04_exercise/main.c
+++ b/04_exercise/main.c
@@ -5,7 +5,7 @@
static TASK(long, fib, long)
- long fib(long n) {
+long fib(long n) {
if (n <= 1)
return n;
diff --git a/04_exercise/quicksort.c b/04_exercise/quicksort.c
index ade513b..1f0bfb5 100644
--- a/04_exercise/quicksort.c
+++ b/04_exercise/quicksort.c
@@ -7,14 +7,20 @@
static size_t partition(int v[], size_t len);
-/* TODO: quicksort-Implementation parallelisieren */
+static TASK_QS(void, quicksort)
static void quicksort(int list[], size_t len) {
+ fprintf(stderr, "quicksort call with %p, %lu\n", (void*)list, (ulong)len);
+
if (len <= 1) return;
-
+
size_t mid = partition(list, len);
- quicksort(list, mid);
- quicksort(list + mid + 1, len - mid - 1);
+
+ quicksort_fut *first = quicksortAsync((quicksort_fut[]){quicksortFuture(list, mid)});
+ quicksort_fut *second = quicksortAsync((quicksort_fut[]){quicksortFuture(list + mid + 1, len - mid - 1)});
+
+ quicksortAwait(first);
+ quicksortAwait(second);
}
size_t partition(int v[], size_t len) {
@@ -87,7 +93,7 @@ int main(int argc, const char *argv[]) {
atexit(&tpRelease);
/* read the length of the list from the command line */
- size_t list_len = 30000000;
+ size_t list_len = 5;
if (argc >= 2) {
long arg = strtol(argv[1], (char **)&argv[strlen(argv[1])], 10);
if (arg > 0)
diff --git a/04_exercise/slotmap/slotmap.c b/04_exercise/slotmap/slotmap.c
index 57c4ef5..ca0460e 100644
--- a/04_exercise/slotmap/slotmap.c
+++ b/04_exercise/slotmap/slotmap.c
@@ -4,18 +4,21 @@
#include "slotmap.h"
#include <stdio.h>
+
+#define intptr_t __intptr_t
+
smHeader smInit(smEntry * slab, size_t size) {
for (int i = 0; i < size; ++i) {
- slab[i].value = (intptr_t)NULL;
+ slab[i].value = (__intptr_t)NULL;
}
return (smHeader){.slab = slab, .size=size};
}
int smInsert(smHeader const * header, void * value) {
for (size_t i = 0; i< header->size; ++i ) {
- intptr_t ptr = atomic_load(&header->slab[i].value);
+ _Atomic(long) ptr = atomic_load(&header->slab[i].value);
if((void *)ptr == NULL) {
- intptr_t expected = (intptr_t) NULL;
- if(atomic_compare_exchange_strong(&header->slab[i].value, &expected, (intptr_t) value)){
+ __intptr_t expected = (__intptr_t) NULL;
+ if(atomic_compare_exchange_strong(&header->slab[i].value, &expected, (__intptr_t) value)){
return 0;
}
}
@@ -23,7 +26,7 @@ int smInsert(smHeader const * header, void * value) {
return -1;
}
void smDelete(smEntry * node) {
- intptr_t oldval = atomic_exchange(&node->value, (intptr_t)(NULL));
+ __intptr_t oldval = atomic_exchange(&node->value, (intptr_t)(NULL));
if((void *) oldval == NULL) {
fprintf(stderr, "A Node has been double deleted");
}
diff --git a/04_exercise/threadpool.h b/04_exercise/threadpool.h
index 745c4dd..413f996 100644
--- a/04_exercise/threadpool.h
+++ b/04_exercise/threadpool.h
@@ -118,4 +118,31 @@ extern void tpAwait(Future *future);
return tpAwait(&future->fut), future->res; \
}
+#define TASK_QS(TYPE, NAME) \
+ TYPE NAME(int *arg1, size_t arg2); \
+ \
+ typedef struct { \
+ Future fut; \
+ int* arg1; \
+ size_t arg2; \
+ } NAME ## _fut; \
+ \
+ static void NAME ## Thunk(void *args) { \
+ NAME ## _fut *data = args; \
+ NAME(data->arg1, data->arg2); \
+ } \
+ static inline NAME ## _fut NAME ## Future(int *arg1, size_t arg2) { \
+ return (NAME ## _fut) { \
+ .fut = { .fn = &NAME ## Thunk, .status=FUT_WAITING}, \
+ .arg1 = arg1, \
+ .arg2 = arg2 \
+ }; \
+ } \
+ static inline NAME ## _fut* NAME ## Async(NAME ## _fut *future) { \
+ return tpAsync(&future->fut), future; \
+ } \
+ static inline TYPE NAME ## Await(NAME ## _fut *future) { \
+ tpAwait(&future->fut); \
+ }
+
#endif