forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst_tk.c
227 lines (189 loc) · 3.89 KB
/
bst_tk.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* File: bst_tk.c
* Author: Vasileios Trigonakis <[email protected]>
* Description: Asynchronized Concurrency: The Secret to Scaling Concurrent
* Search Data Structures, Tudor David, Rachid Guerraoui, Vasileios Trigonakis,
* ASPLOS '15
* bst_tk.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[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 "bst_tk.h"
RETRY_STATS_VARS;
sval_t
bst_tk_delete(intset_t* set, skey_t key)
{
node_t* curr;
node_t* pred = NULL;
node_t* ppred = NULL;
volatile uint64_t curr_ver = 0;
uint64_t pred_ver = 0, ppred_ver = 0, right = 0, pright = 0;
retry:
PARSE_TRY();
UPDATE_TRY();
curr = set->head;
do
{
curr_ver = curr->lock.to_uint64;
ppred = pred;
ppred_ver = pred_ver;
pright = right;
pred = curr;
pred_ver = curr_ver;
if (key < curr->key)
{
right = 0;
curr = (node_t*) curr->left;
}
else
{
right = 1;
curr = (node_t*) curr->right;
}
}
while(likely(!curr->leaf));
if (curr->key != key)
{
return 0;
}
if ((!tl_trylock_version(&ppred->lock, (volatile tl_t*) &ppred_ver, pright)))
{
goto retry;
}
if ((!tl_trylock_version_both(&pred->lock, (volatile tl_t*) &pred_ver)))
{
tl_revert(&ppred->lock, pright);
goto retry;
}
if (pright)
{
if (right)
{
ppred->right = pred->left;
}
else
{
ppred->right = pred->right;
}
}
else
{
if (right)
{
ppred->left = pred->left;
}
else
{
ppred->left = pred->right;
}
}
tl_unlock(&ppred->lock, pright);
#if GC == 1
ssmem_free(alloc, curr);
ssmem_free(alloc, pred);
#endif
return curr->val;
}
sval_t
bst_tk_find(intset_t* set, skey_t key)
{
PARSE_TRY();
node_t* curr = set->head;
while (likely(!curr->leaf))
{
if (key < curr->key)
{
curr = (node_t*) curr->left;
}
else
{
curr = (node_t*) curr->right;
}
}
if (curr->key == key)
{
return curr->val;
}
return 0;
}
int
bst_tk_insert(intset_t* set, skey_t key, sval_t val)
{
node_t* curr;
node_t* pred = NULL;
volatile uint64_t curr_ver = 0;
uint64_t pred_ver = 0, right = 0;
retry:
PARSE_TRY();
UPDATE_TRY();
curr = set->head;
do
{
curr_ver = curr->lock.to_uint64;
pred = curr;
pred_ver = curr_ver;
if (key < curr->key)
{
right = 0;
curr = (node_t*) curr->left;
}
else
{
right = 1;
curr = (node_t*) curr->right;
}
}
while(likely(!curr->leaf));
if (curr->key == key)
{
return 0;
}
node_t* nn = new_node(key, val, NULL, NULL, 0);
node_t* nr = new_node_no_init();
if ((!tl_trylock_version(&pred->lock, (volatile tl_t*) &pred_ver, right)))
{
ssmem_free(alloc, nn);
ssmem_free(alloc, nr);
goto retry;
}
if (key < curr->key)
{
nr->key = curr->key;
nr->left = nn;
nr->right = curr;
}
else
{
nr->key = key;
nr->left = curr;
nr->right = nn;
}
#if defined(__tile__)
/* on tilera you may have store reordering causing the pointer to a new node
to become visible, before the contents of the node are visible */
MEM_BARRIER;
#endif /* __tile__ */
if (right)
{
pred->right = nr;
}
else
{
pred->left = nr;
}
tl_unlock(&pred->lock, right);
return 1;
}