use pthread_setaffinity_np() only when it is available

This commit is contained in:
Lennart Poettering 2009-01-22 22:50:03 +01:00
parent 7c7133e09d
commit ddbe6126d3
2 changed files with 11 additions and 7 deletions

View file

@ -370,7 +370,7 @@ AC_FUNC_SELECT_ARGTYPES
AC_CHECK_FUNCS([chmod chown clock_gettime getaddrinfo getgrgid_r getgrnam_r \ AC_CHECK_FUNCS([chmod chown clock_gettime getaddrinfo getgrgid_r getgrnam_r \
getpwnam_r getpwuid_r gettimeofday getuid inet_ntop inet_pton mlock nanosleep \ getpwnam_r getpwuid_r gettimeofday getuid inet_ntop inet_pton mlock nanosleep \
pipe posix_fadvise posix_madvise posix_memalign setpgid setsid shm_open \ pipe posix_fadvise posix_madvise posix_memalign setpgid setsid shm_open \
sigaction sleep sysconf]) sigaction sleep sysconf pthread_setaffinity_np])
AC_CHECK_FUNCS([mkfifo], [HAVE_MKFIFO=1], [HAVE_MKFIFO=0]) AC_CHECK_FUNCS([mkfifo], [HAVE_MKFIFO=1], [HAVE_MKFIFO=0])
AM_CONDITIONAL(HAVE_MKFIFO, test "x$HAVE_MKFIFO" = "x1") AM_CONDITIONAL(HAVE_MKFIFO, test "x$HAVE_MKFIFO" = "x1")

View file

@ -43,24 +43,28 @@ static int msec_lower, msec_upper;
static void* work(void *p) PA_GCC_NORETURN; static void* work(void *p) PA_GCC_NORETURN;
static void* work(void *p) { static void* work(void *p) {
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
cpu_set_t mask; cpu_set_t mask;
#endif
struct sched_param param; struct sched_param param;
pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_INT(p)); pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p));
memset(&param, 0, sizeof(param)); memset(&param, 0, sizeof(param));
param.sched_priority = 12; param.sched_priority = 12;
pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0); pa_assert_se(pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0);
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
CPU_ZERO(&mask); CPU_ZERO(&mask);
CPU_SET((size_t) PA_PTR_TO_INT(p), &mask); CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask);
pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0); pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
#endif
for (;;) { for (;;) {
struct timespec now, end; struct timespec now, end;
uint64_t nsec; uint64_t nsec;
pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_INT(p)); pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_UINT(p));
sleep(1); sleep(1);
pa_assert_se(clock_gettime(CLOCK_REALTIME, &end) == 0); pa_assert_se(clock_gettime(CLOCK_REALTIME, &end) == 0);
@ -69,7 +73,7 @@ static void* work(void *p) {
(uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_NSEC_PER_MSEC)/RAND_MAX) + (uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_NSEC_PER_MSEC)/RAND_MAX) +
(uint64_t) ((uint64_t) msec_lower*PA_NSEC_PER_MSEC); (uint64_t) ((uint64_t) msec_lower*PA_NSEC_PER_MSEC);
pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_INT(p), (int) (nsec/PA_NSEC_PER_MSEC)); pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_UINT(p), (int) (nsec/PA_NSEC_PER_MSEC));
end.tv_sec += (time_t) (nsec / PA_NSEC_PER_SEC); end.tv_sec += (time_t) (nsec / PA_NSEC_PER_SEC);
end.tv_nsec += (long int) (nsec % PA_NSEC_PER_SEC); end.tv_nsec += (long int) (nsec % PA_NSEC_PER_SEC);
@ -87,7 +91,7 @@ static void* work(void *p) {
} }
int main(int argc, char*argv[]) { int main(int argc, char*argv[]) {
int n; unsigned n;
srand((unsigned) time(NULL)); srand((unsigned) time(NULL));
@ -109,7 +113,7 @@ int main(int argc, char*argv[]) {
for (n = 1; n < pa_ncpus(); n++) { for (n = 1; n < pa_ncpus(); n++) {
pthread_t t; pthread_t t;
pa_assert_se(pthread_create(&t, NULL, work, PA_INT_TO_PTR(n)) == 0); pa_assert_se(pthread_create(&t, NULL, work, PA_UINT_TO_PTR(n)) == 0);
} }
work(PA_INT_TO_PTR(0)); work(PA_INT_TO_PTR(0));