diff --git a/headers/posix/unistd.h b/headers/posix/unistd.h index 3c9a3f67de..63889cec25 100644 --- a/headers/posix/unistd.h +++ b/headers/posix/unistd.h @@ -246,6 +246,8 @@ extern pid_t setpgrp(void); extern int chroot(const char *path); +extern int nice(int incr); + /* access permissions */ extern gid_t getegid(void); extern uid_t geteuid(void); diff --git a/src/system/libroot/posix/unistd/Jamfile b/src/system/libroot/posix/unistd/Jamfile index f973a9c698..522f4c5767 100644 --- a/src/system/libroot/posix/unistd/Jamfile +++ b/src/system/libroot/posix/unistd/Jamfile @@ -30,6 +30,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { lockf.cpp lseek.c mount.c + nice.c pause.c pipe.c process.c diff --git a/src/system/libroot/posix/unistd/nice.c b/src/system/libroot/posix/unistd/nice.c new file mode 100644 index 0000000000..cda59f663f --- /dev/null +++ b/src/system/libroot/posix/unistd/nice.c @@ -0,0 +1,34 @@ +/* + * Copyright 2019, Leorize . All rights reserved. + * Distributed under the terms of the MIT license. + */ + + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#endif + +#include +#include +#include + +#include /* MAX(), MIN() */ + + +/* setpriority() is used for the implementation as they share the same + * restrictions as defined in POSIX.1-2008. However, some restrictions might not + * be implemented by Haiku's setpriority(). */ +int +nice(int incr) +{ + int priority = incr; + + /* avoids overflow by checking the bounds beforehand */ + if (priority > -(2 * NZERO - 1) && priority < (2 * NZERO - 1)) + priority += getpriority(PRIO_PROCESS, 0); + + priority = MAX(priority, -NZERO); + priority = MIN(priority, NZERO - 1); + + return setpriority(PRIO_PROCESS, 0, priority) != -1 ? priority : -1; +}