-
Notifications
You must be signed in to change notification settings - Fork 28
/
ts_queue.h
67 lines (53 loc) · 1.99 KB
/
ts_queue.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
// 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_QUEUE_H_
#define SCAL_DATASTRUCTURES_TS_QUEUE_H_
#include <inttypes.h>
#include <atomic>
#include "datastructures/queue.h"
#include "util/malloc.h"
#include "util/platform.h"
template<typename T, typename TSBuffer, typename Timestamp>
class TSQueue : public Queue<T> {
private:
TSBuffer *buffer_;
Timestamp *timestamping_;
public:
TSQueue (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 enqueue(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 dequeue(T *element) {
buffer_->inc_counter1(1);
uint64_t invocation_time[2];
// The invocation time of this operation is only needed for the
// elimination optimization, which is not possible for a queue.
// Therefore we do not read the time to set the invocation time
// but initialize it with TOP.
timestamping_->init_top(invocation_time);
while (buffer_->try_remove_right(element, invocation_time)) {
if (*element != (T)NULL) {
return true;
}
}
// The queue was empty, return false.
return false;
}
};
#endif // SCAL_DATASTRUCTURES_TS_QUEUE_H_