diff options
author | davidxu <davidxu@FreeBSD.org> | 2008-03-04 03:03:24 +0000 |
---|---|---|
committer | davidxu <davidxu@FreeBSD.org> | 2008-03-04 03:03:24 +0000 |
commit | 7046a9b03766bc2807e976ac85107f18d8727ea2 (patch) | |
tree | 9cc630a024ac946921c43bdf3224141e204e09ce | |
parent | 23fe3bcc743fb5d390e8514b3fbfc0727047c84a (diff) | |
download | FreeBSD-src-7046a9b03766bc2807e976ac85107f18d8727ea2.zip FreeBSD-src-7046a9b03766bc2807e976ac85107f18d8727ea2.tar.gz |
implement pthread_attr_getaffinity_np and pthread_attr_setaffinity_np.
-rw-r--r-- | lib/libthr/thread/thr_attr.c | 59 | ||||
-rw-r--r-- | lib/libthr/thread/thr_create.c | 6 | ||||
-rw-r--r-- | lib/libthr/thread/thr_init.c | 4 | ||||
-rw-r--r-- | lib/libthr/thread/thr_private.h | 2 |
4 files changed, 68 insertions, 3 deletions
diff --git a/lib/libthr/thread/thr_attr.c b/lib/libthr/thread/thr_attr.c index ffb0453..3e5b4bf 100644 --- a/lib/libthr/thread/thr_attr.c +++ b/lib/libthr/thread/thr_attr.c @@ -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); +} diff --git a/lib/libthr/thread/thr_create.c b/lib/libthr/thread/thr_create.c index abab212..03f0584 100644 --- a/lib/libthr/thread/thr_create.c +++ b/lib/libthr/thread/thr_create.c @@ -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) diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index d35834c..d76c5ca 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -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 = { diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index 387f0de..e04de49 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -221,6 +221,8 @@ struct pthread_attr { void *stackaddr_attr; size_t stacksize_attr; size_t guardsize_attr; + cpuset_t *cpuset; + size_t cpusetsize; }; /* |