• 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 <pthread.h>
11 #include <sched.h>
12 }
13 
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, ::pthread_self() );
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     cpu_set_t set;
32     CPU_ZERO( & set);
33     CPU_SET( cpuid, & set);
34     int err = 0;
35     if ( BOOST_UNLIKELY( 0 != ( err = ::pthread_setaffinity_np( h, sizeof( set), & set) ) ) ) {
36         throw std::system_error(
37                 std::error_code( err, std::system_category() ),
38                 "pthread_setaffinity_np() failed");
39     }
40 }
41 
42 }}}
43 
44 #ifdef BOOST_HAS_ABI_HEADERS
45 # include BOOST_ABI_SUFFIX
46 #endif
47