Debian Codegraph @

crt/aws-c-common/cmake/AwsThreadName.cmake

raw
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 include(CheckSymbolExists)
5
6 # Check how the platform supports setting thread name
7 function(aws_set_thread_name_method target)
8
9 function(aws_set_thread_name_setter_method target)
10 if (APPLE)
11 # All Apple platforms we support have 1 arg version of the function.
12 # So skip compile time check here and instead check if its apple in
13 # the thread code.
14 return()
15 endif()
16
17 # pthread_setname_np() usually takes 2 args
18 check_c_source_compiles("
19 ${c_source_start}
20 pthread_setname_np(thread_id, \"asdf\");
21 ${c_source_end}"
22 PTHREAD_SETNAME_TAKES_2ARGS)
23 if (PTHREAD_SETNAME_TAKES_2ARGS)
24 target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_SETNAME_TAKES_2ARGS)
25 return()
26 endif()
27
28 # OpenBSD's function takes 2 args, but has a different name.
29 check_c_source_compiles("
30 ${c_source_start}
31 pthread_set_name_np(thread_id, \"asdf\");
32 ${c_source_end}"
33 PTHREAD_SET_NAME_TAKES_2ARGS)
34 if (PTHREAD_SET_NAME_TAKES_2ARGS)
35 target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_SET_NAME_TAKES_2ARGS)
36 return()
37 endif()
38
39 # But on NetBSD it takes 3!
40 check_c_source_compiles("
41 ${c_source_start}
42 pthread_setname_np(thread_id, \"asdf\", NULL);
43 ${c_source_end}
44 " PTHREAD_SETNAME_TAKES_3ARGS)
45 if (PTHREAD_SETNAME_TAKES_3ARGS)
46 target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_SETNAME_TAKES_3ARGS)
47 return()
48 endif()
49
50 # And on many older/weirder platforms it's just not supported
51 # Consider using prctl if we really want to support those
52 endfunction()
53
54 function(aws_set_thread_name_getter_method target)
55 # Some platforms have 2 arg version
56 check_c_source_compiles("
57 ${c_source_start}
58 char name[16] = {0};
59 pthread_getname_np(thread_id, name);
60 ${c_source_end}
61 " PTHREAD_GETNAME_TAKES_2ARGS)
62 if (PTHREAD_GETNAME_TAKES_2ARGS)
63 target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GETNAME_TAKES_2ARGS)
64 return()
65 endif()
66
67 # Some platforms have 2 arg version but with a different name (eg, OpenBSD)
68 check_c_source_compiles("
69 ${c_source_start}
70 char name[16] = {0};
71 pthread_get_name_np(thread_id, name);
72 ${c_source_end}
73 " PTHREAD_GET_NAME_TAKES_2ARGS)
74 if (PTHREAD_GET_NAME_TAKES_2ARGS)
75 target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GET_NAME_TAKES_2ARGS)
76 return()
77 endif()
78
79 # But majority have 3
80 check_c_source_compiles("
81 ${c_source_start}
82 char name[16] = {0};
83 pthread_getname_np(thread_id, name, 16);
84 ${c_source_end}
85 " PTHREAD_GETNAME_TAKES_3ARGS)
86 if (PTHREAD_GETNAME_TAKES_3ARGS)
87 target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GETNAME_TAKES_3ARGS)
88 return()
89 endif()
90
91 endfunction()
92
93 if (WIN32)
94 # On Windows we do a runtime check for both getter and setter, instead of compile-time check
95 return()
96 endif()
97
98 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
99 list(APPEND CMAKE_REQUIRED_LIBRARIES pthread)
100
101 # The start of the test program
102 set(c_source_start "
103 #define _GNU_SOURCE
104 #include <pthread.h>
105
106 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
107 #include <pthread_np.h>
108 #endif
109
110 int main() {
111 pthread_t thread_id;
112 ")
113
114 # The end of the test program
115 set(c_source_end "}")
116
117 aws_set_thread_name_setter_method(${target})
118 aws_set_thread_name_getter_method(${target})
119 endfunction()