1 | /* $Id: thread-affinity-linux.cpp 37170 2011-05-20 17:15:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Thread Affinity, Linux ring-3 implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #ifndef _GNU_SOURCE
|
---|
32 | # define _GNU_SOURCE
|
---|
33 | #endif
|
---|
34 | #include <features.h>
|
---|
35 | #if __GLIBC_PREREQ(2,4)
|
---|
36 |
|
---|
37 | #include <sched.h>
|
---|
38 | #include <unistd.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <pthread.h>
|
---|
41 |
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include "internal/iprt.h"
|
---|
44 |
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/cpuset.h>
|
---|
47 | #include <iprt/err.h>
|
---|
48 | #include <iprt/mp.h>
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet)
|
---|
53 | {
|
---|
54 | /* convert */
|
---|
55 | cpu_set_t LnxCpuSet;
|
---|
56 | CPU_ZERO(&LnxCpuSet);
|
---|
57 | if (!pCpuSet)
|
---|
58 | for (unsigned iCpu = 0; iCpu < CPU_SETSIZE; iCpu++)
|
---|
59 | CPU_SET(iCpu, &LnxCpuSet);
|
---|
60 | else
|
---|
61 | for (unsigned iCpu = 0; iCpu < RT_MIN(CPU_SETSIZE, RTCPUSET_MAX_CPUS); iCpu++)
|
---|
62 | if (RTCpuSetIsMemberByIndex(pCpuSet, iCpu))
|
---|
63 | CPU_SET(iCpu, &LnxCpuSet);
|
---|
64 |
|
---|
65 | int rc = pthread_setaffinity_np(pthread_self(), sizeof(LnxCpuSet), &LnxCpuSet);
|
---|
66 | if (!rc)
|
---|
67 | return VINF_SUCCESS;
|
---|
68 | rc = errno;
|
---|
69 | if (rc == ENOENT)
|
---|
70 | return VERR_CPU_NOT_FOUND;
|
---|
71 | return RTErrConvertFromErrno(errno);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet)
|
---|
76 | {
|
---|
77 | cpu_set_t LnxCpuSet;
|
---|
78 | int rc = pthread_getaffinity_np(pthread_self(), sizeof(LnxCpuSet), &LnxCpuSet);
|
---|
79 | if (rc != 0)
|
---|
80 | return RTErrConvertFromErrno(errno);
|
---|
81 |
|
---|
82 | /* convert */
|
---|
83 | RTCpuSetEmpty(pCpuSet);
|
---|
84 | for (unsigned iCpu = 0; iCpu < RT_MIN(CPU_SETSIZE, RTCPUSET_MAX_CPUS); iCpu++)
|
---|
85 | if (CPU_ISSET(iCpu, &LnxCpuSet))
|
---|
86 | RTCpuSetAddByIndex(pCpuSet, iCpu);
|
---|
87 |
|
---|
88 | return VINF_SUCCESS;
|
---|
89 | }
|
---|
90 |
|
---|
91 | #else
|
---|
92 | # include "../../generic/RTThreadGetAffinity-stub-generic.cpp"
|
---|
93 | # include "../../generic/RTThreadSetAffinity-stub-generic.cpp"
|
---|
94 | #endif
|
---|
95 |
|
---|