| 1 | #include <mbgl/util/platform.hpp> |
|---|---|
| 2 | #include <mbgl/util/logging.hpp> |
| 3 | |
| 4 | #include <string> |
| 5 | |
| 6 | #include <pthread.h> |
| 7 | #include <sched.h> |
| 8 | |
| 9 | namespace mbgl { |
| 10 | namespace platform { |
| 11 | |
| 12 | std::string getCurrentThreadName() { |
| 13 | char name[32] = "unknown"; |
| 14 | pthread_getname_np(target_thread: pthread_self(), buf: name, buflen: sizeof(name)); |
| 15 | |
| 16 | return name; |
| 17 | } |
| 18 | |
| 19 | void setCurrentThreadName(const std::string& name) { |
| 20 | if (name.size() > 15) { // Linux hard limit (see manpages). |
| 21 | pthread_setname_np(target_thread: pthread_self(), name: name.substr(pos: 0, n: 15).c_str()); |
| 22 | } else { |
| 23 | pthread_setname_np(target_thread: pthread_self(), name: name.c_str()); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | void makeThreadLowPriority() { |
| 28 | struct sched_param param; |
| 29 | param.sched_priority = 0; |
| 30 | |
| 31 | if (sched_setscheduler(pid: 0, SCHED_IDLE, param: ¶m) != 0) { |
| 32 | Log::Warning(event: Event::General, args: "Couldn't set thread scheduling policy"); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | } // namespace platform |
| 37 | } // namespace mbgl |
| 38 |
