summaryrefslogtreecommitdiff
path: root/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'sched.c')
-rw-r--r--sched.c239
1 files changed, 18 insertions, 221 deletions
diff --git a/sched.c b/sched.c
index 6f558ab..bc8aca0 100644
--- a/sched.c
+++ b/sched.c
@@ -91,11 +91,7 @@ pthread_attr_setschedparam(pthread_attr_t *attr,
priority = param->sched_priority;
- /*
- * Validate priority level. Don't check the
- * return values of the function calls because
- * we're sure they will succeed.
- */
+ /* Validate priority level. */
if (priority < sched_get_priority_min(SCHED_OTHER) ||
priority > sched_get_priority_max(SCHED_OTHER))
{
@@ -109,7 +105,7 @@ pthread_attr_setschedparam(pthread_attr_t *attr,
int
pthread_attr_getschedparam(const pthread_attr_t *attr,
- struct sched_param *param)
+ struct sched_param *param)
{
if (is_attr(attr) != 0 || param == NULL)
{
@@ -177,13 +173,9 @@ pthread_setschedparam(pthread_t thread, int policy,
return ENOTSUP;
}
- /*
- * Validate priority level. Don't check the
- * return values of the function calls because
- * we're sure they will succeed.
- */
- if (param->sched_priority < sched_get_priority_min(SCHED_OTHER) ||
- param->sched_priority > sched_get_priority_max(SCHED_OTHER))
+ /* Validate priority level. */
+ if (param->sched_priority < sched_get_priority_min(policy) ||
+ param->sched_priority > sched_get_priority_max(policy))
{
return EINVAL;
}
@@ -253,14 +245,7 @@ sched_get_priority_max(int policy)
{
if (policy < SCHED_MIN || policy > SCHED_MAX)
{
- errno = EINVAL;
- return -1;
- }
-
- if (policy != SCHED_OTHER)
- {
- errno = ENOTSUP;
- return -1;
+ return EINVAL;
}
#if (THREAD_PRIORITY_LOWEST > THREAD_PRIORITY_NORMAL)
@@ -278,14 +263,7 @@ sched_get_priority_min(int policy)
{
if (policy < SCHED_MIN || policy > SCHED_MAX)
{
- errno = EINVAL;
- return -1;
- }
-
- if (policy != SCHED_OTHER)
- {
- errno = ENOTSUP;
- return -1;
+ return EINVAL;
}
#if (THREAD_PRIORITY_LOWEST > THREAD_PRIORITY_NORMAL)
@@ -299,175 +277,31 @@ sched_get_priority_min(int policy)
int
-sched_rr_get_interval(pid_t pid, struct timespec * interval)
- /*
- * ------------------------------------------------------
- * DOCPUBLIC
- * This function updates the timespec structure
- * referenced by the interval argument to contain
- * the current quantum for the process executing
- * under the SCHED_RR policy. If a process, running under
- * the round-robin scheduling policy, runs without
- * blocking or yielding for more than this amount of
- * time, it may be preempted by another runnable
- * process (at the same priority).
- *
- * If the PID argument is zero, the current execution
- * time limit for the calling process is returned.
- *
- * PARAMETERS
- * pid Process identifier.
- *
- *
- * DESCRIPTION
- * This function updates the timespec structure
- * referenced by the interval argument to contain
- * the current quantum for the process executing
- * under the SCHED_RR policy. If a process, running under
- * the round-robin scheduling policy, runs without
- * blocking or yielding for more than this amount of
- * time, it may be preempted by another runnable
- * process (at the same priority).
- *
- * If the PID argument is zero, the current execution
- * time limit for the calling process is returned.
- *
- * RESULTS
- * We don't support SCHED_RR so this routine always fails.
- *
- * NOTE: Since this is part of POSIX 1003.1b
- * (realtime extensions), it is defined as returning
- * -1 if an error occurs and sets errno to the actual
- * error.
- *
- * errno: ESRCH Pid doesn't refer to a valid process.
- * EPERM The process referenced by pid does
- * not allow query access.
- * ENOTSUP SCHED_RR is not supported.
- *
- * ------------------------------------------------------
- */
-{
- /*
- * Provide other valid side-effects
- * such as EPERM and ESRCH errors.
- */
- if (0 != pid)
- {
- DWORD selfPid = GetCurrentProcessId();
-
- if (pid != selfPid)
- {
- HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
-
- if (NULL == h)
- {
- errno = (GetLastError() == (0xFF & ERROR_ACCESS_DENIED)
- ? EPERM
- : ESRCH );
-
- return -1;
- }
-
- (void) CloseHandle(h);
- }
- }
-
- /*
- * We can't return ENOSYS and stay strictly compliant with the
- * standard. We don't support round-robin scheduling so
- * we return ENOTSUP instead. This is consistent with
- * routines which return ENOTSUP if SCHED_RR is given
- * as the policy parameter.
- */
- errno = ENOTSUP;
- return -1;
-}
-
-
-int
sched_setscheduler(pid_t pid, int policy)
- /*
- * ------------------------------------------------------
- * DOCPUBLIC
- * This function returns sets the scheduling policy and
- * scheduling parameters of the process specified by pid
- * to policy and the parameters specified in the
- * sched_param structure pointed to by param, respectively.
- * The value of the sched_priority member in the sched_param
- * structure is any integer within the inclusive priority
- * range for the scheduling policy specified by policy.
- * If the value of pid is negative, the behaviour of the
- * sched_setscheduler() function is unspecified.
- *
- * PARAMETERS
- * pid Identifier of the process in which the
- * policy will be set.
- * policy The new policy value.
- *
- *
- * DESCRIPTION
- * This function returns sets the scheduling policy and
- * scheduling parameters of the process specified by pid
- * to policy and the parameters specified in the
- * sched_param structure pointed to by param, respectively.
- * The value of the sched_priority member in the sched_param
- * structure is any integer within the inclusive priority
- * range for the scheduling policy specified by policy.
- * If the value of pid is negative, the behaviour of the
- * sched_setscheduler() function is unspecified.
- *
- * RESULTS
- * SCHED_OTHER on success this is the only possible
- * value that can be returned.
- *
- * NOTE: Since this is part of POSIX 1003.1b
- * (realtime extensions), it is defined as returning
- * -1 if an error occurs and sets errno to the actual
- * error.
- *
- * errno: ESRCH 'pid' doesn't refer to an existing
- * process.
- * EPERM The process referenced by pid does
- * not allow set access.
- * EINVAL 'policy' is not a valid policy value.
- * ENOSYS the policy specified is not supported.
- *
- * ------------------------------------------------------
- */
{
/*
* Win32 only has one policy which we call SCHED_OTHER.
* However, we try to provide other valid side-effects
- * such as EPERM and ESRCH errors.
+ * such as EPERM and ESRCH errors. Choosing to check
+ * for a valid policy last allows us to get the most value out
+ * of this function.
*/
if (0 != pid)
{
- DWORD selfPid = GetCurrentProcessId();
+ int selfPid = (int) GetCurrentProcessId();
if (pid != selfPid)
{
- HANDLE h = OpenProcess(PROCESS_SET_INFORMATION, FALSE, pid);
+ HANDLE h = OpenProcess(PROCESS_SET_INFORMATION, FALSE, (DWORD) pid);
if (NULL == h)
{
- errno = (GetLastError() == (0xFF & ERROR_ACCESS_DENIED)
- ? EPERM
- : ESRCH );
-
+ errno = (GetLastError() == (0xFF & ERROR_ACCESS_DENIED)) ? EPERM : ESRCH;
return -1;
}
-
- (void) CloseHandle(h);
}
}
- if (policy < SCHED_MIN || policy > SCHED_MAX)
- {
- errno = EINVAL;
- return -1;
- }
-
if (SCHED_OTHER != policy)
{
errno = ENOSYS;
@@ -484,39 +318,6 @@ sched_setscheduler(pid_t pid, int policy)
int
sched_getscheduler(pid_t pid)
- /*
- * ------------------------------------------------------
- * DOCPUBLIC
- * This function returns the scheduling policy of the
- * process specified by pid. If the value of pid is
- * negative, the behaviour of the sched_getscheduler()
- * function is unspecified.
- *
- * PARAMETERS
- * pid Process identifier.
- *
- *
- * DESCRIPTION
- * This function returns the scheduling policy of the
- * process specified by pid. If the value of pid is
- * negative, the behaviour of the sched_getscheduler()
- * function is unspecified.
- *
- * RESULTS
- * SCHED_OTHER on success this is the only possible
- * value that can be returned.
- *
- * NOTE: Since this is part of POSIX 1003.1b
- * (realtime extensions), it is defined as returning
- * -1 if an error occurs and sets errno to the actual
- * error.
- *
- * errno: ESRCH Pid doesn't refer to a valid process.
- * EPERM The process referenced by pid does
- * not allow query access.
- *
- * ------------------------------------------------------
- */
{
/*
* Win32 only has one policy which we call SCHED_OTHER.
@@ -525,22 +326,17 @@ sched_getscheduler(pid_t pid)
*/
if (0 != pid)
{
- DWORD selfPid = GetCurrentProcessId();
+ int selfPid = (int) GetCurrentProcessId();
if (pid != selfPid)
{
- HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+ HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD) pid);
if (NULL == h)
{
- errno = (GetLastError() == (0xFF & ERROR_ACCESS_DENIED)
- ? EPERM
- : ESRCH );
-
+ errno = (GetLastError() == (0xFF & ERROR_ACCESS_DENIED)) ? EPERM : ESRCH;
return -1;
}
-
- (void) CloseHandle(h);
}
}
@@ -569,7 +365,8 @@ sched_yield(void)
* error.
*
* RESULTS
- * 0 always succeeds
+ * 0 successfully created semaphore,
+ * ENOSYS sched_yield not supported,
*
* ------------------------------------------------------
*/