11#pragma once
2-
32#include < cstdint>
43#include < string>
4+ #include < unordered_map>
5+
6+ #include < scorep/SCOREP_User_Functions.h>
7+ #include < scorep/SCOREP_User_Variables.h>
58
69namespace scorepy
710{
11+
12+ struct region_handle
13+ {
14+ constexpr region_handle () = default;
15+ ~region_handle () = default ;
16+ constexpr bool operator ==(const region_handle& other)
17+ {
18+ return this ->value == other.value ;
19+ }
20+ constexpr bool operator !=(const region_handle& other)
21+ {
22+ return this ->value != other.value ;
23+ }
24+
25+ SCOREP_User_RegionHandle value = SCOREP_USER_INVALID_REGION;
26+ };
27+
28+ constexpr region_handle uninitialised_region_handle = region_handle();
29+
830// / Combine the arguments into a region name
931// / Return value is a statically allocated string to avoid memory (re)allocations
1032inline const std::string& make_region_name (const std::string& module_name, const std::string& name)
@@ -16,12 +38,48 @@ inline const std::string& make_region_name(const std::string& module_name, const
1638 return region;
1739}
1840
41+ extern std::unordered_map<std::uintptr_t , region_handle> regions;
42+
43+ /* * tries to enter a region. Return true on success
44+ *
45+ */
46+ inline bool try_region_begin (std::uintptr_t identifier)
47+ {
48+ auto it = regions.find (identifier);
49+ if (it != regions.end ())
50+ {
51+ SCOREP_User_RegionEnter (it->second .value );
52+ return true ;
53+ }
54+ else
55+ {
56+ return false ;
57+ }
58+ }
59+
1960void region_begin (const std::string& function_name, const std::string& module ,
2061 const std::string& file_name, const std::uint64_t line_number,
2162 const std::uintptr_t & identifier);
2263void region_begin (const std::string& function_name, const std::string& module ,
2364 const std::string& file_name, const std::uint64_t line_number);
2465
66+ /* * tries to end a region. Return true on success
67+ *
68+ */
69+ inline bool try_region_end (std::uintptr_t identifier)
70+ {
71+ auto it_region = regions.find (identifier);
72+ if (it_region != regions.end ())
73+ {
74+ SCOREP_User_RegionEnd (it_region->second .value );
75+ return true ;
76+ }
77+ else
78+ {
79+ return false ;
80+ }
81+ }
82+
2583void region_end (const std::string& function_name, const std::string& module ,
2684 const std::uintptr_t & identifier);
2785void region_end (const std::string& function_name, const std::string& module );
@@ -34,5 +92,4 @@ void rewind_end(std::string region_name, bool value);
3492void parameter_int (std::string name, int64_t value);
3593void parameter_uint (std::string name, uint64_t value);
3694void parameter_string (std::string name, std::string value);
37-
3895} // namespace scorepy
0 commit comments