forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts_stack.h
62 lines (47 loc) · 1.67 KB
/
ts_stack.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
// 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_STACK_H_
#define SCAL_DATASTRUCTURES_TS_STACK_H_
#include <inttypes.h>
#include <atomic>
#include "datastructures/stack.h"
#include "util/malloc.h"
#include "util/platform.h"
template<typename T, typename TSBuffer, typename Timestamp>
class TSStack : public Stack<T> {
private:
TSBuffer *buffer_;
Timestamp *timestamping_;
public:
TSStack (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();
}
inline bool push(T element) {
std::atomic<uint64_t> *item = buffer_->insert_right(element);
timestamping_->set_timestamp(item);
return true;
}
inline bool pop(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 stack was empty, return false.
return false;
}
};
#endif // SCAL_DATASTRUCTURES_TS_DATASTRUCTURE_H_