Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:48

0001 // Author: Enrico Guiraud, Danilo Piparo CERN  03/2017
0002 
0003 /*************************************************************************
0004  * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers.               *
0005  * All rights reserved.                                                  *
0006  *                                                                       *
0007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0009  *************************************************************************/
0010 
0011 #ifndef ROOT_RSLOTSTACK
0012 #define ROOT_RSLOTSTACK
0013 
0014 #include <ROOT/TSpinMutex.hxx>
0015 
0016 #include <stack>
0017 
0018 namespace ROOT {
0019 namespace Internal {
0020 
0021 /// A thread-safe stack of N indexes (0 to size - 1).
0022 /// RSlotStack can be used to safely assign a "processing slot" number to
0023 /// each thread in multi-thread applications.
0024 /// In release builds, pop and push operations are unchecked, potentially
0025 /// resulting in undefined behavior if more slot numbers than available are
0026 /// requested.
0027 /// An important design assumption is that a slot will almost always be available
0028 /// when a thread asks for it, and if it is not available it will be very soon,
0029 /// therefore a spinlock is used for synchronization.
0030 class RSlotStack {
0031 private:
0032    const unsigned int fSize;
0033    std::stack<unsigned int> fStack;
0034    ROOT::TSpinMutex fMutex;
0035 
0036 public:
0037    RSlotStack() = delete;
0038    RSlotStack(unsigned int size);
0039    void ReturnSlot(unsigned int slotNumber);
0040    unsigned int GetSlot();
0041 };
0042 
0043 /// A RAII object to pop and push slot numbers from a RSlotStack object.
0044 /// After construction the slot number is available as the data member fSlot.
0045 struct RSlotStackRAII {
0046    ROOT::Internal::RSlotStack &fSlotStack;
0047    unsigned int fSlot;
0048    RSlotStackRAII(ROOT::Internal::RSlotStack &slotStack) : fSlotStack(slotStack), fSlot(slotStack.GetSlot()) {}
0049    ~RSlotStackRAII() { fSlotStack.ReturnSlot(fSlot); }
0050 };
0051 
0052 } // namespace Internal
0053 } // namespace ROOT
0054 
0055 #endif