forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy.c
167 lines (148 loc) · 3.45 KB
/
lazy.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
/*
* File: lazy.c
* Author: Vincent Gramoli <[email protected]>,
* Vasileios Trigonakis <[email protected]>
* Description: A Lazy Concurrent List-Based Set Algorithm,
* S. Heller, M. Herlihy, V. Luchangco, M. Moir, W.N. Scherer III, N. Shavit
* p.3-16, OPODIS 2005
* lazy.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 "lazy.h"
RETRY_STATS_VARS;
/*
* Checking that both curr and pred are both unmarked and that pred's next pointer
* points to curr to verify that the entries are adjacent and present in the list.
*/
inline int
parse_validate(node_l_t* pred, node_l_t* curr)
{
return (!pred->marked && !curr->marked && (pred->next == curr));
}
sval_t
parse_find(intset_l_t *set, skey_t key)
{
PARSE_TRY();
node_l_t* curr = set->head;
while (curr->key < key)
{
curr = curr->next;
}
sval_t res = 0;
if ((curr->key == key) && !curr->marked)
{
res = curr->val;
}
return res;
}
int
parse_insert(intset_l_t *set, skey_t key, sval_t val)
{
node_l_t *curr, *pred, *newnode;
int result = -1;
do
{
PARSE_TRY();
pred = set->head;
curr = pred->next;
while (likely(curr->key < key))
{
pred = curr;
curr = curr->next;
}
UPDATE_TRY();
#if LAZY_RO_FAIL == 1
if (curr->key == key)
{
if (unlikely(curr->marked))
{
continue;
}
return false;
}
#endif
GL_LOCK(set->lock); /* when GL_[UN]LOCK is defined the [UN]LOCK is not ;-) */
LOCK(ND_GET_LOCK(pred));
if (parse_validate(pred, curr))
{
result = (curr->key != key);
if (result)
{
newnode = new_node_l(key, val, curr, 0);
#ifdef __tile__
MEM_BARRIER;
#endif
pred->next = newnode;
}
}
GL_UNLOCK(set->lock);
UNLOCK(ND_GET_LOCK(pred));
}
while (result < 0);
return result;
}
/*
* Logically remove an element by setting a mark bit to 1
* before removing it physically.
*/
sval_t
parse_delete(intset_l_t *set, skey_t key)
{
node_l_t *pred, *curr;
sval_t result = 0;
int done = 0;
do
{
PARSE_TRY();
pred = set->head;
curr = pred->next;
while (likely(curr->key < key))
{
pred = curr;
curr = curr->next;
}
UPDATE_TRY();
#if LAZY_RO_FAIL == 1
if (curr->key != key)
{
return false;
}
#endif
GL_LOCK(set->lock); /* when GL_[UN]LOCK is defined the [UN]LOCK is not ;-) */
LOCK(ND_GET_LOCK(pred));
LOCK(ND_GET_LOCK(curr));
if (parse_validate(pred, curr))
{
if (key == curr->key)
{
result = curr->val;
node_l_t* c_nxt = curr->next;
curr->marked = 1;
pred->next = c_nxt;
#if GC == 1
ssmem_free(alloc, (void*) curr);
#endif
}
done = 1;
}
GL_UNLOCK(set->lock);
UNLOCK(ND_GET_LOCK(curr));
UNLOCK(ND_GET_LOCK(pred));
}
while (!done);
return result;
}