File: Threading.cc

package info (click to toggle)
ptl 2.3.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,296 kB
  • sloc: cpp: 8,195; python: 246; sh: 7; makefile: 3
file content (189 lines) | stat: -rw-r--r-- 5,450 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// MIT License
// Copyright (c) 2020 Jonathan R. Madsen
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ---------------------------------------------------------------
//  Tasking class implementation
//
// Threading.cc
//

#include "PTL/Threading.hh"
#include "PTL/AutoLock.hh"
#include "PTL/Globals.hh"
#include "PTL/Types.hh"

#if defined(PTL_WINDOWS)
#    include <Windows.h>
#elif defined(PTL_UNIX)
#    include <sys/syscall.h>
#    include <sys/types.h>
#    include <unistd.h>
#endif

#if defined(PTL_LINUX)
#    include <fstream>
#endif

#include <atomic>

using namespace PTL;

//======================================================================================//

namespace
{
thread_local int ThreadID = Threading::MASTER_ID;

}  // namespace

//======================================================================================//

Pid_t
Threading::GetPidId()
{
    // In multithreaded mode return Thread ID
    return std::this_thread::get_id();
}

//======================================================================================//

unsigned
Threading::GetNumberOfCores()
{
    return std::thread::hardware_concurrency();
}

//======================================================================================//

unsigned
Threading::GetNumberOfPhysicalCpus()
{
#if defined(PTL_MACOS)
    int    count;
    size_t count_len = sizeof(count);
    sysctlbyname("hw.physicalcpu", &count, &count_len, nullptr, 0);
    return static_cast<unsigned>(count);
#elif defined(PTL_LINUX)
    unsigned      core_id_count = 0;
    std::ifstream ifs("/proc/cpuinfo");
    if(ifs)
    {
        std::set<std::string> core_ids;
        while(true)
        {
            std::string line = {};
            getline(ifs, line);
            if(!ifs.good())
                break;
            if(line.find("core id") != std::string::npos)
            {
                for(std::string&& itr : { "core id", ":", " ", "\t" })
                {
                    static auto _npos = std::string::npos;
                    auto        _pos  = _npos;
                    while((_pos = line.find(itr)) != _npos)
                        line = line.replace(_pos, itr.length(), "");
                }
                core_ids.insert(line);
            }
        }
        core_id_count = static_cast<unsigned>(core_ids.size());
        if(core_id_count > 0)
            return core_id_count;
    }
    return GetNumberOfCores();
#else
    return GetNumberOfCores();
#endif
}

//======================================================================================//

void
Threading::SetThreadId(int value)
{
    ThreadID = value;
}

int
Threading::GetThreadId()
{
    return ThreadID;
}

//======================================================================================//

bool
Threading::SetPinAffinity(int _cpu)
{
#if defined(__linux__) || defined(_AIX)
    cpu_set_t _cpu_set{};
    CPU_ZERO(&_cpu_set);
    if(_cpu >= 0)
        CPU_SET(_cpu, &_cpu_set);
    return (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &_cpu_set) == 0);
#else  // Not available for Mac, WIN,...
    ConsumeParameters(_cpu);
    return true;
#endif
}

//======================================================================================//

bool
Threading::SetThreadPriority(int _prio)
{
#if defined(__linux__) || defined(_AIX)
    return (pthread_setschedprio(pthread_self(), _prio) == 0);
#else  // Not available for Mac, WIN,...
    ConsumeParameters(_prio);
    return true;
#endif
}

//======================================================================================//

bool
Threading::SetPinAffinity(int _cpu, NativeThread& _t)
{
#if defined(__linux__) || defined(_AIX)
    cpu_set_t _cpu_set{};
    CPU_ZERO(&_cpu_set);
    CPU_SET(_cpu, &_cpu_set);
    return (pthread_setaffinity_np(static_cast<pthread_t>(_t), sizeof(cpu_set_t),
                                   &_cpu_set) == 0);
#else  // Not available for Mac, WIN,...
    ConsumeParameters(_cpu, _t);
    return true;
#endif
}

//======================================================================================//

bool
Threading::SetThreadPriority(int _prio, NativeThread& _t)
{
#if defined(__linux__) || defined(_AIX)
    return (pthread_setschedprio(static_cast<pthread_t>(_t), _prio) == 0);
#else
    ConsumeParameters(_prio, _t);
    return true;
#endif
}

//======================================================================================//