forked from RcppCore/RcppParallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.h
More file actions
83 lines (65 loc) · 2.21 KB
/
Common.h
File metadata and controls
83 lines (65 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef __RCPP_PARALLEL_COMMON__
#define __RCPP_PARALLEL_COMMON__
#include <cerrno>
#include <cstddef>
#include <cstdlib>
namespace RcppParallel {
template <typename T, typename U>
inline int resolveValue(const char* envvar,
T requestedValue,
U defaultValue)
{
// if the requested value is non-zero and not the default, we can use it
bool useRequestedValue =
requestedValue != static_cast<T>(defaultValue) &&
requestedValue > 0;
if (useRequestedValue)
return requestedValue;
// otherwise, try reading the default from associated envvar
// if the environment variable is unset, use the default
const char* var = getenv(envvar);
if (var == NULL)
return defaultValue;
// try to convert the string to a number
// if an error occurs during conversion, just use default
errno = 0;
char* end;
long value = strtol(var, &end, 10);
// check for conversion failure
if (end == var || *end != '\0' || errno == ERANGE)
return defaultValue;
// okay, return the parsed environment variable value
return value;
}
// Tag type used for disambiguating splitting constructors
struct Split {};
// Work executed within a background thread. We implement dynamic
// dispatch using vtables so we can have a stable type to cast
// to from the void* passed to the worker thread (required because
// the tinythreads interface allows to pass only a void* to the
// thread main rather than a generic type / template)
struct Worker
{
// construct and destruct (delete virtually)
Worker() {}
virtual ~Worker() {}
// dispatch work over a range of values
virtual void operator()(std::size_t begin, std::size_t end) = 0;
private:
// disable copying and assignment
Worker(const Worker&);
void operator=(const Worker&);
};
// Used for controlling the stack size for threads / tasks within a scope.
class ThreadStackSizeControl
{
public:
ThreadStackSizeControl();
~ThreadStackSizeControl();
private:
// COPYING: not copyable
ThreadStackSizeControl(const ThreadStackSizeControl&);
ThreadStackSizeControl& operator=(const ThreadStackSizeControl&);
};
} // namespace RcppParallel
#endif // __RCPP_PARALLEL_COMMON__