While we don't actually make use of thread scheduling attributes, we do
support setting and retrieving them. However, it doesn't make any sense
to let you set them in a pthread_attr_t...but then ignore that in
pthread_create().
Diff below deletes the duplicated sched_{policy,param} members from the
internal struct pthread and instead use the values from the embedded
struct pthread_attr. For bonus points, pay attention to the sched_inherit
attribute and possibly set the values from the parent thread.
Problem noted by natano of bitrig.
ok?
Philip
Index: rthread.c
===================================================================
RCS file: /data/src/openbsd/src/lib/librthread/rthread.c,v
retrieving revision 1.80
diff -u -p -r1.80 rthread.c
--- rthread.c 7 Apr 2015 01:27:07 -0000 1.80
+++ rthread.c 21 Apr 2015 23:09:43 -0000
@@ -197,6 +197,7 @@ _rthread_init(void)
_thread_pagesize = (size_t)sysconf(_SC_PAGESIZE);
_rthread_attr_default.guard_size = _thread_pagesize;
+ thread->attr = _rthread_attr_default;
_rthread_initlib();
@@ -429,6 +430,12 @@ pthread_create(pthread_t *threadp, const
thread->tid = -1;
thread->attr = attr != NULL ? *(*attr) : _rthread_attr_default;
+ if (thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
+ pthread_t self = pthread_self();
+
+ thread->attr.sched_policy = self->attr.sched_policy;
+ thread->attr.sched_param = self->attr.sched_param;
+ }
if (thread->attr.detach_state == PTHREAD_CREATE_DETACHED)
thread->flags |= THREAD_DETACHED;
thread->flags |= THREAD_CANCEL_ENABLE|THREAD_CANCEL_DEFERRED;
Index: rthread.h
===================================================================
RCS file: /data/src/openbsd/src/lib/librthread/rthread.h,v
retrieving revision 1.50
diff -u -p -r1.50 rthread.h
--- rthread.h 31 Aug 2014 04:02:08 -0000 1.50
+++ rthread.h 21 Apr 2015 22:26:02 -0000
@@ -170,9 +170,7 @@ struct pthread {
LIST_ENTRY(pthread) threads;
TAILQ_ENTRY(pthread) waiting;
pthread_cond_t blocking_cond;
- int sched_policy;
struct pthread_attr attr;
- struct sched_param sched_param;
struct rthread_storage *local_storage;
struct rthread_cleanup_fn *cleanup_fns;
int myerrno;
Index: rthread_sched.c
===================================================================
RCS file: /data/src/openbsd/src/lib/librthread/rthread_sched.c,v
retrieving revision 1.12
diff -u -p -r1.12 rthread_sched.c
--- rthread_sched.c 22 Mar 2012 15:26:04 -0000 1.12
+++ rthread_sched.c 21 Apr 2015 23:10:47 -0000
@@ -32,9 +32,9 @@ int
pthread_getschedparam(pthread_t thread, int *policy,
struct sched_param *param)
{
- *policy = thread->sched_policy;
+ *policy = thread->attr.sched_policy;
if (param)
- *param = thread->sched_param;
+ *param = thread->attr.sched_param;
return (0);
}
@@ -47,15 +47,16 @@ pthread_setschedparam(pthread_t thread,
if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
policy != SCHED_RR)
return (EINVAL);
- thread->sched_policy = policy;
+ thread->attr.sched_policy = policy;
if (param)
- thread->sched_param = *param;
+ thread->attr.sched_param = *param;
return (0);
}
int
-pthread_attr_getschedparam(const pthread_attr_t *attrp, struct sched_param
*param)
+pthread_attr_getschedparam(const pthread_attr_t *attrp,
+ struct sched_param *param)
{
*param = (*attrp)->sched_param;
@@ -63,7 +64,8 @@ pthread_attr_getschedparam(const pthread
}
int
-pthread_attr_setschedparam(pthread_attr_t *attrp, const struct sched_param
*param)
+pthread_attr_setschedparam(pthread_attr_t *attrp,
+ const struct sched_param *param)
{
(*attrp)->sched_param = *param;
@@ -112,13 +114,13 @@ pthread_attr_setinheritsched(pthread_att
int
pthread_getprio(pthread_t thread)
{
- return (thread->sched_param.sched_priority);
+ return (thread->attr.sched_param.sched_priority);
}
int
pthread_setprio(pthread_t thread, int priority)
{
- thread->sched_param.sched_priority = priority;
+ thread->attr.sched_param.sched_priority = priority;
return (0);
}