// ---------------------------------------------------------------------- // File: RRSeed // Author: Abhishek Lekshmanan - CERN // ---------------------------------------------------------------------- /************************************************************************ * EOS - the CERN Disk Storage System * * Copyright (C) 2023 CERN/Switzerland * * * * This program 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 of the License, or * * (at your option) any later version. * * * * This program 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 this program. If not, see .* ************************************************************************/ #ifndef EOS_RRSEED_HH #define EOS_RRSEED_HH #include #include #include namespace eos::mgm::placement { // A copyable atomic type! DO NOT use this as a synchronization primitive! // This is only meant for storing a seed value for a random number generator // wherein we only use this at the initialization phase to put onto a vector! // Do not copy atomic types when you are using them to synchronize values! template struct AtomicWrapper { AtomicWrapper(T t): value(t) {} AtomicWrapper(const AtomicWrapper& other): value(other.value.load()) {} std::atomic value; }; // A simple round robin seed generator, stored as a list of atomic values, // the use case of the list is when you'd need a 2-D round robin and you'd // need to RR over the 2nd dimension. Under the hood this is nothing but just // a 1-D counter incremented to a given size. template class RRSeed { public: // Currently the counter will wrap around to 0 if it reaches the max value // of type T, as is defined for unsigned integers. If you need to -ve values // rewrite carefully considering overflows! static_assert(std::is_integral::value && std::is_unsigned::value, "We expect only unsigned integer types, " "otherwise overflow would be Undefined Behaviour"); // Initialization is not TS: we assume that this is only called once!!! explicit RRSeed(size_t max_items) : mSeeds(max_items, 0) {} // Get a seed at an index, also reserve n_items, so that the next seed is n_items // away. Please ensure the index is within range! T get(size_t index, size_t n_items) { return mSeeds.at(index).value.fetch_add(n_items, std::memory_order_relaxed); } size_t getNumSeeds() const { return mSeeds.size(); } private: std::vector> mSeeds; }; } #endif // EOS_RRSEED_HH