1 /** 2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved. 3 License: MPL-2 4 Author: Joakim Brännström (joakim.brannstrom@gmx.com) 5 6 This Source Code Form is subject to the terms of the Mozilla Public License, 7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain 8 one at http://mozilla.org/MPL/2.0/. 9 10 Contains facilities to generate globally unique numbers. 11 */ 12 module cpptooling.utility.global_unique; 13 14 private shared(size_t) _nextSequence; 15 16 static this() { 17 // Use a fixed number to minimize the difference between two sets of 18 // generated data. 19 // 20 // Keeping it fixed to make it easier to debug, read the logs. Aka 21 // reproduce the result. 22 // 23 // It is extremly important to minimize differences. 24 // Diffs are used as the basis to evaluate changes. 25 // No diff, no evaluation needed from an architectural point of view. 26 // A change? Further inspection needed. 27 _nextSequence = 42; 28 } 29 30 size_t nextNumber() @trusted nothrow { 31 import core.atomic; 32 33 size_t rval; 34 35 synchronized { 36 if (_nextSequence == size_t.max) { 37 _nextSequence = size_t.min; 38 } 39 40 core.atomic.atomicOp!"+="(_nextSequence, 1); 41 rval = _nextSequence; 42 } 43 44 return rval; 45 }