From b9c25b0d0ec7bb16d72dca4b6f529af604e66df6 Mon Sep 17 00:00:00 2001 From: Leorize Date: Wed, 9 Jan 2019 15:52:04 +0700 Subject: [PATCH] unistd: introduce nice() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit implements nice() as specified in POSIX.1-2008. Fixes #4932. Change-Id: Ibd8d4636d9e3a8adf0f26a081d5b1180f0cbb839 Reviewed-on: https://review.haiku-os.org/c/863 Reviewed-by: Jérôme Duval Reviewed-by: waddlesplash --- headers/posix/unistd.h | 2 ++ src/system/libroot/posix/unistd/Jamfile | 1 + src/system/libroot/posix/unistd/nice.c | 34 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/system/libroot/posix/unistd/nice.c 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; +}