forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.h
158 lines (133 loc) · 3.56 KB
/
bst.h
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
/*
* File: bst.h
* Author: Vasileios Trigonakis <[email protected]>
* Description:
* bst.h 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.
*
*/
#ifndef _H_BST_TK_
#define _H_BST_TK_
#include <assert.h>
#include <getopt.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
#include <atomic_ops.h>
#include "lock_if.h"
#include "common.h"
#include "utils.h"
#include "measurements.h"
#include "ssalloc.h"
#include "ssmem.h"
static volatile int stop;
extern __thread ssmem_allocator_t* alloc;
typedef union tl32
{
struct
{
volatile uint16_t version;
volatile uint16_t ticket;
};
volatile uint32_t to_uint32;
} tl32_t;
typedef union tl
{
tl32_t lr[2];
uint64_t to_uint64;
} tl_t;
static inline int
tl_trylock_version(volatile tl_t* tl, volatile tl_t* tl_old, int right)
{
uint16_t version = tl_old->lr[right].version;
if (unlikely(version != tl_old->lr[right].ticket))
{
return 0;
}
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 6
tl32_t tlo = { .version = version, .ticket = version };
tl32_t tln = { .version = version, .ticket = (version + 1) };
return CAS_U32(&tl->lr[right].to_uint32, tlo.to_uint32, tln.to_uint32) == tlo.to_uint32;
#else
tl32_t tlo = { version, version };
tl32_t tln = { version, (version + 1) };
#endif
return CAS_U32(&tl->lr[right].to_uint32, tlo.to_uint32, tln.to_uint32) == tlo.to_uint32;
}
#define TLN_REMOVED 0x0000FFFF0000FFFF0000LL
static inline int
tl_trylock_version_both(volatile tl_t* tl, volatile tl_t* tl_old)
{
uint16_t v0 = tl_old->lr[0].version;
uint16_t v1 = tl_old->lr[1].version;
if (unlikely(v0 != tl_old->lr[0].ticket || v1 != tl_old->lr[1].ticket))
{
return 0;
}
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 6
tl_t tlo = { .to_uint64 = tl_old->to_uint64 };
return CAS_U64(&tl->to_uint64, tlo.to_uint64, TLN_REMOVED) == tlo.to_uint64;
#else
/* tl_t tlo; */
/* tlo.uint64_t = tl_old->to_uint64; */
uint64_t tlo = *(uint64_t*) tl_old;
return CAS_U64((uint64_t*) tl, tlo, TLN_REMOVED) == tlo;
#endif
}
static inline void
tl_unlock(volatile tl_t* tl, int right)
{
/* PREFETCHW(tl); */
#ifdef __tile__
MEM_BARRIER;
#endif
COMPILER_NO_REORDER(tl->lr[right].version++);
}
static inline void
tl_revert(volatile tl_t* tl, int right)
{
/* PREFETCHW(tl); */
COMPILER_NO_REORDER(tl->lr[right].ticket--);
}
typedef struct node
{
skey_t key;
union
{
sval_t val;
volatile uint64_t leaf;
};
volatile struct node* left;
volatile struct node* right;
volatile tl_t lock;
uint8_t padding[CACHE_LINE_SIZE - 40];
} node_t;
typedef ALIGNED(CACHE_LINE_SIZE) struct intset
{
node_t* head;
} intset_t;
node_t* new_node(skey_t key, sval_t val, node_t* l, node_t* r, int initializing);
node_t* new_node_no_init();
intset_t* set_new();
void set_delete(intset_t* set);
int set_size(intset_t* set);
void node_delete(node_t* node);
#endif /* _H_BST_TK_ */