summaryrefslogtreecommitdiffstats
path: root/04_exercise/arena/arena_list.c
blob: 4becef66c44f27cb76f89e8763a8212c75c124ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Created by stefan on 10.06.20.
//
#include "arena_list.h"
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <unistd.h>

#include <rwlock.h>

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) {
    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) {
    fprintf(stderr, "Thread %lu: Removing element \n", pthread_self() % 1000);
    rwLockWrite(&al->lock);
    for (size_t i = 0; i < al->size; ++i) {
        if (al->arena[i].value != value) {
            continue;
        }
        Node *node = &al->arena[i];
        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;
    }
    rwUnlockWrite(&al->lock);
    return -1;
}

void *alFindLastElem(AtomicArenaList *al, SearchFunction f) {
    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;
}