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
|
/*
* include/framework/scheduling/Epoch.h
*
* Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu>
*
* Distributed under the Modified BSD License.
*
*/
#pragma once
#include <condition_variable>
#include <mutex>
#include "framework/structure/MutableBuffer.h"
#include "framework/structure/ExtensionStructure.h"
#include "framework/structure/BufferView.h"
namespace de {
template <RecordInterface R, ShardInterface S, QueryInterface<R, S> Q, LayoutPolicy L>
class Epoch {
private:
typedef MutableBuffer<R> Buffer;
typedef ExtensionStructure<R, S, Q, L> Structure;
typedef BufferView<R> BufView;
public:
Epoch(size_t number=0)
: m_buffer(nullptr)
, m_structure(nullptr)
, m_active_merge(false)
, m_epoch_number(number)
, m_buffer_head(0)
{}
Epoch(size_t number, Structure *structure, Buffer *buff, size_t head)
: m_buffer(buff)
, m_structure(structure)
, m_active_merge(false)
, m_epoch_number(number)
, m_buffer_head(head)
{
structure->take_reference();
}
~Epoch() {
/* FIXME: this is needed to keep the destructor from sometimes locking
* up here. But there *shouldn't* be any threads waiting on this signal
* at object destruction, so something else is going on here that needs
* looked into
*/
// m_active_cv.notify_all();
if (m_structure) {
m_structure->release_reference();
}
}
/*
* Epochs are *not* copyable or movable. Only one can exist, and all users
* of it work with pointers
*/
Epoch(const Epoch&) = delete;
Epoch(Epoch&&) = delete;
Epoch &operator=(const Epoch&) = delete;
Epoch &operator=(Epoch&&) = delete;
size_t get_epoch_number() {
return m_epoch_number;
}
Structure *get_structure() {
return m_structure;
}
BufView get_buffer() {
return m_buffer->get_buffer_view(m_buffer_head);
}
/*
* Returns a new Epoch object that is a copy of this one. The new object
* will also contain a copy of the m_structure, rather than a reference to
* the same one. The epoch number of the new epoch will be set to the
* provided argument.
*/
Epoch *clone(size_t number) {
std::unique_lock<std::mutex> m_buffer_lock;
auto epoch = new Epoch(number);
epoch->m_buffer = m_buffer;
epoch->m_buffer_head = m_buffer_head;
if (m_structure) {
epoch->m_structure = m_structure->copy();
/* the copy routine returns a structure with 0 references */
epoch->m_structure->take_reference();
}
return epoch;
}
/*
* Check if a merge can be started from this Epoch. At present, without
* concurrent merging, this simply checks if there is currently a scheduled
* merge based on this Epoch. If there is, returns false. If there isn't,
* return true and set a flag indicating that there is an active merge.
*/
bool prepare_reconstruction() {
auto old = m_active_merge.load();
if (old) {
return false;
}
// FIXME: this needs cleaned up
while (!m_active_merge.compare_exchange_strong(old, true)) {
old = m_active_merge.load();
if (old) {
return false;
}
}
return true;
}
bool advance_buffer_head(size_t head) {
m_buffer_head = head;
return m_buffer->advance_head(m_buffer_head);
}
private:
Structure *m_structure;
Buffer *m_buffer;
std::mutex m_buffer_lock;
std::atomic<bool> m_active_merge;
/*
* The number of currently active jobs
* (queries/merges) operating on this
* epoch. An epoch can only be retired
* when this number is 0.
*/
size_t m_epoch_number;
size_t m_buffer_head;
};
}
|