ATLAS Offline Software
RCUObject.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 */
118 #ifndef ATHENAKERNEL_RCUOBJECT_H
119 #define ATHENAKERNEL_RCUOBJECT_H
120 
121 
122 #include "GaudiKernel/ThreadLocalContext.h"
123 #include "GaudiKernel/EventContext.h"
124 #include "boost/dynamic_bitset.hpp"
125 #include <atomic>
126 #include <deque>
127 #include <vector>
128 #include <memory>
129 #include <mutex>
130 
131 
132 namespace Athena {
133 
134 
135 class IRCUSvc;
136 template <class T> class RCURead;
137 template <class T> class RCUReadQuiesce;
138 template <class T> class RCUUpdate;
139 
140 
141 
146 {
147 public:
151 
152 
160 
161 
168  IRCUObject (size_t nslots);
169 
170 
177 
178 
179  // Make these explicit to prevent warnings from coverity.
180  // (copy/assign are implicitly deleted due to the mutex member.)
181  IRCUObject (const IRCUObject&) = delete;
182  IRCUObject& operator= (const IRCUObject&) = delete;
184 
185 
191  virtual ~IRCUObject();
192 
193 
199  void quiescent();
200 
201 
208  void quiescent (const EventContext& ctx);
209 
210 
211 protected:
213  typedef std::unique_lock<mutex_t> lock_t;
214 
215 
225  bool endGrace (lock_t& lock,
226  const EventContext& ctx);
227 
228 
235  void setGrace (lock_t& /*lock*/);
236 
237 
242 
243 
250  virtual void clearAll (lock_t& /*lock*/) = 0;
251 
252 
261  virtual bool clearOld (lock_t& /*lock*/, size_t nold) = 0;
262 
263 
273  void makeOld (lock_t& lock, size_t garbageSize);
274 
275 
276 private:
287  bool endGrace (lock_t& /*lock*/,
288  const EventContext& ctx,
289  boost::dynamic_bitset<>& grace) const;
290 
291 
294 
297 
299  boost::dynamic_bitset<> m_grace;
300 
302  boost::dynamic_bitset<> m_oldGrace;
303 
306  size_t m_nold;
307 
310  std::atomic<bool> m_dirty;
311 };
312 
313 
319 template <class T>
321  : public IRCUObject
322 {
323 public:
327 
328 
336  template <typename... Args>
338 
339 
345  template <typename... Args>
346  RCUObject (size_t nslots, Args&&... args);
347 
348 
357 
358 
365 
366 
367  // Make these explicit to prevent warnings from coverity.
368  // (copy/assign are implicitly deleted due to the mutex member.)
369  RCUObject (const RCUObject&) = delete;
370  RCUObject& operator= (const RCUObject&) = delete;
372 
373 
377  Read_t reader() const;
378 
379 
388 
389 
396  ReadQuiesce_t readerQuiesce (const EventContext& ctx);
397  ReadQuiesce_t readerQuiesce (const EventContext&& ctx) = delete;
398 
399 
406 
407 
412  Update_t updater (const EventContext& ctx);
413  Update_t updater (const EventContext&& ctx) = delete;
414 
415 
426  void discard (std::unique_ptr<const T> p);
427 
428 
429 protected:
435  void discard (lock_t& /*lock*/, std::unique_ptr<const T> p);
436 
437 
438 private:
445  virtual void clearAll (lock_t& /*lock*/) override;
446 
447 
456  virtual bool clearOld (lock_t& /*lock*/, size_t nold) override;
457 
458 
459 private:
460  template <class U> friend class RCUReadQuiesce;
461  template <class U> friend class RCURead;
462  template <class U> friend class RCUUpdate;
463 
465  std::unique_ptr<T> m_objref;
466 
468  std::atomic<const T*> m_obj;
469 
471  std::deque<std::unique_ptr<const T> > m_garbage;
472 };
473 
474 
480 template <class T>
481 class RCURead
482 {
483 public:
488  RCURead (const RCUObject<T>& rcuobj);
489 
490 
491  operator bool() const { return m_obj != nullptr; }
492 
493 
497  const T& operator*() const;
498 
499 
503  const T* operator->() const;
504 
505 
506 private:
508  const T* m_obj;
509 };
510 
511 
519 template <class T>
520 class RCUReadQuiesce
521  : public RCURead<T>
522 {
523 public:
531 
532 
538  RCUReadQuiesce (RCUObject<T>& rcuobj, const EventContext& ctx);
539  RCUReadQuiesce (RCUObject<T>& rcuobj, const EventContext&& ctx) = delete;
540 
541 
548 
549 
550 private:
553 
555  const EventContext& m_ctx;
556 };
557 
558 
564 template <class T>
565 class RCUUpdate
566 {
567 public:
575 
576 
582  RCUUpdate (RCUObject<T>& rcuobj, const EventContext& ctx);
583  RCUUpdate (RCUObject<T>& rcuobj, const EventContext&& ctx) = delete;
584 
585 
589  const T& operator*() const;
590 
591 
595  const T* operator->() const;
596 
597 
602  void update (std::unique_ptr<T> ptr);
603 
604 
605 private:
608 
610  const EventContext& m_ctx;
611 
613  std::unique_lock<std::mutex> m_g;
614 };
615 
616 
617 } // namespace Athena
618 
619 
621 
622 
623 #endif // not ATHENAKERNEL_RCUOBJECT_H
Athena::RCUObject
Wrapper object controlled by RCU synchonization.
Definition: RCUObject.h:322
Athena::IRCUObject::m_dirty
std::atomic< bool > m_dirty
True if there are any objects pending deletion.
Definition: RCUObject.h:310
Athena::RCUObject::readerQuiesce
ReadQuiesce_t readerQuiesce()
Return a reader for this RCUObject.
Athena::RCUObject::RCUObject
RCUObject(IRCUSvc &svc, NoObjectEnum)
Constructor, with RCUSvc.
Athena::RCUUpdate::operator->
const T * operator->() const
Access data.
Athena::IRCUObject::IRCUObject
IRCUObject(IRCUSvc &svc)
Constructor, with RCUSvc.
Definition: RCUObject.cxx:26
Athena::RCUUpdate::update
void update(std::unique_ptr< T > ptr)
Publish a new version of the data object.
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
Athena::RCURead::operator*
const T & operator*() const
Access data.
Athena::RCUUpdate
Helper to update data in a RCUObject.
Definition: RCUObject.h:138
Athena::RCUObject::reader
Read_t reader() const
Return a reader for this RCUObject.
Athena::RCUObject::m_objref
std::unique_ptr< T > m_objref
Owning reference to the current object.
Definition: RCUObject.h:465
Athena::RCUObject::readerQuiesce
ReadQuiesce_t readerQuiesce(const EventContext &ctx)
Return a reader for this RCUObject.
Athena::IRCUObject::m_svc
IRCUSvc * m_svc
The service with which we're registered, or null.
Definition: RCUObject.h:296
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
Athena::RCUObject::Update_t
RCUUpdate< T > Update_t
Definition: RCUObject.h:326
Athena::IRCUObject::clearOld
virtual bool clearOld(lock_t &, size_t nold)=0
Delete old objects.
Athena::RCUObject::clearAll
virtual void clearAll(lock_t &) override
Delete all objects.
Athena::RCURead::RCURead
RCURead(const RCUObject< T > &rcuobj)
Constructor.
Athena::IRCUObject::quiescent
void quiescent(const EventContext &ctx)
Declare the event slot ctx of this object to be quiescent.
Athena::IRCUObject::~IRCUObject
virtual ~IRCUObject()
Destructor.
Definition: RCUObject.cxx:58
Athena::RCUReadQuiesce::RCUReadQuiesce
RCUReadQuiesce(RCUObject< T > &rcuobj, const EventContext &&ctx)=delete
Athena::RCUReadQuiesce::RCUReadQuiesce
RCUReadQuiesce(RCUObject< T > &rcuobj, const EventContext &ctx)
Constructor.
Athena::RCUObject::Read_t
RCURead< T > Read_t
Definition: RCUObject.h:324
Athena::RCUReadQuiesce::m_ctx
const EventContext & m_ctx
The current event context.
Definition: RCUObject.h:555
Athena::IRCUObject::mutex
mutex_t & mutex()
Return the mutex for this object.
Athena::IRCUObject::endGrace
bool endGrace(lock_t &, const EventContext &ctx, boost::dynamic_bitset<> &grace) const
Declare that the grace period for a slot is ending.
Athena::RCUObject::ReadQuiesce_t
RCUReadQuiesce< T > ReadQuiesce_t
Definition: RCUObject.h:325
Athena::IRCUObject::m_nold
size_t m_nold
Number of old objects.
Definition: RCUObject.h:306
Athena::RCUObject::updater
Update_t updater(const EventContext &ctx)
Return an updater for this RCUObject.
Args
Definition: test_lwtnn_fastgraph.cxx:12
Athena::RCUObject::m_garbage
std::deque< std::unique_ptr< const T > > m_garbage
List of objects pending cleanup.
Definition: RCUObject.h:471
Athena::IRCUObject::IRCUObject
IRCUObject(const IRCUObject &)=delete
Athena::RCUObject::clearOld
virtual bool clearOld(lock_t &, size_t nold) override
Delete old objects.
Athena::RCUObject::m_obj
std::atomic< const T * > m_obj
Pointer to the current object.
Definition: RCUObject.h:468
Athena::RCUUpdate::m_g
std::unique_lock< std::mutex > m_g
Lock for synchonization.
Definition: RCUObject.h:613
Athena::RCUReadQuiesce::m_rcuobj
RCUObject< T > & m_rcuobj
The RCUObject we're referencing.
Definition: RCUObject.h:552
Athena::RCUObject::RCUObject
RCUObject(const RCUObject &)=delete
Athena::RCUReadQuiesce::~RCUReadQuiesce
~RCUReadQuiesce()
Destructor.
Athena::IRCUObject::m_grace
boost::dynamic_bitset m_grace
Bit[i] set means that slot i is in a grace period.
Definition: RCUObject.h:299
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
Athena::RCUObject::discard
void discard(std::unique_ptr< const T > p)
Queue an object for later deletion.
Athena::RCUUpdate::RCUUpdate
RCUUpdate(RCUObject< T > &rcuobj, const EventContext &ctx)
Constructor.
Athena::RCURead
Helper to read data from a RCUObject.
Definition: RCUObject.h:136
Athena::IRCUObject::makeOld
void makeOld(lock_t &lock, size_t garbageSize)
Make existing pending objects old, if possible.
Athena::IRCUSvc
Interface for RCU service.
Definition: IRCUSvc.h:40
Athena::RCUObject::discard
void discard(lock_t &, std::unique_ptr< const T > p)
Queue an object for later deletion.
Athena::IRCUObject::m_mutex
std::mutex m_mutex
The mutex for this object.
Definition: RCUObject.h:293
Athena::IRCUObject::quiescent
void quiescent()
Declare the current event slot of this object to be quiescent.
Athena::IRCUObject::lock_t
std::unique_lock< mutex_t > lock_t
Definition: RCUObject.h:213
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
Athena::RCUUpdate::m_rcuobj
RCUObject< T > & m_rcuobj
The object we're referencing.
Definition: RCUObject.h:607
Athena::RCURead::operator->
const T * operator->() const
Access data.
Athena::IRCUObject::operator=
IRCUObject & operator=(const IRCUObject &)=delete
Athena::RCUUpdate::RCUUpdate
RCUUpdate(RCUObject< T > &rcuobj)
Constructor.
Athena::IRCUObject::NoObject
@ NoObject
Definition: RCUObject.h:150
Athena::RCUObject::RCUObject
RCUObject(RCUObject &&other)
Move constructor.
Athena::RCUUpdate::RCUUpdate
RCUUpdate(RCUObject< T > &rcuobj, const EventContext &&ctx)=delete
Athena::IRCUObject::setGrace
void setGrace(lock_t &)
Declare that all slots are in a grace period.
WriteCalibToCool.nold
nold
Definition: WriteCalibToCool.py:464
Athena::RCURead::m_obj
const T * m_obj
The data object we're reading.
Definition: RCUObject.h:508
Athena::RCUObject::readerQuiesce
ReadQuiesce_t readerQuiesce(const EventContext &&ctx)=delete
Athena::RCUObject::updater
Update_t updater()
Return an updater for this RCUObject.
Athena::RCUUpdate::m_ctx
const EventContext & m_ctx
The event context.
Definition: RCUObject.h:610
Athena::RCUObject::updater
Update_t updater(const EventContext &&ctx)=delete
Athena::IRCUObject
Base object class for RCU-style synchronization for Athena.
Definition: RCUObject.h:146
Athena::RCUObject::RCUObject
RCUObject(size_t nslots, Args &&... args)
Constructor, with number of slots.
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
Athena::IRCUObject::clearAll
virtual void clearAll(lock_t &)=0
Delete all objects.
Athena::IRCUObject::endGrace
bool endGrace(lock_t &lock, const EventContext &ctx)
Declare that the grace period for a slot is ending.
Athena::RCUObject::operator=
RCUObject & operator=(const RCUObject &)=delete
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
Athena::IRCUObject::mutex_t
std::mutex mutex_t
Definition: RCUObject.h:212
Athena::RCUReadQuiesce::RCUReadQuiesce
RCUReadQuiesce(RCUObject< T > &rcuobj)
Constructor.
Athena::RCUReadQuiesce
Helper to read data from a RCUObject.
Definition: RCUObject.h:137
python.CaloScaleNoiseConfig.args
args
Definition: CaloScaleNoiseConfig.py:80
Athena::RCUUpdate::operator*
const T & operator*() const
Access data.
RCUObject.icc
Athena::IRCUObject::m_oldGrace
boost::dynamic_bitset m_oldGrace
Same thing, for the objects marked as old.
Definition: RCUObject.h:302
Athena::IRCUObject::NoObjectEnum
NoObjectEnum
Value to pass to a constructor to mark that we shouldn't allocate an object.
Definition: RCUObject.h:150
Athena::RCUObject::RCUObject
RCUObject(IRCUSvc &svc, Args &&... args)
Constructor, with RCUSvc.