Move libcds 1.6.0 from SVN
[libcds.git] / cds / os / free_bsd / topology.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_FREE_BSD_TOPOLOGY_H
4 #define __CDS_OS_FREE_BSD_TOPOLOGY_H
5
6 #ifndef __CDS_OS_TOPOLOGY_H
7 #   error "<cds/os/topology.h> must be included instead"
8 #endif
9
10 #include <cds/os/details/fake_topology.h>
11
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
14
15 namespace cds { namespace OS {
16
17     /// FreeBSD-specific wrappers
18     CDS_CXX11_INLINE_NAMESPACE namespace Free_BSD {
19
20         /// System topology
21         /**
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
24         */
25         struct topology: public OS::details::fake_topology
26         {
27         private:
28             //@cond
29             typedef OS::details::fake_topology  base_class;
30             //@endcond
31         public:
32
33             /// Logical processor count for the system
34             static unsigned int processor_count()
35             {
36                 int mib[4];
37
38                 /* set the mib for hw.ncpu */
39                 mib[0] = CTL_HW;
40                 mib[1] = HW_NCPU;
41                 int nCPU = 0;
42                 size_t len = sizeof(nCPU);
43
44                 /* get the number of CPUs from the system */
45                 return ::sysctl(mib, 2, &nCPU, &len, NULL, 0) == 0 && nCPU > 0 ? (unsigned int) nCPU : 1;
46             }
47
48             /// Get current processor number
49             /**
50                 Caveat: FreeBSD has no "get current processor number" system call.
51                 The function emulates "current processor number" using thread-specific data.
52             */
53             static unsigned int current_processor()
54             {
55                 return base_class::current_processor();
56             }
57
58             /// Synonym for \ref current_processor
59             static unsigned int native_current_processor()
60             {
61                 return current_processor();
62             }
63
64             //@cond
65             static void init()
66             {}
67             static void fini()
68             {}
69             //@endcond
70         };
71     }   // namespace Free_BSD
72
73 #ifndef CDS_CXX11_INLINE_NAMESPACE_SUPPORT
74     using Free_BSD::topology;
75 #endif
76 }}  // namespace cds::OS
77
78 #endif  // #ifndef __CDS_OS_FREE_BSD_TOPOLOGY_H