xref: /haiku/src/system/libroot/posix/unistd/nice.c (revision 899e0ef82b5624ace2ccfa5f5a58c8ebee54aaef)
1 /*
2  * Copyright 2019, Leorize <leorize+oss@disroot.org>. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  */
5 
6 
7 #ifndef _XOPEN_SOURCE
8 #define _XOPEN_SOURCE 600
9 #endif
10 
11 #include <limits.h>
12 #include <sys/resource.h>
13 #include <unistd.h>
14 
15 #include <sys/param.h> /* MAX(), MIN() */
16 
17 
18 /* setpriority() is used for the implementation as they share the same
19  * restrictions as defined in POSIX.1-2008. However, some restrictions might not
20  * be implemented by Haiku's setpriority(). */
21 int
22 nice(int incr)
23 {
24 	int priority = incr;
25 
26 	/* avoids overflow by checking the bounds beforehand */
27 	if (priority > -(2 * NZERO - 1) && priority < (2 * NZERO - 1))
28 		priority += getpriority(PRIO_PROCESS, 0);
29 
30 	priority = MAX(priority, -NZERO);
31 	priority = MIN(priority, NZERO - 1);
32 
33 	return setpriority(PRIO_PROCESS, 0, priority) != -1 ? priority : -1;
34 }
35