-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathset-optik.c
114 lines (105 loc) · 2.3 KB
/
set-optik.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
/*
* File: set-optik.c
* Author: Vasileios Trigonakis <[email protected]>
* Description: A simple OPTIK-based array map
* set-optik.c is part of ASCYLIB
*
* Copyright (c) 2015 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 "set-optik.h"
RETRY_STATS_VARS;
LOCK_LOCAL_DATA;
const int version_every = 31;
sval_t
optik_map_contains(map_t* map, skey_t key)
{
int i;
restart:
COMPILER_NO_REORDER(optik_t version = optik_get_version_wait(map->lock););
for (i = 0; i < map->size; i++)
{
if (map->array[i].key == key)
{
sval_t val = map->array[i].val;
if (optik_is_same_version(version, *map->lock))
{
return val;
}
goto restart;
}
}
return 0;
}
int
optik_map_insert(map_t* map, skey_t key, sval_t val)
{
NUM_RETRIES();
optik_t version;
restart:
COMPILER_NO_REORDER(version = *map->lock;);
int free_idx = -1;
int i;
for (i = 0; i < map->size; i++)
{
skey_t ck = map->array[i].key;
if (ck == key)
{
return 0;
}
else if (ck == 0)
{
free_idx = i;
}
}
if (!optik_trylock_version(map->lock, version))
{
DO_PAUSE();
goto restart;
}
int res = 0;
if (free_idx >= 0)
{
map->array[free_idx].key = key;
map->array[free_idx].val = val;
res = 1;
}
optik_unlock(map->lock);
return res;
}
sval_t
optik_map_remove(map_t* map, skey_t key)
{
NUM_RETRIES();
optik_t version;
restart:
COMPILER_NO_REORDER(version = *map->lock;);
int i;
for (i = 0; i < map->size; i++)
{
if (map->array[i].key == key)
{
if (!optik_trylock_version(map->lock, version))
{
DO_PAUSE();
goto restart;
}
sval_t val = map->array[i].val;
map->array[i].key = 0;
optik_unlock(map->lock);
return val;
}
}
return 0;
}