File: thread.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (73 lines) | stat: -rw-r--r-- 1,934 bytes parent folder | download
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
#include "thread.hpp"

#include <components/debug/debuglog.hpp>

#include <cstring>
#include <thread>

#ifdef __linux__

#include <pthread.h>
#include <sched.h>

namespace Misc
{
    void setCurrentThreadIdlePriority()
    {
        sched_param param;
        param.sched_priority = 0;
        if (pthread_setschedparam(pthread_self(), SCHED_IDLE, &param) == 0)
            Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id();
        else
            Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": "
                                << std::generic_category().message(errno);
    }
}

#elif defined(WIN32)

#include <components/misc/windows.hpp>

namespace Misc
{
    void setCurrentThreadIdlePriority()
    {
        if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST))
            Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id();
        else
            Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": "
                                << GetLastError();
    }
}

#elif defined(__FreeBSD__)

#include <sys/rtprio.h>
#include <sys/types.h>

namespace Misc
{
    void setCurrentThreadIdlePriority()
    {
        struct rtprio prio;
        prio.type = RTP_PRIO_IDLE;
        prio.prio = RTP_PRIO_MAX;
        if (rtprio_thread(RTP_SET, 0, &prio) == 0)
            Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id();
        else
            Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": "
                                << std::generic_category().message(errno);
    }
}

#else

namespace Misc
{
    void setCurrentThreadIdlePriority()
    {
        Log(Debug::Warning) << "Idle thread priority is not supported on this system";
    }
}

#endif