Thrill  0.1
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 #include <dirent.h>
28 
29 namespace thrill {
30 namespace common {
31 
32 //! set FD_CLOEXEC on file descriptor (if possible)
33 void PortSetCloseOnExec(int fd);
34 
35 //! create a pair of pipe file descriptors
36 void MakePipe(int out_pipefds[2]);
37 
38 //! try to figure out the command line options of the current process and log it
39 //! to json logger
40 void LogCmdlineParams(JsonLogger& logger);
41 
42 //! create a std::thread and repeat creation if it fails
43 template <typename... Args>
44 std::thread CreateThread(Args&& ... args) {
45  // try for 300 seconds
46  size_t r = 3000;
47  while (1) {
48  try {
49  return std::thread(std::forward<Args>(args) ...);
50  }
51  catch (std::system_error&) {
52  if (--r == 0) throw;
53  LOG1 << "Thread creation failed, retrying.";
54  std::this_thread::sleep_for(std::chrono::milliseconds(100));
55  }
56  }
57 }
58 
59 //! set cpu/core affinity of a thread
60 void SetCpuAffinity(std::thread& thread, size_t cpu_id);
61 
62 //! set cpu/core affinity of current thread
63 void SetCpuAffinity(size_t cpu_id);
64 
65 //! get hostname
67 
68 //! mutex-locked readdir() call
69 struct dirent * ts_readdir(DIR* dirp);
70 
71 } // namespace common
72 } // namespace thrill
73 
74 #endif // !THRILL_COMMON_PORTING_HEADER
75 
76 /******************************************************************************/
std::thread CreateThread(Args &&... args)
create a std::thread and repeat creation if it fails
Definition: porting.hpp:44
void LogCmdlineParams(JsonLogger &logger)
Definition: porting.cpp:68
#define LOG1
Definition: logger.hpp:28
void PortSetCloseOnExec(int fd)
set FD_CLOEXEC on file descriptor (if possible)
Definition: porting.cpp:42
void MakePipe(int out_pipefds[2])
create a pair of pipe file descriptors
Definition: porting.cpp:52
std::string GetHostname()
get hostname
Definition: porting.cpp:143
struct dirent * ts_readdir(DIR *dirp)
mutex-locked readdir() call
Definition: porting.cpp:153
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
void SetCpuAffinity(std::thread &thread, size_t cpu_id)
set cpu/core affinity of a thread
Definition: porting.cpp:111