implement pthread_attr_getaffinity_np and pthread_attr_setaffinity_np.

This commit is contained in:
David Xu 2008-03-04 03:03:24 +00:00
parent 9b9f2abc3c
commit a759db946a
4 changed files with 68 additions and 3 deletions

View File

@ -148,7 +148,9 @@ _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
attr.flags |= PTHREAD_DETACHED;
_thr_ref_delete(curthread, pid);
memcpy(*dst, &attr, sizeof(struct pthread_attr));
/* XXX */
(*dst)->cpuset = NULL;
(*dst)->cpusetsize = 0;
return (0);
}
@ -543,3 +545,58 @@ _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
}
return(ret);
}
int
pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
const cpuset_t *cpuset)
{
pthread_attr_t attr;
int ret;
if (pattr == NULL || (attr = (*pattr)) == NULL)
ret = EINVAL;
else {
if (cpusetsize == 0 || cpuset == NULL) {
if (attr->cpuset != NULL) {
free(attr->cpuset);
attr->cpuset = NULL;
attr->cpusetsize = 0;
}
return (0);
}
/*
* XXX figure out kernel cpuset size, reject invalid size.
*/
if (cpusetsize != attr->cpusetsize) {
void *newset = realloc(attr->cpuset, cpusetsize);
if (newset == NULL)
return (ENOMEM);
attr->cpuset = newset;
attr->cpusetsize = cpusetsize;
}
memcpy(attr->cpuset, cpuset, cpusetsize);
ret = 0;
}
return (ret);
}
int
pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
cpuset_t *cpuset)
{
pthread_attr_t attr;
int ret = 0;
if (pattr == NULL || (attr = (*pattr)) == NULL)
ret = EINVAL;
else if (attr->cpuset != NULL) {
memcpy(cpuset, attr->cpuset, MIN(cpusetsize, attr->cpusetsize));
if (cpusetsize > attr->cpusetsize)
memset(((char *)cpuset) + attr->cpusetsize, 0,
cpusetsize - attr->cpusetsize);
} else {
memset(cpuset, -1, cpusetsize);
}
return (ret);
}

View File

@ -36,6 +36,7 @@
#include <string.h>
#include <stddef.h>
#include <pthread.h>
#include <pthread_np.h>
#include "un-namespace.h"
#include "thr_private.h"
@ -73,8 +74,11 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
if (attr == NULL || *attr == NULL)
/* Use the default thread attributes: */
new_thread->attr = _pthread_attr_default;
else
else {
new_thread->attr = *(*attr);
new_thread->attr.cpuset = NULL;
new_thread->attr.cpusetsize = 0;
}
if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
/* inherit scheduling contention scope */
if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)

View File

@ -81,7 +81,9 @@ struct pthread_attr _pthread_attr_default = {
.flags = PTHREAD_SCOPE_SYSTEM,
.stackaddr_attr = NULL,
.stacksize_attr = THR_STACK_DEFAULT,
.guardsize_attr = 0
.guardsize_attr = 0,
.cpusetsize = 0,
.cpuset = NULL
};
struct pthread_mutex_attr _pthread_mutexattr_default = {

View File

@ -221,6 +221,8 @@ struct pthread_attr {
void *stackaddr_attr;
size_t stacksize_attr;
size_t guardsize_attr;
cpuset_t *cpuset;
size_t cpusetsize;
};
/*