Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
porting.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/porting.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_COMMON_PORTING_HEADER
13 #define THRILL_COMMON_PORTING_HEADER
14 
15 #include <thrill/common/logger.hpp>
16 
17 #if defined(_MSC_VER)
18 // windows/msvc is a mess.
19 #include <BaseTsd.h>
20 using ssize_t = SSIZE_T;
21 #endif
22 
23 #include <string>
24 #include <system_error>
25 #include <thread>
26 
27 namespace thrill {
28 namespace common {
29 
30 //! set FD_CLOEXEC on file descriptor (if possible)
31 void PortSetCloseOnExec(int fd);
32 
33 //! create a pair of pipe file descriptors
34 void MakePipe(int out_pipefds[2]);
35 
36 //! try to figure out the command line options of the current process and log it
37 //! to json logger
38 void LogCmdlineParams(JsonLogger& logger);
39 
40 //! create a std::thread and repeat creation if it fails
41 template <typename... Args>
42 std::thread CreateThread(Args&& ... args) {
43  // try for 300 seconds
44  size_t r = 3000;
45  while (1) {
46  try {
47  return std::thread(std::forward<Args>(args) ...);
48  }
49  catch (std::system_error&) {
50  if (--r == 0) throw;
51  LOG1 << "Thread creation failed, retrying.";
52  std::this_thread::sleep_for(std::chrono::milliseconds(100));
53  }
54  }
55 }
56 
57 //! set cpu/core affinity of a thread
58 void SetCpuAffinity(std::thread& thread, size_t cpu_id);
59 
60 //! set cpu/core affinity of current thread
61 void SetCpuAffinity(size_t cpu_id);
62 
63 //! get hostname
65 
66 } // namespace common
67 } // namespace thrill
68 
69 #endif // !THRILL_COMMON_PORTING_HEADER
70 
71 /******************************************************************************/
void LogCmdlineParams(JsonLogger &logger)
Definition: porting.cpp:67
#define LOG1
Definition: logger.hpp:28
void PortSetCloseOnExec(int fd)
set FD_CLOEXEC on file descriptor (if possible)
Definition: porting.cpp:41
void MakePipe(int out_pipefds[2])
create a pair of pipe file descriptors
Definition: porting.cpp:51
std::string GetHostname()
get hostname
Definition: porting.cpp:142
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
std::thread CreateThread(Args &&...args)
create a std::thread and repeat creation if it fails
Definition: porting.hpp:42
void SetCpuAffinity(std::thread &thread, size_t cpu_id)
set cpu/core affinity of a thread
Definition: porting.cpp:110