Skip to content

Commit 110462d

Browse files
authored
Documentation for Custom Assertion Handler (#1873)
1 parent 5232315 commit 110462d

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. _assertion_handler:
2+
3+
Custom Assertion Handler
4+
========================
5+
6+
.. note::
7+
The availability of the extension can be checked with the ``TBB_EXT_CUSTOM_ASSERTION_HANDLER`` macro defined in
8+
``oneapi/tbb/global_control.h`` and ``oneapi/tbb/version.h`` headers.
9+
10+
.. contents::
11+
:local:
12+
:depth: 2
13+
14+
Description
15+
***********
16+
17+
oneTBB implements assertion checking that detects errors in header files and library code. While most assertions are
18+
active in debug builds (controlled by ``TBB_USE_ASSERT``), some assertions remain present in release builds. By
19+
default, failed assertions display an error message and terminate the application. The custom assertion handler
20+
mechanism extends this by allowing developers to implement their own assertion handling functions. The API is
21+
semantically similar to the standard ``std::set_terminate`` and ``std::get_terminate`` functions.
22+
23+
API
24+
***
25+
26+
Header
27+
------
28+
29+
.. code:: cpp
30+
31+
#include <oneapi/tbb/global_control.h>
32+
33+
Synopsis
34+
--------
35+
36+
.. code:: cpp
37+
38+
#define TBB_EXT_CUSTOM_ASSERTION_HANDLER 202510
39+
40+
namespace oneapi {
41+
namespace tbb {
42+
namespace ext {
43+
using assertion_handler_type = void(*)(const char* location, int line,
44+
const char* expression, const char* comment);
45+
46+
assertion_handler_type set_assertion_handler(assertion_handler_type new_handler) noexcept;
47+
48+
assertion_handler_type get_assertion_handler() noexcept;
49+
}}}
50+
51+
Types
52+
-----
53+
54+
.. cpp:type:: assertion_handler_type
55+
56+
Type alias for the pointer to an assertion handler function.
57+
58+
Functions
59+
---------
60+
61+
.. cpp:function:: assertion_handler_type set_assertion_handler(assertion_handler_type new_handler) noexcept
62+
63+
Sets the provided assertion handler and returns the previous handler. If ``new_handler`` is ``nullptr``, resets to the
64+
default handler.
65+
66+
.. note:: ``new_handler`` must not return. If it does, the behavior is undefined.
67+
68+
.. cpp:function:: assertion_handler_type get_assertion_handler() noexcept
69+
70+
Returns the current assertion handler.
71+
72+
Example
73+
*******
74+
75+
.. literalinclude:: ./examples/assertion_handler.cpp
76+
:language: c++
77+
:start-after: /*begin_assertion_handler_example*/
78+
:end-before: /*end_assertion_handler_example*/
79+
80+
.. rubric:: See also
81+
82+
* `Enabling Debugging Features specification <https://oneapi-spec.uxlfoundation.org/specifications/oneapi/v1.4-rev-1/elements/onetbb/source/configuration/enabling_debugging_features>`_
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright (c) 2025 Intel Corporation
3+
Copyright (c) 2025 UXL Foundation Contributors
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
/*begin_assertion_handler_example*/
19+
#include <oneapi/tbb/global_control.h>
20+
#include <fstream>
21+
22+
#ifdef TBB_EXT_CUSTOM_ASSERTION_HANDLER
23+
24+
auto default_handler = tbb::ext::get_assertion_handler();
25+
26+
void wrapper_assertion_handler(const char* location, int line,
27+
const char* expression, const char* comment) {
28+
// Execute a custom step before the default handler: log the assertion
29+
std::ofstream log{"example.log", std::ios::app};
30+
log << "Function: " << location << ", line: " << line << ", assertion "
31+
<< expression << " failed: " << comment << "\n";
32+
log.close();
33+
34+
default_handler(location, line, expression, comment);
35+
}
36+
37+
#endif // TBB_EXT_CUSTOM_ASSERTION_HANDLER
38+
39+
int main() {
40+
#ifdef TBB_EXT_CUSTOM_ASSERTION_HANDLER
41+
// Set custom handler
42+
tbb::ext::set_assertion_handler(wrapper_assertion_handler);
43+
#endif // TBB_EXT_CUSTOM_ASSERTION_HANDLER
44+
45+
// Use oneTBB normally - any assertion failures will use custom handler
46+
// ...
47+
48+
#ifdef TBB_EXT_CUSTOM_ASSERTION_HANDLER
49+
// Restore the default handler
50+
tbb::ext::set_assertion_handler(nullptr);
51+
#endif // TBB_EXT_CUSTOM_ASSERTION_HANDLER
52+
}
53+
/*end_assertion_handler_example*/

doc/main/reference/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ It also describes features that are not included in the oneTBB specification.
1818

1919
scalable_memory_pools/malloc_replacement_log
2020
rvalue_reduce
21+
assertion_handler
2122

2223
Preview features
2324
****************

0 commit comments

Comments
 (0)