-
Notifications
You must be signed in to change notification settings - Fork 28
/
ts_deque.h
108 lines (88 loc) · 3.09 KB
/
ts_deque.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
// Copyright (c) 2012-2013, 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_TS_DEQUE_H_
#define SCAL_DATASTRUCTURES_TS_DEQUE_H_
#include <inttypes.h>
#include <atomic>
#include "datastructures/pool.h"
#include "util/malloc.h"
#include "util/platform.h"
#include "util/random.h"
template<typename T, typename TSBuffer, typename Timestamp>
class TSDeque : public Pool<T> {
private:
TSBuffer *buffer_;
Timestamp *timestamping_;
public:
TSDeque (uint64_t num_threads, uint64_t delay) {
timestamping_ = static_cast<Timestamp*>(
scal::get<Timestamp>(scal::kCachePrefetch * 4));
timestamping_->initialize(delay, num_threads);
buffer_ = static_cast<TSBuffer*>(
scal::get<TSBuffer>(scal:: kCachePrefetch * 4));
buffer_->initialize(num_threads, timestamping_);
}
char* ds_get_stats(void) {
return buffer_->ds_get_stats();
}
bool put(T element) {
// Randomly insert an element either at the left or the right side
// of the deque.
if ((hwrand() % 2) == 0) {
return insert_left(element);
}
return insert_right(element);
}
bool get(T *element) {
// Randomly remove an element either at the left or the right side
// of the deque.
if ((hwrand() % 2) == 0) {
return remove_left(element);
}
return remove_right(element);
}
bool insert_left(T element) {
std::atomic<uint64_t> *item = buffer_->insert_left(element);
// In the set_timestamp operation first a new timestamp is acquired
// and then assigned to the item. The operation may not be executed
// atomically.
timestamping_->set_timestamp(item);
return true;
}
bool insert_right(T element) {
std::atomic<uint64_t> *item = buffer_->insert_right(element);
// In the set_timestamp operation first a new timestamp is acquired
// and then assigned to the item. The operation may not be executed
// atomically.
timestamping_->set_timestamp(item);
return true;
}
bool remove_left(T *element) {
// Read the invocation time of this operation, needed for the
// elimination optimization.
uint64_t invocation_time[2];
timestamping_->read_time(invocation_time);
while (buffer_->try_remove_left(element, invocation_time)) {
if (*element != (T)NULL) {
return true;
}
}
// The deque was empty, return false.
return false;
}
bool remove_right(T *element) {
// Read the invocation time of this operation, needed for the
// elimination optimization.
uint64_t invocation_time[2];
timestamping_->read_time(invocation_time);
while (buffer_->try_remove_right(element, invocation_time)) {
if (*element != (T)NULL) {
return true;
}
}
// The deque was empty, return false.
return false;
}
};
#endif // SCAL_DATASTRUCTURES_TS_DEQUE_H_