Flow 1.0.0
Flow project: Full implementation reference.
async.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19
21#include "flow/common.hpp"
22#include <limits>
23#ifdef FLOW_OS_LINUX
24# include <sched.h>
25#endif
26#ifdef FLOW_OS_MAC
27# include <cpuid.h>
28#endif
29
30namespace flow::async
31{
32
33uint16_t cpu_idx()
34{
35 using std::numeric_limits;
36 using ret_int_t = uint16_t;
37
38#ifdef FLOW_OS_LINUX
39 using ::sched_getcpu;
40
41 return ret_int_t(sched_getcpu());
42#elif defined(FLOW_OS_MAC)
43 /* Adapted from: https://stackoverflow.com/questions/33745364/sched-getcpu-equivalent-for-os-x
44 * Their comments omitted, since they wouldn't be to the desired level of quality anyway. @todo Comment well. */
45 uint32_t cpu_info[4];
46 __cpuid_count(1, 0, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]); // It's a macro. It loads cpu_info[]s.
47 if ((cpu_info[3] & (1 << 9)) == 0)
48 {
49 return 0;
50 }
51 // else
52 const auto idx = cpu_info[1] >> 24;
53 assert(idx <= numeric_limits<ret_int_t>::max());
54 return ret_int_t(idx);
55#else
56# error "cpu_idx() implementation is for Linux or Mac only. Mac way'd likely work in any x86, but that's untested."
57#endif
58}
59
60} // namespace flow::async
Flow module containing tools enabling multi-threaded event loops operating under the asynchronous-tas...
Definition: async_fwd.hpp:75
uint16_t cpu_idx()
Returns the 0-based processor logical (not hardware) core index of the core executing the calling thr...
Definition: async.cpp:33