3 #ifndef CDSLIB_OS_LINUX_TOPOLOGY_H
4 #define CDSLIB_OS_LINUX_TOPOLOGY_H
6 #ifndef CDSLIB_OS_TOPOLOGY_H
7 # error "<cds/os/topology.h> must be included instead"
10 #include <cds/details/defs.h>
11 #include <cds/threading/model.h>
13 #include <sys/syscall.h>
16 namespace cds { namespace OS {
17 /// Linux-specific wrappers
18 CDS_CXX11_INLINE_NAMESPACE namespace Linux {
22 The implementation assumes that processor IDs are in numerical order
23 from 0 to N - 1, where N - count of processor in the system
28 static unsigned int s_nProcessorCount;
32 /// Logical processor count for the system
33 static unsigned int processor_count()
35 return s_nProcessorCount;
38 /// Get current processor number
40 Caveat: \p current_processor calls system \p sched_getcpu function
41 that may not be defined for target system (\p sched_getcpu is available since glibc 2.6).
42 If \p sched_getcpu is not defined the function emulates "current processor number" using
43 thread-specific data. You may manually disable the \p sched_getcpu usage compiling with
44 <tt>-DCDS_LINUX_NO_sched_getcpu</tt>.
46 static unsigned int current_processor()
48 // Compile libcds with -DCDS_LINUX_NO_sched_getcpu if your linux does not have sched_getcpu (glibc version less than 2.6)
49 # if !defined(CDS_LINUX_NO_sched_getcpu) && defined(SYS_getcpu)
50 int nProcessor = ::sched_getcpu();
51 return nProcessor == -1 ? 0 : (unsigned int) nProcessor;
53 // Use fake "current processor number" assigned for current thread
54 return (unsigned int) threading::Manager::fake_current_processor();
57 Another way (for x86/amd64 only)
59 Using cpuid and APIC ID (http://www.scss.tcd.ie/Jeremy.Jones/GetCurrentProcessorNumberXP.htm)
68 - cpuid is full sync barrier
69 - it is only for x86/amd64 architecture
74 /// Synonym for \ref current_processor
75 static unsigned int native_current_processor()
77 return current_processor();
87 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
88 using Linux::topology;
90 }} // namespace cds::OS
92 #endif // #ifndef CDSLIB_OS_LINUX_TOPOLOGY_H