Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Add platform::setCurrentThreadPriority(double)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov committed Feb 5, 2020
1 parent 49d5ca1 commit 755c9f1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/mbgl/util/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ void setCurrentThreadName(const std::string& name);
// Makes the current thread low priority.
void makeThreadLowPriority();

// Sets priority of a current thread. Platform implementation
// must validate provided value.
void setCurrentThreadPriority(double priority);

} // namespace platform
} // namespace mbgl
8 changes: 8 additions & 0 deletions platform/android/src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ void makeThreadLowPriority() {
setpriority(PRIO_PROCESS, 0, 19);
}

void setCurrentThreadPriority(double priority) {
if (priority < -20 || priority > 19) {
Log::Warning(Event::General, "Couldn't set thread priority");
return;
}
setpriority(PRIO_PROCESS, 0, int(priority));
}

void attachThread() {
using namespace android;
assert(env == nullptr);
Expand Down
10 changes: 10 additions & 0 deletions platform/darwin/src/nsthread.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <Foundation/Foundation.h>

#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/platform/thread.hpp>

Expand All @@ -24,6 +25,15 @@ void makeThreadLowPriority() {
[[NSThread currentThread] setThreadPriority:0.0];
}

void setCurrentThreadPriority(double priority) {
if (priority > 1.0 || priority < 0.0) {
Log::Warning(Event::General, "Invalid thread priority was provided");
return;
}

[[NSThread currentThread] setThreadPriority:priority];
}

void attachThread() {
}

Expand Down
19 changes: 16 additions & 3 deletions platform/default/src/mbgl/util/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <pthread.h>
#include <sched.h>
#include <sys/resource.h>

namespace mbgl {
namespace platform {
Expand Down Expand Up @@ -40,11 +41,23 @@ void makeThreadLowPriority() {
#endif
}

void attachThread() {
}
void setCurrentThreadPriority(double priority) {
if (setpriority(PRIO_PROCESS, 0, int(priority)) < 0) {
Log::Warning(Event::General, "Couldn't set thread priority");
}

void detachThread() {
#ifdef SCHED_OTHER
struct sched_param param;
param.sched_priority = 0;
if (sched_setscheduler(0, SCHED_OTHER, &param) != 0) {
Log::Warning(Event::General, "Couldn't set thread scheduling policy");
}
#endif
}

void attachThread() {}

void detachThread() {}

} // namespace platform
} // namespace mbgl
2 changes: 2 additions & 0 deletions platform/qt/src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ void setCurrentThreadName(const std::string&) {
void makeThreadLowPriority() {
}

void setCurrentThreadPriority(double) {}

void attachThread() {
}

Expand Down

0 comments on commit 755c9f1

Please sign in to comment.