File: AwsThreadName.cmake

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (126 lines) | stat: -rw-r--r-- 4,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

include(CheckSymbolExists)

# Check how the platform supports setting thread name
function(aws_set_thread_name_method target)

    function(aws_set_thread_name_setter_method target)
        if (APPLE)
            # All Apple platforms we support have 1 arg version of the function.
            # So skip compile time check here and instead check if its apple in
            # the thread code.
            return()
        endif()

        # pthread_setname_np() usually takes 2 args
        check_c_source_compiles("
            ${c_source_start}
            pthread_setname_np(thread_id, \"asdf\");
            ${c_source_end}"
        PTHREAD_SETNAME_TAKES_2ARGS)
        if (PTHREAD_SETNAME_TAKES_2ARGS)
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_SETNAME_TAKES_2ARGS)
            return()
        endif()

        # OpenBSD's function takes 2 args, but has a different name.
        check_c_source_compiles("
            ${c_source_start}
            pthread_set_name_np(thread_id, \"asdf\");
            ${c_source_end}"
            PTHREAD_SET_NAME_TAKES_2ARGS)
        if (PTHREAD_SET_NAME_TAKES_2ARGS)
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_SET_NAME_TAKES_2ARGS)
            return()
        endif()

        # But on NetBSD it takes 3!
        check_c_source_compiles("
            ${c_source_start}
            pthread_setname_np(thread_id, \"asdf\", NULL);
            ${c_source_end}
            " PTHREAD_SETNAME_TAKES_3ARGS)
        if (PTHREAD_SETNAME_TAKES_3ARGS)
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_SETNAME_TAKES_3ARGS)
            return()
        endif()

        # And on many older/weirder platforms it's just not supported
        # Consider using prctl if we really want to support those
    endfunction()

    function(aws_set_thread_name_getter_method target)
        if (APPLE)
            # All Apple platforms we support have the same function, so no need for
            # compile-time check.
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GETNAME_TAKES_3ARGS)
            return()
        endif()

        # Some platforms have 2 arg version
        check_c_source_compiles("
            ${c_source_start}
                char name[16] = {0};
                pthread_getname_np(thread_id, name);
            ${c_source_end}
        " PTHREAD_GETNAME_TAKES_2ARGS)
        if (PTHREAD_GETNAME_TAKES_2ARGS)
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GETNAME_TAKES_2ARGS)
            return()
        endif()

        # Some platforms have 2 arg version but with a different name (eg, OpenBSD)
        check_c_source_compiles("
            ${c_source_start}
                char name[16] = {0};
                pthread_get_name_np(thread_id, name);
            ${c_source_end}
        " PTHREAD_GET_NAME_TAKES_2ARGS)
        if (PTHREAD_GET_NAME_TAKES_2ARGS)
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GET_NAME_TAKES_2ARGS)
            return()
        endif()

        # But majority have 3
        check_c_source_compiles("
            ${c_source_start}
                char name[16] = {0};
                pthread_getname_np(thread_id, name, 16);
            ${c_source_end}
        " PTHREAD_GETNAME_TAKES_3ARGS)
        if (PTHREAD_GETNAME_TAKES_3ARGS)
            target_compile_definitions(${target} PRIVATE -DAWS_PTHREAD_GETNAME_TAKES_3ARGS)
            return()
        endif()

    endfunction()

    if (WIN32)
        # On Windows we do a runtime check for both getter and setter, instead of compile-time check
        return()
    endif()

    list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
    list(APPEND CMAKE_REQUIRED_LIBRARIES pthread)

    # The start of the test program
    set(c_source_start "
        #define _GNU_SOURCE
        #include <pthread.h>

        #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
        #include <pthread_np.h>
        #endif

        int main() {
            pthread_t thread_id;
        ")

    # The end of the test program
    set(c_source_end "}")

    aws_set_thread_name_setter_method(${target})
    aws_set_thread_name_getter_method(${target})
endfunction()