forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist-lock.c
196 lines (172 loc) · 3.83 KB
/
skiplist-lock.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* File: skiplist-lock.c
* Author: Vasileios Trigonakis <[email protected]>
* Description:
* skiplist-lock.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "skiplist-lock.h"
#include "utils.h"
unsigned int levelmax;
unsigned int size_pad_32;
__thread ssmem_allocator_t* alloc;
inline int
get_rand_level()
{
int i, level = 1;
for (i = 0; i < levelmax - 1; i++)
{
if ((rand_range(100)-1) < 50)
level++;
else
break;
}
/* 1 <= level <= levelmax */
return level;
}
int
floor_log_2(unsigned int n)
{
int pos = 0;
if (n >= 1<<16) { n >>= 16; pos += 16; }
if (n >= 1<< 8) { n >>= 8; pos += 8; }
if (n >= 1<< 4) { n >>= 4; pos += 4; }
if (n >= 1<< 2) { n >>= 2; pos += 2; }
if (n >= 1<< 1) { pos += 1; }
return ((n == 0) ? (-1) : pos);
}
/*
* Create a new node without setting its next fields.
*/
sl_node_t*
sl_new_simple_node(skey_t key, sval_t val, int toplevel, int transactional)
{
sl_node_t* node;
#if GC == 1
if (unlikely(transactional))
{
/* use levelmax instead of toplevel in order to be able to use the ssalloc allocator */
size_t ns = size_pad_32;
size_t ns_rm = ns & 63;
if (ns_rm)
{
ns += 64 - ns_rm;
}
node = (sl_node_t*)ssalloc_aligned(CACHE_LINE_SIZE, ns);
}
else
{
size_t ns = size_pad_32;
# if defined(DO_PAD)
size_t ns_rm = ns & 63;
if (ns_rm)
{
ns += 64 - ns_rm;
}
# endif
node = (sl_node_t*) ssmem_alloc(alloc, ns);
}
#else
size_t ns = size_pad_32;
if (transactional)
{
size_t ns_rm = ns & 63;
if (ns_rm)
{
ns += 64 - ns_rm;
}
}
node = (sl_node_t*)ssalloc(ns);
#endif
node->key = key;
node->val = val;
node->toplevel = toplevel;
node->state = ND_LINKING;
/* node->fullylinked = 0; */
optik_init(&node->lock);
#if defined(__tile__)
MEM_BARRIER;
#endif
return node;
}
/*
* Create a new node with its next field.
* If next=NULL, then this create a tail node.
*/
sl_node_t*
sl_new_node(skey_t key, sval_t val, sl_node_t *next, int toplevel, int transactional)
{
sl_node_t *node;
int i;
node = sl_new_simple_node(key, val, toplevel, transactional);
for (i = 0; i < toplevel; i++)
node->next[i] = next;
MEM_BARRIER;
return node;
}
void
sl_delete_node(sl_node_t *n)
{
DESTROY_LOCK(ND_GET_LOCK(n));
ssfree_alloc(1, (void*) n);
}
sl_intset_t*
sl_set_new()
{
sl_intset_t *set;
sl_node_t *min, *max;
if ((set = (sl_intset_t *)ssalloc_aligned(CACHE_LINE_SIZE, sizeof(sl_intset_t))) == NULL)
{
perror("malloc");
exit(1);
}
max = sl_new_node(KEY_MAX, 0, NULL, levelmax, 1);
min = sl_new_node(KEY_MIN, 0, max, levelmax, 1);
set->head = min;
return set;
}
void
sl_set_delete(sl_intset_t *set)
{
sl_node_t *node, *next;
node = set->head;
while (node != NULL)
{
next = node->next[0];
sl_delete_node(node);
node = next;
}
#if defined(LL_GLOBAL_LOCK)
ssfree((void*) set->lock);
#endif
ssfree((void*) set);
}
int sl_set_size(sl_intset_t *set)
{
int size = 0;
sl_node_t *node;
/* We have at least 2 elements */
node = set->head->next[0];
while (node->next[0] != NULL)
{
/* if (!optik_is_deleted(node->lock)) */
{
size++;
}
node = node->next[0];
}
return size;
}