use rtprio_thread system call to get or set thread priority.

This commit is contained in:
davidxu 2006-09-21 04:21:30 +00:00
parent 92bd1e76b1
commit d2c57b7fad
7 changed files with 90 additions and 16 deletions

View File

@ -29,6 +29,7 @@
#include "namespace.h"
#include <sys/types.h>
#include <sys/rtprio.h>
#include <sys/signalvar.h>
#include <errno.h>
#include <stdlib.h>
@ -50,7 +51,8 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
{
struct pthread *curthread, *new_thread;
struct thr_param param;
struct thr_sched_param sched_param;
struct sched_param sched_param;
struct rtprio rtp;
int ret = 0, locked, create_suspended;
sigset_t set, oset;
@ -144,12 +146,12 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
param.flags |= THR_SYSTEM_SCOPE;
if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
param.sched_param = NULL;
param.rtp = NULL;
else {
param.sched_param = &sched_param;
param.sched_param_size = sizeof(sched_param);
sched_param.policy = new_thread->attr.sched_policy;
sched_param.param.sched_priority = new_thread->attr.prio;
sched_param.sched_priority = new_thread->attr.prio;
_schedparam_to_rtp(new_thread->attr.sched_policy,
&sched_param, &rtp);
param.rtp = &rtp;
}
/* Schedule the new thread. */

View File

@ -33,6 +33,8 @@
*/
#include "namespace.h"
#include <sys/types.h>
#include <sys/rtprio.h>
#include <errno.h>
#include <pthread.h>
#include "un-namespace.h"

View File

@ -411,8 +411,8 @@ init_main_thread(struct pthread *thread)
thread->state = PS_RUNNING;
thr_getscheduler(thread->tid, &thread->attr.sched_policy,
&sched_param, sizeof(sched_param));
_thr_getscheduler(thread->tid, &thread->attr.sched_policy,
&sched_param);
thread->attr.prio = sched_param.sched_priority;
/* Others cleared to zero by thr_alloc() */

View File

@ -29,6 +29,7 @@
#include <sys/types.h>
#include <sys/signalvar.h>
#include <sys/rtprio.h>
#include <pthread.h>
#include "thr_private.h"
@ -96,3 +97,68 @@ _thr_assert_lock_level()
{
PANIC("locklevel <= 0");
}
int
_rtp_to_schedparam(const struct rtprio *rtp, int *policy,
struct sched_param *param)
{
switch(rtp->type) {
case RTP_PRIO_REALTIME:
*policy = SCHED_RR;
param->sched_priority = RTP_PRIO_MAX - rtp->prio;
break;
case RTP_PRIO_FIFO:
*policy = SCHED_FIFO;
param->sched_priority = RTP_PRIO_MAX - rtp->prio;
break;
default:
*policy = SCHED_OTHER;
param->sched_priority = 0;
break;
}
return (0);
}
int
_schedparam_to_rtp(int policy, const struct sched_param *param,
struct rtprio *rtp)
{
switch(policy) {
case SCHED_RR:
rtp->type = RTP_PRIO_REALTIME;
rtp->prio = RTP_PRIO_MAX - param->sched_priority;
break;
case SCHED_FIFO:
rtp->type = RTP_PRIO_FIFO;
rtp->prio = RTP_PRIO_MAX - param->sched_priority;
break;
case SCHED_OTHER:
default:
rtp->type = RTP_PRIO_NORMAL;
rtp->prio = 0;
break;
}
return (0);
}
int
_thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
{
struct rtprio rtp;
int ret;
ret = rtprio_thread(RTP_LOOKUP, lwpid, &rtp);
if (ret == -1)
return (ret);
_rtp_to_schedparam(&rtp, policy, param);
return (0);
}
int
_thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
{
struct rtprio rtp;
_schedparam_to_rtp(policy, param, &rtp);
return (rtprio_thread(RTP_SET, lwpid, &rtp));
}

View File

@ -640,6 +640,12 @@ void _thr_once_init(void) __hidden;
void _thr_report_creation(struct pthread *curthread,
struct pthread *newthread) __hidden;
void _thr_report_death(struct pthread *curthread) __hidden;
int _thr_getscheduler(lwpid_t, int *, struct sched_param *) __hidden;
int _thr_setscheduler(lwpid_t, int, const struct sched_param *) __hidden;
int _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
struct sched_param *param) __hidden;
int _schedparam_to_rtp(int policy, const struct sched_param *param,
struct rtprio *rtp) __hidden;
void _thread_bp_create(void);
void _thread_bp_death(void);

View File

@ -55,8 +55,8 @@ _pthread_setprio(pthread_t pthread, int prio)
curthread->attr.prio = prio;
ret = 0;
} else {
ret = thr_setschedparam(curthread->tid,
&param, sizeof(struct sched_param));
ret = _thr_setscheduler(curthread->tid,
curthread->attr.sched_policy, &param);
if (ret == -1)
ret = errno;
else
@ -71,8 +71,8 @@ _pthread_setprio(pthread_t pthread, int prio)
pthread->attr.prio = prio;
ret = 0;
} else {
ret = thr_setschedparam(pthread->tid, &param,
sizeof(struct sched_param));
ret = _thr_setscheduler(pthread->tid,
curthread->attr.sched_policy, &param);
if (ret == -1)
ret = errno;
else

View File

@ -62,8 +62,7 @@ _pthread_setschedparam(pthread_t pthread, int policy,
THR_UNLOCK(curthread);
return (0);
}
ret = thr_setscheduler(curthread->tid, policy, param,
sizeof(struct sched_param));
ret = _thr_setscheduler(curthread->tid, policy, param);
if (ret == -1)
ret = errno;
else {
@ -81,8 +80,7 @@ _pthread_setschedparam(pthread_t pthread, int policy,
THR_THREAD_UNLOCK(curthread, pthread);
return (0);
}
ret = thr_setscheduler(pthread->tid, policy, param,
sizeof(struct sched_param));
ret = _thr_setscheduler(pthread->tid, policy, param);
if (ret == -1)
ret = errno;
else {