-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseqobjv.cc
280 lines (234 loc) · 8.47 KB
/
seqobjv.cc
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// file seqobjv.cc - sequence of object values - sets and tuples
/** Copyright (C) 2017 Basile Starynkevitch and later the FSF
MONIMELT is a monitor for MELT - see http://gcc-melt.org/
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
**/
#include "meltmoni.hh"
template<unsigned hinit, unsigned k1, unsigned k2, unsigned k3, unsigned k4>
MomHash
MomAnyObjSeq::compute_hash_seq(MomObject*const* obarr, unsigned sz)
{
if (MOM_UNLIKELY(sz>0 && obarr==nullptr))
MOM_FAILURE("MomAnyObjSeq::compute_hash null obarr for sz=" << sz);
MomHash h1 = hinit;
MomHash h2 = sz ^ (hinit + k2 + (k2 + k3) / ((sz&0xffff)+2) + k4);
for (unsigned ix=0; ix<sz; ix++)
{
const MomObject* curob = obarr[ix];
if (MOM_UNLIKELY(curob==nullptr))
MOM_FAILURE("MomAnyObjSeq::compute_hash null object at ix=" << ix);
if (MOM_UNLIKELY(curob->kindw() != MomKind::TagObjectK))
MOM_FAILURE("MomAnyObjSeq::compute_hash bad object at ix=" << ix);
MomHash hob = curob->hash();
if (ix % 2 == 0)
h1 = (k1 * h1) ^ (hob * k2 + ix);
else
h2 = (k3 * h2 + 11 * (ix&0xffff)) ^ (hob * k4);
}
MomHash h = h1 ^ h2;
if (MOM_UNLIKELY(mom_hash(h)==0))
return 3*(h1 & 0xffffff) + 5*(h2 & 0xfffff) + (sz & 0xffff) + hinit/1000 + 8;
return mom_hash(h);
} // end of MomAnyObjSeq::compute_hash
void
MomAnyObjSeq::scan_gc(MomGC* gc) const
{
for (auto pob : *this)
if (pob)
gc->scan_object(const_cast<MomObject*>(pob));
} // end MomAnyObjSeq::scan_gc
MomAnyObjSeq::MomAnyObjSeq(MomKind kd, MomObject*const* obarr, MomSize sz, MomHash h)
: MomAnyVal(kd,sz,h),
_obseq{nullptr}
{
memcpy(const_cast<MomObject**>(_obseq), obarr, sz*sizeof(MomObject*));
} // end MomAnyObjSeq::MomAnyObjSeq
//////////////// sets
MomSet::MomPtrBag<MomSet> MomSet::_bagarr_[MomSet::_swidth_];
std::atomic<unsigned> MomSet::_nbclearedbags_;
std::atomic<unsigned> MomSet::_nbsweepedbags_;
MomHash
MomSet::compute_hash(MomObject*const* obarr, unsigned sz)
{
return compute_hash_seq<MomSet::hinit,
MomSet::k1,
MomSet::k2,
MomSet::k3,
MomSet::k4>(obarr, sz);
};
void
MomSet::gc_todo_clear_marks(MomGC* gc)
{
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_clear_marks start");
_nbclearedbags_.store(0);
for (unsigned ix=0; ix<_swidth_; ix++)
gc->add_todo([=](MomGC*thisgc)
{
gc_todo_clear_mark_slot(thisgc,ix);
if (1+_nbclearedbags_.fetch_add(1) >= _swidth_)
thisgc->add_todo([=](MomGC*ourgc)
{
ourgc->maybe_start_scan();
});
});
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_clear_marks end");
} // end MomSet::gc_todo_clear_marks
void
MomSet::gc_todo_clear_mark_slot(MomGC*gc,unsigned slotix)
{
MOM_ASSERT(slotix<_swidth_, "gc_todo_clear_mark_slot invalid slotix=" << slotix);
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_clear_mark_slot start slotix=" << slotix);
auto& curbag = _bagarr_[slotix];
std::lock_guard<std::mutex> gu(curbag._bag_mtx);
curbag.unsync_bag_gc_clear_marks(gc);
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_clear_mark_slot end slotix=" << slotix);
} // end MomSet::gc_todo_clear_mark_slot
void
MomSet::gc_todo_destroy_dead(MomGC* gc)
{
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_destroy_dead start");
_nbsweepedbags_.store(0);
for (unsigned ix=0; ix<_swidth_; ix++)
gc->add_todo([=](MomGC*thisgc)
{
gc_todo_sweep_destroy_slot(thisgc,ix);
if (1+_nbsweepedbags_.fetch_add(1) >= _swidth_)
thisgc->add_todo([=](MomGC*ourgc)
{
ourgc->maybe_done_sweep();
});
});
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_destroy_dead end");
} // end MomSet::gc_todo_destroy_dead
void
MomSet::gc_todo_sweep_destroy_slot(MomGC*gc,unsigned slotix)
{
MOM_ASSERT(slotix<_swidth_, "gc_todo_sweep_destroy_slot invalid slotix=" << slotix);
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_sweep_destroy_slot start slotix=" << slotix);
auto& curbag = _bagarr_[slotix];
std::lock_guard<std::mutex> gu(curbag._bag_mtx);
curbag.unsync_bag_gc_delete_unmarked_values(gc);
MOM_DEBUGLOG(garbcoll, "MomSet::gc_todo_sweep_destroy_slot end slotix=" << slotix);
} // end MomSet::gc_todo_clear_mark_slot
const MomSet*
MomSet::make_from_ascending_array(const MomObject*const* obarr, MomSize sz)
{
MomHash h = compute_hash(const_cast<MomObject*const*>(obarr), sz);
unsigned slotix = slotindex(h);
auto& curbag = _bagarr_[slotix];
return curbag.unsync_bag_make_from_hash
(h,
mom_align((sz-MOM_FLEXIBLE_DIM)*sizeof(MomObject*)),
const_cast<MomObject**>(obarr), sz);
} // end MomSet::make_from_ascending_array
const MomSet*
MomSet::make_from_objptr_set(const MomObjptrSet&oset)
{
MomObjptrVector vec;
vec.reserve(oset.size());
for (const MomObject* pob : oset)
{
if (MOM_LIKELY(pob != nullptr))
vec.push_back(pob);
}
return make_from_ascending_objptr_vector(vec);
} // end MomSet::make_from_objptr_set
std::mutex*
MomSet::valmtx() const
{
return &_bagarr_[slotindex(hash())]._bag_mtx;
} // end MomSet::valmtx
//////////////// tuples
MomTuple::MomPtrBag<MomTuple> MomTuple::_bagarr_[MomTuple::_swidth_];
std::atomic<unsigned> MomTuple::_nbclearedbags_;
std::atomic<unsigned> MomTuple::_nbsweepedbags_;
MomHash
MomTuple::compute_hash(MomObject*const* obarr, unsigned sz)
{
return compute_hash_seq<MomTuple::hinit,
MomTuple::k1,
MomTuple::k2,
MomTuple::k3,
MomTuple::k4>(obarr, sz);
};
const MomTuple*
MomTuple::make_from_array(MomObject*const* obarr, MomSize sz)
{
MomHash h = compute_hash(obarr, sz);
unsigned slotix = slotindex(h);
auto& curbag = _bagarr_[slotix];
return curbag.unsync_bag_make_from_hash
(h,
mom_align((sz-MOM_FLEXIBLE_DIM)*sizeof(MomObject*)),
obarr, sz);
} // end MomTuple::make_from_array
void
MomTuple::gc_todo_clear_marks(MomGC* gc)
{
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_clear_marks start");
_nbclearedbags_.store(0);
for (unsigned ix=0; ix<_swidth_; ix++)
gc->add_todo([=](MomGC*thisgc)
{
gc_todo_clear_mark_slot(thisgc,ix);
if (1+_nbclearedbags_.fetch_add(1) >= _swidth_)
thisgc->add_todo([=](MomGC*ourgc)
{
ourgc->maybe_start_scan();
});
});
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_clear_marks end");
} // end MomTuple::gc_todo_clear_marks
void
MomTuple::gc_todo_destroy_dead(MomGC* gc)
{
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_destroy_dead start");
_nbsweepedbags_.store(0);
for (unsigned ix=0; ix<_swidth_; ix++)
gc->add_todo([=](MomGC*thisgc)
{
gc_todo_sweep_destroy_slot(thisgc,ix);
if (1+_nbsweepedbags_.fetch_add(1) >= _swidth_)
thisgc->add_todo([=](MomGC*ourgc)
{
ourgc->maybe_done_sweep();
});
});
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_destroy_dead end");
} // end MomTuple::gc_todo_destroy_dead
void
MomTuple::gc_todo_sweep_destroy_slot(MomGC*gc,unsigned slotix)
{
MOM_ASSERT(slotix<_swidth_, "gc_todo_sweep_destroy_slot invalid slotix=" << slotix);
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_sweep_destroy_slot start slotix=" << slotix);
auto& curbag = _bagarr_[slotix];
std::lock_guard<std::mutex> gu(curbag._bag_mtx);
curbag.unsync_bag_gc_delete_unmarked_values(gc);
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_sweep_destroy_slot end slotix=" << slotix);
} // end MomTuple::gc_todo_clear_mark_slot
void
MomTuple::gc_todo_clear_mark_slot(MomGC*gc,unsigned slotix)
{
MOM_ASSERT(slotix<_swidth_, "gc_todo_clear_mark_slot invalid slotix=" << slotix);
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_clear_mark_slot start slotix=" << slotix);
auto& curbag = _bagarr_[slotix];
std::lock_guard<std::mutex> gu(curbag._bag_mtx);
curbag.unsync_bag_gc_clear_marks(gc);
MOM_DEBUGLOG(garbcoll, "MomTuple::gc_todo_clear_mark_slot end slotix=" << slotix);
} // end MomTuple::gc_todo_clear_mark_slot
std::mutex*
MomTuple::valmtx() const
{
return &_bagarr_[slotindex(hash())]._bag_mtx;
} // end MomTuple::valmtx