Skip to content

Commit

Permalink
Use pthread_attr_setaffinity_np instead of pthread_setaffinity_np
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Nov 6, 2023
1 parent 8582c6b commit 0ff1798
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ext/socket/extconf.rb
Expand Up @@ -704,7 +704,7 @@ def %(s) s || self end

have_func("pthread_create")
have_func("pthread_detach")
have_func("pthread_setaffinity_np")
have_func("pthread_attr_setaffinity_np")
have_func("sched_getcpu")

$VPATH << '$(topdir)' << '$(top_srcdir)'
Expand Down
30 changes: 20 additions & 10 deletions ext/socket/raddrinfo.c
Expand Up @@ -475,18 +475,23 @@ rb_getaddrinfo(const char *hostp, const char *portp, const struct addrinfo *hint
return EAI_MEMORY;
}

pthread_t th;
if (pthread_create(&th, 0, do_getaddrinfo, arg) != 0) {
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
free_getaddrinfo_arg(arg);
return EAI_AGAIN;
}

#if defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
cpu_set_t tmp_cpu_set;
CPU_ZERO(&tmp_cpu_set);
CPU_SET(sched_getcpu(), &tmp_cpu_set);
pthread_setaffinity_np(th, sizeof(cpu_set_t), &tmp_cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
#endif

pthread_t th;
if (pthread_create(&th, &attr, do_getaddrinfo, arg) != 0) {
free_getaddrinfo_arg(arg);
return EAI_AGAIN;
}
pthread_detach(th);

rb_thread_call_without_gvl2(wait_getaddrinfo, arg, cancel_getaddrinfo, arg);
Expand Down Expand Up @@ -694,18 +699,23 @@ rb_getnameinfo(const struct sockaddr *sa, socklen_t salen,
return EAI_MEMORY;
}

pthread_t th;
if (pthread_create(&th, 0, do_getnameinfo, arg) != 0) {
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
free_getnameinfo_arg(arg);
return EAI_AGAIN;
}

#if defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
#if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU)
cpu_set_t tmp_cpu_set;
CPU_ZERO(&tmp_cpu_set);
CPU_SET(sched_getcpu(), &tmp_cpu_set);
pthread_setaffinity_np(th, sizeof(cpu_set_t), &tmp_cpu_set);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set);
#endif

pthread_t th;
if (pthread_create(&th, 0, do_getnameinfo, arg) != 0) {
free_getnameinfo_arg(arg);
return EAI_AGAIN;
}
pthread_detach(th);

rb_thread_call_without_gvl2(wait_getnameinfo, arg, cancel_getnameinfo, arg);
Expand Down

0 comments on commit 0ff1798

Please sign in to comment.