/**************************************************************************/ /* safe_refcount.h */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #ifndef SAFE_REFCOUNT_H #define SAFE_REFCOUNT_H #include "core/typedefs.h" #ifdef DEV_ENABLED #include "core/error/error_macros.h" #endif #include <atomic> #include <type_traits> // Design goals for these classes: // - No automatic conversions or arithmetic operators, // to keep explicit the use of atomics everywhere. // - Using acquire-release semantics, even to set the first value. // The first value may be set relaxedly in many cases, but adding the distinction // between relaxed and unrelaxed operation to the interface would make it needlessly // flexible. There's negligible waste in having release semantics for the initial // value and, as an important benefit, you can be sure the value is properly synchronized // even with threads that are already running. // These are used in very specific areas of the engine where it's critical that these guarantees are held #define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) … #define SAFE_FLAG_TYPE_PUN_GUARANTEES … template <typename T> class SafeNumeric { … }; class SafeFlag { … }; class SafeRefCount { … }; #endif // SAFE_REFCOUNT_H