1*4882a593SmuzhiyunFrom 7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: jzmaddock <john@johnmaddock.co.uk>
3*4882a593SmuzhiyunDate: Wed, 1 Sep 2021 20:31:53 +0100
4*4882a593SmuzhiyunSubject: [PATCH] Make no atomics a soft failure in bernoulli_details.hpp.
5*4882a593Smuzhiyun Include an "escape macro" so thread safety can be disabled if certain
6*4882a593Smuzhiyun bernoulli features are to be used in a no-atomics environment. Fixes
7*4882a593Smuzhiyun https://github.com/boostorg/math/issues/673.
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun[buildroot@heine.tech:
10*4882a593Smuzhiyun  - backport from boostorg/math 7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b
11*4882a593Smuzhiyun  - alter path to match boost release
12*4882a593Smuzhiyun]
13*4882a593SmuzhiyunSigned-off-by: Michael Nosthoff <buildroot@heine.tech>
14*4882a593Smuzhiyun---
15*4882a593Smuzhiyun .../detail/bernoulli_details.hpp                 | 10 +++++++---
16*4882a593Smuzhiyun libs/math/test/Jamfile.v2                        |  3 +++
17*4882a593Smuzhiyun test/compile_test/bernoulli_no_atomic_d.cpp      | 14 ++++++++++++++
18*4882a593Smuzhiyun test/compile_test/bernoulli_no_atomic_fail.cpp   | 15 +++++++++++++++
19*4882a593Smuzhiyun test/compile_test/bernoulli_no_atomic_mp.cpp     | 16 ++++++++++++++++
20*4882a593Smuzhiyun 5 files changed, 55 insertions(+), 3 deletions(-)
21*4882a593Smuzhiyun create mode 100644 test/compile_test/bernoulli_no_atomic_d.cpp
22*4882a593Smuzhiyun create mode 100644 test/compile_test/bernoulli_no_atomic_fail.cpp
23*4882a593Smuzhiyun create mode 100644 test/compile_test/bernoulli_no_atomic_mp.cpp
24*4882a593Smuzhiyun
25*4882a593Smuzhiyundiff --git a/boost/math/special_functions/detail/bernoulli_details.hpp b/boost/math/special_functions/detail/bernoulli_details.hpp
26*4882a593Smuzhiyunindex cf35545264..8519b7c89c 100644
27*4882a593Smuzhiyun--- a/boost/math/special_functions/detail/bernoulli_details.hpp
28*4882a593Smuzhiyun+++ b/boost/math/special_functions/detail/bernoulli_details.hpp
29*4882a593Smuzhiyun@@ -360,7 +360,7 @@ class bernoulli_numbers_cache
30*4882a593Smuzhiyun          return out;
31*4882a593Smuzhiyun       }
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun-      #ifndef BOOST_HAS_THREADS
34*4882a593Smuzhiyun+      #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
35*4882a593Smuzhiyun       //
36*4882a593Smuzhiyun       // Single threaded code, very simple:
37*4882a593Smuzhiyun       //
38*4882a593Smuzhiyun@@ -382,6 +382,8 @@ class bernoulli_numbers_cache
39*4882a593Smuzhiyun          *out = (i >= m_overflow_limit) ? policies::raise_overflow_error<T>("boost::math::bernoulli_b2n<%1%>(std::size_t)", 0, T(i), pol) : bn[i];
40*4882a593Smuzhiyun          ++out;
41*4882a593Smuzhiyun       }
42*4882a593Smuzhiyun+      #elif defined(BOOST_MATH_NO_ATOMIC_INT)
43*4882a593Smuzhiyun+      static_assert(sizeof(T) == 1, "Unsupported configuration: your platform appears to have no atomic integers.  If you are happy with thread-unsafe code, then you may define BOOST_MATH_BERNOULLI_UNTHREADED to suppress this error.");
44*4882a593Smuzhiyun       #else
45*4882a593Smuzhiyun       //
46*4882a593Smuzhiyun       // Double-checked locking pattern, lets us access cached already cached values
47*4882a593Smuzhiyun@@ -464,7 +466,7 @@ class bernoulli_numbers_cache
48*4882a593Smuzhiyun          return out;
49*4882a593Smuzhiyun       }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun-      #ifndef BOOST_HAS_THREADS
52*4882a593Smuzhiyun+      #if !defined(BOOST_HAS_THREADS) || defined(BOOST_MATH_BERNOULLI_UNTHREADED)
53*4882a593Smuzhiyun       //
54*4882a593Smuzhiyun       // Single threaded code, very simple:
55*4882a593Smuzhiyun       //
56*4882a593Smuzhiyun@@ -494,6 +496,8 @@ class bernoulli_numbers_cache
57*4882a593Smuzhiyun          }
58*4882a593Smuzhiyun          ++out;
59*4882a593Smuzhiyun       }
60*4882a593Smuzhiyun+      #elif defined(BOOST_MATH_NO_ATOMIC_INT)
61*4882a593Smuzhiyun+      static_assert(sizeof(T) == 1, "Unsupported configuration: your platform appears to have no atomic integers.  If you are happy with thread-unsafe code, then you may define BOOST_MATH_BERNOULLI_UNTHREADED to suppress this error.");
62*4882a593Smuzhiyun       #else
63*4882a593Smuzhiyun       //
64*4882a593Smuzhiyun       // Double-checked locking pattern, lets us access cached already cached values
65*4882a593Smuzhiyun@@ -555,7 +559,7 @@ class bernoulli_numbers_cache
66*4882a593Smuzhiyun    // The value at which we know overflow has already occurred for the Bn:
67*4882a593Smuzhiyun    std::size_t m_overflow_limit;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun-   #ifdef BOOST_HAS_THREADS
70*4882a593Smuzhiyun+   #if defined(BOOST_HAS_THREADS) && !defined(BOOST_MATH_NO_ATOMIC_INT)
71*4882a593Smuzhiyun    std::mutex m_mutex;
72*4882a593Smuzhiyun    atomic_counter_type m_counter, m_current_precision;
73*4882a593Smuzhiyun    #else
74*4882a593Smuzhiyundiff --git a/libs/math/test/Jamfile.v2 b/libs/math/test/Jamfile.v2
75*4882a593Smuzhiyunindex 52fb87f5e5..3ac63f9279 100644
76*4882a593Smuzhiyun--- a/libs/math/test/Jamfile.v2
77*4882a593Smuzhiyun+++ b/libs/math/test/Jamfile.v2
78*4882a593Smuzhiyun@@ -1137,6 +1137,9 @@ test-suite misc :
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #   [ run __temporary_test.cpp test_instances//test_instances : : : <test-info>always_show_run_output <pch>off ]
81*4882a593Smuzhiyun    [ compile test_no_long_double_policy.cpp ]
82*4882a593Smuzhiyun+   [ compile compile_test/bernoulli_no_atomic_d.cpp ]
83*4882a593Smuzhiyun+   [ compile compile_test/bernoulli_no_atomic_mp.cpp ]
84*4882a593Smuzhiyun+   [ compile-fail compile_test/bernoulli_no_atomic_fail.cpp ]
85*4882a593Smuzhiyun ;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun test-suite interpolators :
88*4882a593Smuzhiyundiff --git a/test/compile_test/bernoulli_no_atomic_d.cpp b/test/compile_test/bernoulli_no_atomic_d.cpp
89*4882a593Smuzhiyunnew file mode 100644
90*4882a593Smuzhiyunindex 0000000000..61926f7e1f
91*4882a593Smuzhiyun--- /dev/null
92*4882a593Smuzhiyun+++ b/test/compile_test/bernoulli_no_atomic_d.cpp
93*4882a593Smuzhiyun@@ -0,0 +1,14 @@
94*4882a593Smuzhiyun+//  (C) Copyright John Maddock 2021.
95*4882a593Smuzhiyun+//  Use, modification and distribution are subject to the
96*4882a593Smuzhiyun+//  Boost Software License, Version 1.0. (See accompanying file
97*4882a593Smuzhiyun+//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
98*4882a593Smuzhiyun+
99*4882a593Smuzhiyun+#define BOOST_MATH_NO_ATOMIC_INT
100*4882a593Smuzhiyun+
101*4882a593Smuzhiyun+#include <boost/math/special_functions/bernoulli.hpp>
102*4882a593Smuzhiyun+#include "test_compile_result.hpp"
103*4882a593Smuzhiyun+
104*4882a593Smuzhiyun+void compile_and_link_test()
105*4882a593Smuzhiyun+{
106*4882a593Smuzhiyun+   check_result<double>(boost::math::bernoulli_b2n<double>(4));
107*4882a593Smuzhiyun+}
108*4882a593Smuzhiyundiff --git a/test/compile_test/bernoulli_no_atomic_fail.cpp b/test/compile_test/bernoulli_no_atomic_fail.cpp
109*4882a593Smuzhiyunnew file mode 100644
110*4882a593Smuzhiyunindex 0000000000..bbd7152412
111*4882a593Smuzhiyun--- /dev/null
112*4882a593Smuzhiyun+++ b/test/compile_test/bernoulli_no_atomic_fail.cpp
113*4882a593Smuzhiyun@@ -0,0 +1,15 @@
114*4882a593Smuzhiyun+//  (C) Copyright John Maddock 2021.
115*4882a593Smuzhiyun+//  Use, modification and distribution are subject to the
116*4882a593Smuzhiyun+//  Boost Software License, Version 1.0. (See accompanying file
117*4882a593Smuzhiyun+//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
118*4882a593Smuzhiyun+
119*4882a593Smuzhiyun+#define BOOST_MATH_NO_ATOMIC_INT
120*4882a593Smuzhiyun+
121*4882a593Smuzhiyun+#include <boost/math/special_functions/bernoulli.hpp>
122*4882a593Smuzhiyun+#include <boost/multiprecision/cpp_bin_float.hpp>
123*4882a593Smuzhiyun+#include "test_compile_result.hpp"
124*4882a593Smuzhiyun+
125*4882a593Smuzhiyun+void compile_and_link_test()
126*4882a593Smuzhiyun+{
127*4882a593Smuzhiyun+   check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
128*4882a593Smuzhiyun+}
129*4882a593Smuzhiyundiff --git a/test/compile_test/bernoulli_no_atomic_mp.cpp b/test/compile_test/bernoulli_no_atomic_mp.cpp
130*4882a593Smuzhiyunnew file mode 100644
131*4882a593Smuzhiyunindex 0000000000..8d5a6e78e6
132*4882a593Smuzhiyun--- /dev/null
133*4882a593Smuzhiyun+++ b/test/compile_test/bernoulli_no_atomic_mp.cpp
134*4882a593Smuzhiyun@@ -0,0 +1,16 @@
135*4882a593Smuzhiyun+//  (C) Copyright John Maddock 2021.
136*4882a593Smuzhiyun+//  Use, modification and distribution are subject to the
137*4882a593Smuzhiyun+//  Boost Software License, Version 1.0. (See accompanying file
138*4882a593Smuzhiyun+//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
139*4882a593Smuzhiyun+
140*4882a593Smuzhiyun+#define BOOST_MATH_NO_ATOMIC_INT
141*4882a593Smuzhiyun+#define BOOST_MATH_BERNOULLI_UNTHREADED
142*4882a593Smuzhiyun+
143*4882a593Smuzhiyun+#include <boost/math/special_functions/bernoulli.hpp>
144*4882a593Smuzhiyun+#include <boost/multiprecision/cpp_bin_float.hpp>
145*4882a593Smuzhiyun+#include "test_compile_result.hpp"
146*4882a593Smuzhiyun+
147*4882a593Smuzhiyun+void compile_and_link_test()
148*4882a593Smuzhiyun+{
149*4882a593Smuzhiyun+   check_result<boost::multiprecision::cpp_bin_float_50>(boost::math::bernoulli_b2n<boost::multiprecision::cpp_bin_float_50>(4));
150*4882a593Smuzhiyun+}
151