File size: 2,629 Bytes
e26fd16
 
 
 
f86fb3b
 
 
 
 
 
e26fd16
 
 
f86fb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include "../../include/core/thread_utils.hpp"
#include <pthread.h>
#include <unistd.h>
#include <sys/mman.h>
#include <iostream>

#if defined(__linux__)
#include <sched.h>
#elif defined(__APPLE__)
#include <mach/mach.h>
#include <mach/thread_policy.h>
#endif

namespace quant::core {

bool ThreadUtils::pin_current_thread(int core_id) noexcept {
#if defined(__linux__)
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(core_id, &cpuset);
    pthread_t current_thread = pthread_self();
    return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset) == 0;
#elif defined(__APPLE__)
    // On macOS, Darwin does not provide hard core-pinning like Linux pthread_setaffinity_np.
    // Instead, Darwin provides affinity tags via THREAD_AFFINITY_POLICY.
    // Threads sharing an affinity tag will share L2/L3 cache where possible.
    thread_affinity_policy_data_t policy = { core_id };
    mach_port_t mach_thread = pthread_mach_thread_np(pthread_self());
    kern_return_t ret = thread_policy_set(
        mach_thread,
        THREAD_AFFINITY_POLICY,
        reinterpret_cast<thread_policy_t>(&policy),
        THREAD_AFFINITY_POLICY_COUNT
    );
    return ret == KERN_SUCCESS;
#else
    (void)core_id;
    return false;
#endif
}

bool ThreadUtils::set_realtime_priority(int priority) noexcept {
#if defined(__linux__)
    sched_param param{};
    param.sched_priority = priority;
    return pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0;
#elif defined(__APPLE__)
    (void)priority;
    // Mach real-time policy
    mach_port_t mach_thread = pthread_mach_thread_np(pthread_self());
    thread_time_constraint_policy_data_t policy;
    policy.period = 1000000;      // 1ms
    policy.computation = 500000;  // 0.5ms
    policy.constraint = 1000000;  // 1ms
    policy.preemptible = 1;

    kern_return_t ret = thread_policy_set(
        mach_thread,
        THREAD_TIME_CONSTRAINT_POLICY,
        reinterpret_cast<thread_policy_t>(&policy),
        THREAD_TIME_CONSTRAINT_POLICY_COUNT
    );
    return ret == KERN_SUCCESS;
#else
    (void)priority;
    return false;
#endif
}

bool ThreadUtils::set_thread_name(const char* name) noexcept {
#if defined(__linux__)
    return pthread_setname_np(pthread_self(), name) == 0;
#elif defined(__APPLE__)
    return pthread_setname_np(name) == 0;
#else
    (void)name;
    return false;
#endif
}

bool ThreadUtils::lock_all_memory() noexcept {
#if defined(__linux__) || defined(__APPLE__)
    return mlockall(MCL_CURRENT | MCL_FUTURE) == 0;
#else
    return false;
#endif
}

} // namespace quant::core