File: thread_priority.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (63 lines) | stat: -rw-r--r-- 2,095 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
#include "thread_priority.h"

#include "logger.h"
#ifdef _WIN32
#include <windows.h>
#endif

void setThreadPriority(std::thread &th, thread_priority_t priority)
{
#ifdef _WIN32
    if (SetThreadPriority(th.native_handle(), priority) == 0)
        logger->error("Could not set thread priority!");
#else
    sched_param sch_params;
    int policy = 0;
    pthread_getschedparam(th.native_handle(), &policy, &sch_params);
    sch_params.sched_priority = priority;
    if (pthread_setschedparam(th.native_handle(), SCHED_RR, &sch_params))
        logger->error("Could not set thread priority!");
#endif
}

void setLowestThreadPriority(std::thread &th)
{
#ifdef _WIN32
    if (SetThreadPriority(th.native_handle(), -2) == 0)
        logger->error("Could not set thread priority!");
#elif defined(__APPLE__)
    sched_param sch_params;
    int policy = 0;
    pthread_getschedparam(th.native_handle(), &policy, &sch_params);
    sch_params.sched_priority = PRIORITY_LOWEST;
    if (pthread_setschedparam(th.native_handle(), SCHED_RR, &sch_params))
        logger->error("Could not set thread priority!");
#else
    sched_param sch_params;
    int policy = 0;
    pthread_getschedparam(th.native_handle(), &policy, &sch_params);
    if (pthread_setschedparam(th.native_handle(), SCHED_IDLE, &sch_params))
        logger->error("Could not set thread priority!");
#endif
}

void setLowestThreadPriority()
{
#ifdef _WIN32
    if (SetThreadPriority(GetCurrentThread(), -2) == 0)
        logger->error("Could not set thread priority!");
#elif defined(__APPLE__)
    sched_param sch_params;
    int policy = 0;
    pthread_getschedparam(pthread_self(), &policy, &sch_params);
    sch_params.sched_priority = PRIORITY_LOWEST;
    if (pthread_setschedparam(pthread_self(), SCHED_RR, &sch_params))
        logger->error("Could not set thread priority!");
#else
    sched_param sch_params;
    int policy = 0;
    pthread_getschedparam(pthread_self(), &policy, &sch_params);
    if (pthread_setschedparam(pthread_self(), SCHED_IDLE, &sch_params))
        logger->error("Could not set thread priority!");
#endif
}