1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TASK_SEQUENCE_MANAGER_ENQUEUE_ORDER_GENERATOR_H_ 6 #define BASE_TASK_SEQUENCE_MANAGER_ENQUEUE_ORDER_GENERATOR_H_ 7 8 #include <stdint.h> 9 10 #include <atomic> 11 12 #include "base/base_export.h" 13 #include "base/task/sequence_manager/enqueue_order.h" 14 15 namespace base { 16 namespace sequence_manager { 17 namespace internal { 18 19 // EnqueueOrder can't be created from a raw number in non-test code. 20 // EnqueueOrderGenerator is used to create it with strictly monotonic guarantee. 21 class BASE_EXPORT EnqueueOrderGenerator { 22 public: 23 EnqueueOrderGenerator(); 24 EnqueueOrderGenerator(const EnqueueOrderGenerator&) = delete; 25 EnqueueOrderGenerator& operator=(const EnqueueOrderGenerator&) = delete; 26 ~EnqueueOrderGenerator(); 27 28 // Can be called from any thread. GenerateNext()29 EnqueueOrder GenerateNext() { 30 return EnqueueOrder(std::atomic_fetch_add_explicit( 31 &counter_, uint64_t(1), std::memory_order_relaxed)); 32 } 33 34 private: 35 std::atomic<uint64_t> counter_; 36 }; 37 38 } // namespace internal 39 } // namespace sequence_manager 40 } // namespace base 41 42 #endif // BASE_TASK_SEQUENCE_MANAGER_ENQUEUE_ORDER_GENERATOR_H_ 43