forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer_local_linearizability.h
53 lines (41 loc) · 1.41 KB
/
balancer_local_linearizability.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
// Copyright (c) 2014, the Scal Project Authors. All rights reserved.
// Please see the AUTHORS file for details. Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
#ifndef SCAL_DATASTRUCTURES_BALANCER_LOCAL_LINEARIZABILITY_H_
#define SCAL_DATASTRUCTURES_BALANCER_LOCAL_LINEARIZABILITY_H_
#include <gflags/gflags.h>
#include "datastructures/balancer.h"
#include "util/platform.h"
#include "util/threadlocals.h"
#include "util/random.h"
namespace scal {
DEFINE_uint64(ll_balancer_seed, 0, "local linearizability balancer seed");
class BalancerLocalLinearizability {
public:
BalancerLocalLinearizability(size_t size) : size_(size) {
distribution_ = new size_t[size];
for (size_t i = 0; i < size_; i++) {
distribution_[i] = i;
}
scal::shuffle<size_t>(distribution_, size, FLAGS_ll_balancer_seed);
}
_always_inline uint64_t get_id() {
return scal::hwrand() % size_;
}
_always_inline uint64_t put_id() {
return distribution_[scal::ThreadContext::get().thread_id() % size_];
}
_always_inline bool local_get_id(uint64_t* idx) {
*idx = distribution_[scal::ThreadContext::get().thread_id() % size_];
return true;
}
private:
size_t size_;
size_t* distribution_;
uint8_t pad_[
128
- sizeof(size_)
- sizeof(distribution_)];
};
} // namespace scal
#endif // SCAL_DATASTRUCTURES_BALANCER_LOCAL_LINEARIZABILITY_H_