1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
//
// MIT License
// Copyright (c) 2020 Jonathan R. Madsen
// 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
// 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.
//
// Tasking native types
//
#pragma once
#include "PTL/Config.hh"
#if defined(__APPLE__) || defined(__MACH__)
# if !defined(PTL_MACOS)
# define PTL_MACOS 1
# endif
# if !defined(PTL_UNIX)
# define PTL_UNIX 1
# endif
#endif
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
# if !defined(PTL_WINDOWS)
# define PTL_WINDOWS 1
# endif
#endif
#if defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
# if !defined(PTL_LINUX)
# define PTL_LINUX 1
# endif
# if !defined(PTL_UNIX)
# define PTL_UNIX 1
# endif
#endif
#if defined(__unix__) || defined(__unix) || defined(unix)
# if !defined(PTL_UNIX)
# define PTL_UNIX 1
# endif
#endif
#if defined(PTL_WINDOWS)
// Disable warning C4786 on WIN32 architectures:
// identifier was truncated to '255' characters
// in the debug information
//
# pragma warning(disable : 4786)
//
// Define DLL export macro for WIN32 systems for
// importing/exporting external symbols to DLLs
//
# if defined LIB_BUILD_DLL
# define DLLEXPORT __declspec(dllexport)
# define DLLIMPORT __declspec(dllimport)
# else
# define DLLEXPORT
# define DLLIMPORT
# endif
//
// Unique identifier for global module
//
# if defined PTL_ALLOC_EXPORT
# define PTL_DLL DLLEXPORT
# else
# define PTL_DLL DLLIMPORT
# endif
#else
# define DLLEXPORT
# define DLLIMPORT
# define PTL_DLL
#endif
#if !defined(PTL_DEFAULT_OBJECT)
# define PTL_DEFAULT_OBJECT(NAME) \
NAME() = default; \
~NAME() = default; \
NAME(const NAME&) = default; \
NAME(NAME&&) = default; \
NAME& operator=(const NAME&) = default; \
NAME& operator=(NAME&&) = default;
#endif
#include <atomic>
#include <complex>
#include <functional>
#include <limits>
#include <memory>
#include <utility>
namespace PTL
{
//--------------------------------------------------------------------------------------//
//
namespace api
{
struct native
{};
struct tbb
{};
} // namespace api
//
//--------------------------------------------------------------------------------------//
//
template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
typename Pair = std::pair<Ptr, Ptr>>
Pair&
GetSharedPointerPair()
{
static auto _master = std::make_shared<Tp>();
static std::atomic<int64_t> _count(0);
static thread_local auto _inst =
Pair(_master, Ptr((_count++ == 0) ? nullptr : new Tp()));
return _inst;
}
//
//--------------------------------------------------------------------------------------//
//
template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
typename Pair = std::pair<Ptr, Ptr>>
Ptr
GetSharedPointerPairInstance()
{
static thread_local auto& _pinst = GetSharedPointerPair<Tp, Tag>();
static thread_local auto& _inst = _pinst.second.get() ? _pinst.second : _pinst.first;
return _inst;
}
//
//--------------------------------------------------------------------------------------//
//
template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
typename Pair = std::pair<Ptr, Ptr>>
Ptr
GetSharedPointerPairMasterInstance()
{
static auto& _pinst = GetSharedPointerPair<Tp, Tag>();
static auto _inst = _pinst.first;
return _inst;
}
//======================================================================================//
} // namespace PTL
// Forward declation of void type argument for usage in direct object
// persistency to define fake default constructors
//
class __void__;
|