Commit c3e09627 authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

Add support for getpriority and setpriority to the SysResource portability header

Summary: It's not a perfect mapping, but it's good enough.

Reviewed By: yfeldblum

Differential Revision: D9569405

fbshipit-source-id: 7559fb94f8a57717b5a594be0c05a99ac88c6c6e
parent a1a6981f
......@@ -16,6 +16,8 @@
#include <folly/portability/SysResource.h>
#include <errno.h>
#ifdef _WIN32
#include <folly/portability/Windows.h>
......@@ -41,5 +43,64 @@ int setrlimit(int /* type */, rlimit* /* src */) {
// We couldn't set the stack size at runtime even if we wanted to.
return 0;
}
int getpriority(int which, int who) {
if (which != PRIO_PROCESS || who != 0) {
errno = EINVAL;
return -1;
}
auto ret = GetPriorityClass(GetCurrentProcess());
switch (ret) {
case 0:
errno = EACCES;
return -1;
case IDLE_PRIORITY_CLASS:
return 39;
case BELOW_NORMAL_PRIORITY_CLASS:
return 30;
case NORMAL_PRIORITY_CLASS:
return 20;
case ABOVE_NORMAL_PRIORITY_CLASS:
return 10;
case HIGH_PRIORITY_CLASS:
return 0;
case REALTIME_PRIORITY_CLASS:
// I'd return -1 if it weren't an error :(
// Realtime priority processes can't be set
// through these APIs because it's a terrible idea.
return 0;
default:
errno = EINVAL;
return -1;
}
}
int setpriority(int which, int who, int value) {
if (which != PRIO_PROCESS || who != 0) {
errno = EINVAL;
return -1;
}
auto newClass = [value] {
if (value >= 39) {
return IDLE_PRIORITY_CLASS;
} else if (value >= 30) {
return BELOW_NORMAL_PRIORITY_CLASS;
} else if (value >= 20) {
return NORMAL_PRIORITY_CLASS;
} else if (value >= 10) {
return ABOVE_NORMAL_PRIORITY_CLASS;
} else {
return HIGH_PRIORITY_CLASS;
}
}();
if (!SetPriorityClass(GetCurrentProcess(), newClass)) {
errno = EACCES;
return -1;
}
return 0;
}
}
#endif
......@@ -23,6 +23,8 @@
#include <folly/portability/SysTime.h>
#define PRIO_PROCESS 1
#define RLIMIT_CORE 0
#define RLIMIT_NOFILE 0
#define RLIMIT_DATA 0
......@@ -62,5 +64,8 @@ extern "C" {
int getrlimit(int type, rlimit* dst);
int getrusage(int who, rusage* usage);
int setrlimit(int type, rlimit* src);
int getpriority(int which, int who);
int setpriority(int which, int who, int value);
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment