Skip to content

Commit 95b8e5c

Browse files
authored
Merge pull request swiftlang#2215 from spevans/pr_sr_10611
2 parents 76fe3ec + e4fa760 commit 95b8e5c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ include(SwiftSupport)
2424
include(GNUInstallDirs)
2525
include(ExternalProject)
2626

27+
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
28+
include(CheckSymbolExists)
29+
include(CheckIncludeFile)
30+
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
31+
check_include_file("sched.h" HAVE_SCHED_H)
32+
check_symbol_exists(sched_getaffinity "sched.h" HAVE_SCHED_GETAFFINITY)
33+
endif()
34+
2735
string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os)
2836
get_swift_host_arch(swift_arch)
2937

CoreFoundation/Base.subproj/CFUtilities.c

+13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
#include <pthread.h>
7272
#endif
7373

74+
#if DEPLOYMENT_TARGET_LINUX
75+
#ifdef HAVE_SCHED_GETAFFINITY
76+
#include <sched.h>
77+
#endif
78+
#endif
7479

7580

7681
#if !defined(CF_HAVE_HW_CONFIG_COMMPAGE) && defined(_COMM_PAGE_LOGICAL_CPUS) && defined(_COMM_PAGE_PHYSICAL_CPUS) && defined(_COMM_PAGE_ACTIVE_CPUS) && !__has_feature(address_sanitizer)
@@ -451,6 +456,14 @@ CF_PRIVATE CFIndex __CFActiveProcessorCount() {
451456
pcnt = 0;
452457
}
453458
#elif DEPLOYMENT_TARGET_LINUX
459+
460+
#ifdef HAVE_SCHED_GETAFFINITY
461+
cpu_set_t set;
462+
if (sched_getaffinity (getpid(), sizeof(set), &set) == 0) {
463+
return CPU_COUNT (&set);
464+
}
465+
#endif
466+
454467
pcnt = sysconf(_SC_NPROCESSORS_ONLN);
455468
#else
456469
// Assume the worst

CoreFoundation/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
337337
PRIVATE
338338
-DDEPLOYMENT_TARGET_LINUX
339339
-D_GNU_SOURCE)
340+
if(HAVE_SCHED_GETAFFINITY)
341+
target_compile_definitions(CoreFoundation
342+
PRIVATE
343+
-DHAVE_SCHED_GETAFFINITY)
344+
endif()
340345
elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
341346
target_compile_definitions(CoreFoundation
342347
PRIVATE
@@ -516,4 +521,3 @@ install(DIRECTORY
516521
${CMAKE_INSTALL_PREFIX}/System/Library/Frameworks
517522
USE_SOURCE_PERMISSIONS
518523
PATTERN PrivateHeaders EXCLUDE)
519-

0 commit comments

Comments
 (0)