-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist0.c
144 lines (113 loc) · 3.21 KB
/
list0.c
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
#include <inttypes.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <threads.h>
#define TID_UNKNOWN -1
#define MAX_THREADS 128
typedef struct {
void *next;
uintptr_t key;
} list_node_t;
typedef struct {
list_node_t *head;
list_node_t *tail;
} list_t;
static list_t *list_new() {
list_t *list = malloc(sizeof(list_t));
list_node_t *sentry_head = malloc(sizeof(list_node_t));
list_node_t *sentry_tail = malloc(sizeof(list_node_t));
sentry_head->next = sentry_tail;
sentry_head->key = 0;
sentry_tail->key = UINTPTR_MAX;
list->head = sentry_head;
list->tail = sentry_tail;
return list;
}
static bool __list_find(list_t *list,
uintptr_t *key,
list_node_t ***par_prev,
list_node_t **par_curr,
list_node_t **par_next)
{
list_node_t **prev = &list->head;
list_node_t *curr = *prev;
list_node_t *next;
while (true) {
next = curr->next;
if (!(curr->key < *key)) {
*par_curr = curr;
*par_prev = prev;
*par_next = next;
return (curr->key == *key);
}
prev = (list_node_t **) &curr->next;
curr = next;
if (next == list->tail)
break;
}
*par_curr = curr;
*par_prev = prev;
*par_next = next;
return false;
}
static bool list_insert(list_t *list, uintptr_t key)
{
list_node_t *new = malloc(sizeof(list_node_t));
new->key = key;
list_node_t **prev, *curr, *next;
if (__list_find(list, &key, &prev, &curr, &next)) {
return false;
}
new->next = curr;
*prev = new;
return true;
}
static thread_local int tid_v = TID_UNKNOWN;
static atomic_int_fast32_t tid_v_base = ATOMIC_VAR_INIT(0);
static inline int tid(void)
{
if (tid_v == TID_UNKNOWN) {
tid_v = atomic_fetch_add(&tid_v_base, 1);
}
return tid_v;
}
#define N_ELEMENTS 128
static uintptr_t elements[MAX_THREADS + 1][N_ELEMENTS];
static void *insert_thread(void *arg)
{
list_t *list = arg;
// Slight changes to test ordering
for (int i = N_ELEMENTS - 1; i >= 0; i--)
list_insert(list, (uintptr_t) &elements[tid()][i]);
return NULL;
}
#define N_THREADS 1
int main() {
pthread_t thr[N_THREADS];
list_t *list = list_new();
for (size_t i = 0; i < N_THREADS; i++)
pthread_create(&thr[i], NULL, insert_thread, list);
for (size_t i = 0; i < N_THREADS; i++)
pthread_join(thr[i], NULL);
list_node_t *cur = list->head;
for (size_t tid = 0; tid < tid_v_base; tid++) {
for (size_t i = 0; i < N_ELEMENTS; i++) {
list_node_t *next = cur->next;
if (!next) {
fprintf(stderr, "PREMATURE END OF LIST!\n");
return -1;
}
if (next->key != (uintptr_t) &elements[tid][i]) {
fprintf(stderr, "UNEXPECTED ORDERING, EXPECTED %lu GOT %lu\n",
(uintptr_t) &elements[tid][i], next->key);
return -1;
}
cur = next;
}
}
fprintf(stderr, "TEST OK!\n");
return 0;
}