1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_assert_impl_H
18 #define __TBB_assert_impl_H
19 
20 #include "oneapi/tbb/detail/_config.h"
21 #include "oneapi/tbb/detail/_utils.h"
22 
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include <cstdarg>
27 #if _MSC_VER && _DEBUG
28 #include <crtdbg.h>
29 #endif
30 
31 #include <mutex>
32 
33 #if __TBBMALLOC_BUILD
34 namespace rml { namespace internal {
35 #else
36 namespace tbb {
37 namespace detail {
38 namespace r1 {
39 #endif
40 // TODO: consider extension for formatted error description string
assertion_failure_impl(const char * location,int line,const char * expression,const char * comment)41 static void assertion_failure_impl(const char* location, int line, const char* expression, const char* comment) {
42 
43     std::fprintf(stderr, "Assertion %s failed (located in the %s function, line in file: %d)\n",
44         expression, location, line);
45 
46     if (comment) {
47         std::fprintf(stderr, "Detailed description: %s\n", comment);
48     }
49 #if _MSC_VER && _DEBUG
50     if (1 == _CrtDbgReport(_CRT_ASSERT, location, line, "tbb_debug.dll", "%s\r\n%s", expression, comment?comment:"")) {
51         _CrtDbgBreak();
52     } else
53 #endif
54     {
55         std::fflush(stderr);
56         std::abort();
57     }
58 }
59 
60 // Do not move the definition into the assertion_failure function because it will require "magic statics".
61 // It will bring a dependency on C++ runtime on some platforms while assert_impl.h is reused in tbbmalloc
62 // that should not depend on C++ runtime
63 static std::atomic<tbb::detail::do_once_state> assertion_state;
64 
assertion_failure(const char * location,int line,const char * expression,const char * comment)65 void __TBB_EXPORTED_FUNC assertion_failure(const char* location, int line, const char* expression, const char* comment) {
66 #if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
67     // Workaround for erroneous "unreachable code" during assertion throwing using call_once
68     #pragma warning (push)
69     #pragma warning (disable: 4702)
70 #endif
71     // We cannot use std::call_once because it brings a dependency on C++ runtime on some platforms
72     // while assert_impl.h is reused in tbbmalloc that should not depend on C++ runtime
73     atomic_do_once([&](){ assertion_failure_impl(location, line, expression, comment); }, assertion_state);
74 #if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
75     #pragma warning (pop)
76 #endif
77 }
78 
79 //! Report a runtime warning.
runtime_warning(const char * format,...)80 void runtime_warning( const char* format, ... ) {
81     char str[1024]; std::memset(str, 0, 1024);
82     va_list args; va_start(args, format);
83     vsnprintf( str, 1024-1, format, args);
84     va_end(args);
85     fprintf(stderr, "TBB Warning: %s\n", str);
86 }
87 
88 #if __TBBMALLOC_BUILD
89 }} // namespaces rml::internal
90 #else
91 } // namespace r1
92 } // namespace detail
93 } // namespace tbb
94 #endif
95 
96 #endif // __TBB_assert_impl_H
97 
98