@@ -152,6 +152,9 @@ class TrainingData : public Metadata {
152152 _lock_mode = need_data () ? +1 : -1 ; // if -1, we go lock-free
153153#endif
154154 }
155+ static void assert_locked_or_snapshotted () {
156+ assert (safely_locked () || _snapshot, " use under TrainingDataLocker or after snapshot" );
157+ }
155158 static void assert_locked () {
156159 assert (safely_locked (), " use under TrainingDataLocker" );
157160 }
@@ -338,20 +341,24 @@ class TrainingData : public Metadata {
338341 }
339342
340343 int length () const {
344+ TrainingDataLocker::assert_locked_or_snapshotted ();
341345 return (_deps_dyn != nullptr ? _deps_dyn->length ()
342346 : _deps != nullptr ? _deps->length ()
343347 : 0 );
344348 }
345349 E* adr_at (int i) const {
350+ TrainingDataLocker::assert_locked_or_snapshotted ();
346351 return (_deps_dyn != nullptr ? _deps_dyn->adr_at (i)
347352 : _deps != nullptr ? _deps->adr_at (i)
348353 : nullptr );
349354 }
350355 E at (int i) const {
356+ TrainingDataLocker::assert_locked_or_snapshotted ();
351357 assert (i >= 0 && i < length (), " oob" );
352358 return *adr_at (i);
353359 }
354360 bool append_if_missing (E dep) {
361+ TrainingDataLocker::assert_can_add ();
355362 if (_deps_dyn == nullptr ) {
356363 _deps_dyn = new GrowableArrayCHeap<E, mtCompiler>(10 );
357364 _deps_dyn->append (dep);
@@ -361,23 +368,27 @@ class TrainingData : public Metadata {
361368 }
362369 }
363370 bool remove_if_existing (E dep) {
371+ TrainingDataLocker::assert_can_add ();
364372 if (_deps_dyn != nullptr ) {
365373 return _deps_dyn->remove_if_existing (dep);
366374 }
367375 return false ;
368376 }
369377 void clear () {
378+ TrainingDataLocker::assert_can_add ();
370379 if (_deps_dyn != nullptr ) {
371380 _deps_dyn->clear ();
372381 }
373382 }
374383 void append (E dep) {
384+ TrainingDataLocker::assert_can_add ();
375385 if (_deps_dyn == nullptr ) {
376386 _deps_dyn = new GrowableArrayCHeap<E, mtCompiler>(10 );
377387 }
378388 _deps_dyn->append (dep);
379389 }
380390 bool contains (E dep) {
391+ TrainingDataLocker::assert_locked ();
381392 for (int i = 0 ; i < length (); i++) {
382393 if (dep == at (i)) {
383394 return true ; // found
@@ -591,6 +602,7 @@ class CompileTrainingData : public TrainingData {
591602 DepList<Record> _data;
592603 public:
593604 OptionalReturnType find (const Args&... args) {
605+ TrainingDataLocker l;
594606 ArgumentsType a (args...);
595607 for (int i = 0 ; i < _data.length (); i++) {
596608 if (_data.at (i).arguments () == a) {
@@ -599,8 +611,11 @@ class CompileTrainingData : public TrainingData {
599611 }
600612 return OptionalReturnType (false , ReturnType ());
601613 }
602- bool append_if_missing (const ReturnType& result, const Args&... args) {
603- return _data.append_if_missing (Record (result, ArgumentsType (args...)));
614+ void append_if_missing (const ReturnType& result, const Args&... args) {
615+ TrainingDataLocker l;
616+ if (l.can_add ()) {
617+ _data.append_if_missing (Record (result, ArgumentsType (args...)));
618+ }
604619 }
605620#if INCLUDE_CDS
606621 void remove_unshareable_info () { _data.remove_unshareable_info (); }
0 commit comments