aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/port/detail/ThreadPthread.cpp
blob: dd9bfeeb1a55b0799f13ea7ee68a262427dec022 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/port/detail/ThreadPthread.h"

char disable_linker_warning_about_empty_file_thread_pthread_cpp TD_UNUSED;

#if TD_THREAD_PTHREAD

#include "td/utils/misc.h"
#include "td/utils/port/detail/skip_eintr.h"
#if TD_NETBSD
#include "td/utils/ScopeGuard.h"
#endif

#include <pthread.h>
#if TD_FREEBSD
#include <pthread_np.h>
#endif
#include <sched.h>
#include <signal.h>
#if TD_FREEBSD
#include <sys/cpuset.h>
#endif
#if TD_FREEBSD || TD_OPENBSD || TD_NETBSD
#include <sys/sysctl.h>
#endif
#include <unistd.h>

namespace td {
namespace detail {
unsigned ThreadPthread::hardware_concurrency() {
// Linux and macOS
#if defined(_SC_NPROCESSORS_ONLN)
  {
    auto res = sysconf(_SC_NPROCESSORS_ONLN);
    if (res > 0) {
      return narrow_cast<unsigned>(res);
    }
  }
#endif

#if TD_FREEBSD || TD_OPENBSD || TD_NETBSD
#if defined(HW_AVAILCPU) && defined(CTL_HW)
  {
    int mib[2] = {CTL_HW, HW_AVAILCPU};
    int res{0};
    size_t len = sizeof(res);
    if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
      return res;
    }
  }
#endif

#if defined(HW_NCPU) && defined(CTL_HW)
  {
    int mib[2] = {CTL_HW, HW_NCPU};
    int res{0};
    size_t len = sizeof(res);
    if (sysctl(mib, 2, &res, &len, nullptr, 0) == 0 && res != 0) {
      return res;
    }
  }
#endif
#endif

  // Just in case
  return 8;
}

void ThreadPthread::set_name(CSlice name) {
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 12)
  pthread_setname_np(thread_, name.c_str());
#endif
#endif
}

void ThreadPthread::join() {
  if (is_inited_.get()) {
    is_inited_ = false;
    pthread_join(thread_, nullptr);
  }
}

void ThreadPthread::detach() {
  if (is_inited_.get()) {
    is_inited_ = false;
    pthread_detach(thread_);
  }
}

void ThreadPthread::send_real_time_signal(id thread_id, int real_time_signal_number) {
#ifdef SIGRTMIN
  pthread_kill(thread_id, SIGRTMIN + real_time_signal_number);
#endif
}

int ThreadPthread::do_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),
                                     void *arg) {
  return pthread_create(thread, attr, start_routine, arg);
}

#if TD_HAVE_THREAD_AFFINITY
Status ThreadPthread::set_affinity_mask(id thread_id, uint64 mask) {
#if TD_LINUX || TD_FREEBSD
#if TD_FREEBSD
  cpuset_t cpuset;
#else
  cpu_set_t cpuset;
#endif
  CPU_ZERO(&cpuset);
  for (int j = 0; j < 64 && j < CPU_SETSIZE; j++) {
    if ((mask >> j) & 1) {
      CPU_SET(j, &cpuset);
    }
  }

  auto res = skip_eintr([&] { return pthread_setaffinity_np(thread_id, sizeof(cpuset), &cpuset); });
  if (res) {
    return OS_ERROR("Failed to set thread affinity mask");
  }
  return Status::OK();
#elif TD_NETBSD
  cpuset_t *cpuset = cpuset_create();
  if (cpuset == nullptr) {
    return OS_ERROR("Failed to create cpuset");
  }
  SCOPE_EXIT {
    cpuset_destroy(cpuset);
  };
  for (int j = 0; j < 64; j++) {
    if ((mask >> j) & 1) {
      if (cpuset_set(j, cpuset) != 0) {
        return OS_ERROR("Failed to set CPU identifier");
      }
    }
  }

  auto res = skip_eintr([&] { return pthread_setaffinity_np(thread_id, cpuset_size(cpuset), cpuset); });
  if (res) {
    return OS_ERROR("Failed to set thread affinity mask");
  }
  if (get_affinity_mask(thread_id) != mask) {
    return Status::Error("Failed to set exact thread affinity mask");
  }
  return Status::OK();
#else
  return Status::Error("Unsupported");
#endif
}

uint64 ThreadPthread::get_affinity_mask(id thread_id) {
#if TD_LINUX || TD_FREEBSD
#if TD_FREEBSD
  cpuset_t cpuset;
#else
  cpu_set_t cpuset;
#endif
  CPU_ZERO(&cpuset);
  auto res = skip_eintr([&] { return pthread_getaffinity_np(thread_id, sizeof(cpuset), &cpuset); });
  if (res) {
    return 0;
  }

  uint64 mask = 0;
  for (int j = 0; j < 64 && j < CPU_SETSIZE; j++) {
    if (CPU_ISSET(j, &cpuset)) {
      mask |= static_cast<uint64>(1) << j;
    }
  }
  return mask;
#elif TD_NETBSD
  cpuset_t *cpuset = cpuset_create();
  if (cpuset == nullptr) {
    return 0;
  }
  SCOPE_EXIT {
    cpuset_destroy(cpuset);
  };
  auto res = skip_eintr([&] { return pthread_getaffinity_np(thread_id, cpuset_size(cpuset), cpuset); });
  if (res) {
    return 0;
  }

  uint64 mask = 0;
  for (int j = 0; j < 64; j++) {
    if (cpuset_isset(j, cpuset) > 0) {
      mask |= static_cast<uint64>(1) << j;
    }
  }
  if (mask == 0) {
    // the mask wasn't set, all CPUs are allowed
    auto proc_count = sysconf(_SC_NPROCESSORS_ONLN);
    for (int j = 0; j < 64 && j < proc_count; j++) {
      mask |= static_cast<uint64>(1) << j;
    }
  }
  return mask;
#else
  return 0;
#endif
}
#endif

namespace this_thread_pthread {
ThreadPthread::id get_id() {
  return pthread_self();
}
}  // namespace this_thread_pthread

}  // namespace detail
}  // namespace td
#endif