• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //          Copyright Oliver Kowalke 2017.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include "boost/fiber/numa/pin_thread.hpp"
8 
9 extern "C" {
10 #include <windows.h>
11 }
12 
13 #include <cstring>
14 #include <system_error>
15 
16 #ifdef BOOST_HAS_ABI_HEADERS
17 # include BOOST_ABI_PREFIX
18 #endif
19 
20 namespace boost {
21 namespace fibers {
22 namespace numa {
23 
24 BOOST_FIBERS_DECL
pin_thread(std::uint32_t cpuid)25 void pin_thread( std::uint32_t cpuid) {
26     pin_thread( cpuid, ::GetCurrentThread() );
27 }
28 
29 BOOST_FIBERS_DECL
pin_thread(std::uint32_t cpuid,std::thread::native_handle_type h)30 void pin_thread( std::uint32_t cpuid, std::thread::native_handle_type h) {
31     GROUP_AFFINITY affinity;
32     std::memset( & affinity, 0, sizeof( affinity) );
33     // compute processor group
34     // a group contains max 64 logical CPUs
35     affinity.Group = static_cast< WORD >(cpuid / 64);
36     // compute the ID of the logical CPU in the group
37     uint32_t id = cpuid % 64;
38     // set the bit mask of the logical CPU
39     affinity.Mask = static_cast< KAFFINITY >( 1) << id;
40     if ( BOOST_UNLIKELY( 0 == ::SetThreadGroupAffinity( h, & affinity, nullptr) ) ) {
41         throw std::system_error(
42                 std::error_code( ::GetLastError(), std::system_category() ),
43                 "::SetThreadiGroupAffinity() failed");
44     }
45 }
46 
47 }}}
48 
49 #ifdef BOOST_HAS_ABI_HEADERS
50 # include BOOST_ABI_SUFFIX
51 #endif
52