/src/libtorrent/src/platform_util.cpp
Line | Count | Source |
1 | | /* |
2 | | |
3 | | Copyright (c) 2020, zywo |
4 | | Copyright (c) 2020, zywo |
5 | | Copyright (c) 2010, 2014-2016, 2018, 2020-2022, Arvid Norberg |
6 | | Copyright (c) 2021, Alden Torres |
7 | | All rights reserved. |
8 | | |
9 | | You may use, distribute and modify this code under the terms of the BSD license, |
10 | | see LICENSE file. |
11 | | */ |
12 | | |
13 | | #include "libtorrent/config.hpp" |
14 | | #include "libtorrent/aux_/platform_util.hpp" |
15 | | |
16 | | #include <cstdint> |
17 | | #include <limits> |
18 | | |
19 | | #if TORRENT_HAS_PTHREAD_SET_NAME |
20 | | #include <pthread.h> |
21 | | #ifdef TORRENT_BSD |
22 | | #include <pthread_np.h> |
23 | | #endif |
24 | | #endif |
25 | | |
26 | | #ifdef TORRENT_BEOS |
27 | | #include <kernel/OS.h> |
28 | | #endif |
29 | | |
30 | | #include "libtorrent/aux_/disable_warnings_push.hpp" |
31 | | |
32 | | #if TORRENT_USE_RLIMIT |
33 | | |
34 | | #include <sys/resource.h> |
35 | | |
36 | | // capture this here where warnings are disabled (the macro generates warnings) |
37 | | const rlim_t rlimit_as = RLIMIT_AS; |
38 | | const rlim_t rlimit_nofile = RLIMIT_NOFILE; |
39 | | const rlim_t rlim_infinity = RLIM_INFINITY; |
40 | | |
41 | | #endif // TORRENT_USE_RLIMIT |
42 | | |
43 | | #ifdef TORRENT_BSD |
44 | | #include <sys/types.h> |
45 | | #include <sys/sysctl.h> |
46 | | #endif |
47 | | |
48 | | #if defined TORRENT_WINDOWS |
49 | | #include "libtorrent/aux_/windows.hpp" |
50 | | #include "libtorrent/aux_/win_util.hpp" |
51 | | #endif |
52 | | |
53 | | #include "libtorrent/aux_/disable_warnings_pop.hpp" |
54 | | |
55 | | namespace libtorrent::aux { |
56 | | |
57 | | int max_open_files() |
58 | 13 | { |
59 | | #if defined TORRENT_BUILD_SIMULATOR |
60 | | return 256; |
61 | | #elif TORRENT_USE_RLIMIT |
62 | | |
63 | 13 | int const inf = 10000000; |
64 | | |
65 | 13 | struct rlimit rl{}; |
66 | 13 | if (getrlimit(RLIMIT_NOFILE, &rl) == 0) |
67 | 13 | { |
68 | 13 | if (rl.rlim_cur == rlim_infinity) return inf; |
69 | 13 | return rl.rlim_cur <= static_cast<rlim_t>(inf) |
70 | 13 | ? static_cast<int>(rl.rlim_cur) : inf; |
71 | 13 | } |
72 | 0 | return 1024; |
73 | | #else |
74 | | // this seems like a reasonable limit for windows. |
75 | | // http://blogs.msdn.com/b/oldnewthing/archive/2007/07/18/3926581.aspx |
76 | | return 10000; |
77 | | #endif |
78 | 13 | } |
79 | | |
80 | | void set_thread_name(char const* name) |
81 | 37 | { |
82 | 37 | TORRENT_UNUSED(name); |
83 | 37 | #if TORRENT_HAS_PTHREAD_SET_NAME |
84 | | #ifdef TORRENT_BSD |
85 | | pthread_set_name_np(pthread_self(), name); |
86 | | #else |
87 | 37 | pthread_setname_np(pthread_self(), name); |
88 | 37 | #endif |
89 | 37 | #endif |
90 | | #ifdef TORRENT_WINDOWS |
91 | | using SetThreadDescription_t = HRESULT (WINAPI*)(HANDLE, PCWSTR); |
92 | | auto SetThreadDescription = |
93 | | aux::get_library_procedure<aux::kernel32, SetThreadDescription_t>("SetThreadDescription"); |
94 | | if (SetThreadDescription) { |
95 | | |
96 | | wchar_t wide_name[50]; |
97 | | int i = -1; |
98 | | do { |
99 | | ++i; |
100 | | wide_name[i] = name[i]; |
101 | | } while (name[i] != 0); |
102 | | SetThreadDescription(GetCurrentThread(), wide_name); |
103 | | } |
104 | | #endif |
105 | | #ifdef TORRENT_BEOS |
106 | | rename_thread(find_thread(nullptr), name); |
107 | | #endif |
108 | 37 | } |
109 | | } |