1 | /**************************************************************************/ |
2 | /* thread_posix.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #if defined(UNIX_ENABLED) |
32 | |
33 | #include "thread_posix.h" |
34 | |
35 | #include "core/os/thread.h" |
36 | #include "core/string/ustring.h" |
37 | |
38 | #ifdef PTHREAD_BSD_SET_NAME |
39 | #include <pthread_np.h> |
40 | #endif |
41 | |
42 | static Error set_name(const String &p_name) { |
43 | #ifdef PTHREAD_NO_RENAME |
44 | return ERR_UNAVAILABLE; |
45 | |
46 | #else |
47 | |
48 | #ifdef PTHREAD_RENAME_SELF |
49 | |
50 | // check if thread is the same as caller |
51 | int err = pthread_setname_np(p_name.utf8().get_data()); |
52 | |
53 | #else |
54 | |
55 | pthread_t running_thread = pthread_self(); |
56 | #ifdef PTHREAD_BSD_SET_NAME |
57 | pthread_set_name_np(running_thread, p_name.utf8().get_data()); |
58 | int err = 0; // Open/FreeBSD ignore errors in this function |
59 | #elif defined(PTHREAD_NETBSD_SET_NAME) |
60 | int err = pthread_setname_np(running_thread, "%s" , const_cast<char *>(p_name.utf8().get_data())); |
61 | #else |
62 | int err = pthread_setname_np(running_thread, p_name.utf8().get_data()); |
63 | #endif // PTHREAD_BSD_SET_NAME |
64 | |
65 | #endif // PTHREAD_RENAME_SELF |
66 | |
67 | return err == 0 ? OK : ERR_INVALID_PARAMETER; |
68 | |
69 | #endif // PTHREAD_NO_RENAME |
70 | } |
71 | |
72 | void init_thread_posix() { |
73 | Thread::_set_platform_functions({ .set_name = set_name }); |
74 | } |
75 | |
76 | #endif // UNIX_ENABLED |
77 | |