1 | /* $Id: thread-affinity-solaris.cpp 37154 2011-05-19 12:54:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Thread Affinity, Solaris 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.215389.xyz. 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 | #include <iprt/thread.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/cpuset.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/mp.h>
|
---|
38 |
|
---|
39 | #include <sys/types.h>
|
---|
40 | #include <sys/processor.h>
|
---|
41 | #include <sys/procset.h>
|
---|
42 | #include <unistd.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /* Note! The current implementation can only bind to a single CPU. */
|
---|
46 |
|
---|
47 |
|
---|
48 | RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet)
|
---|
49 | {
|
---|
50 | int rc;
|
---|
51 | if (pCpuSet == NULL)
|
---|
52 | rc = processor_bind(P_LWPID, P_MYID, PBIND_NONE, NULL);
|
---|
53 | else
|
---|
54 | {
|
---|
55 | RTCPUSET PresentSet;
|
---|
56 | int cCpusInSet = RTCpuSetCount(&pCpuSet);
|
---|
57 | if (cCpusInSet == 1)
|
---|
58 | {
|
---|
59 | unsigned iCpu = 0;
|
---|
60 | while ( iCpu < RTCPUSET_MAX_CPUS
|
---|
61 | && !RTCpuSetIsMemberByIndex(pCpuSet, iCpu))
|
---|
62 | iCpu++;
|
---|
63 | rc = processor_bind(P_LWPID, P_MYID, iCpu, NULL);
|
---|
64 | }
|
---|
65 | else if ( cCpusInSet == RTCPUSET_MAX_CPUS
|
---|
66 | || RTCpuSetIsEqual(pCpuSet, RTMpGetPresentSet(&PresentSet)))
|
---|
67 | rc = processor_bind(P_LWPID, P_MYID, PBIND_NONE, NULL);
|
---|
68 | else
|
---|
69 | return VERR_NOT_SUPPORTED;
|
---|
70 | }
|
---|
71 | if (!rc)
|
---|
72 | return VINF_SUCCESS;
|
---|
73 | return RTErrConvertFromErrno(errno);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet)
|
---|
78 | {
|
---|
79 | processorid_t iOldCpu;
|
---|
80 | int rc = processor_bind(P_LWPID, P_MYID, PBIND_QUERY, &iOldCpu);
|
---|
81 | if (rc)
|
---|
82 | return RTErrConvertFromErrno(errno);
|
---|
83 | if (iOldCpu == PBIND_NONE)
|
---|
84 | RTMpGetPresentSet(pCpuSet);
|
---|
85 | else
|
---|
86 | {
|
---|
87 | RTCpuSetEmpty(pCpuSet);
|
---|
88 | if (RTCpuSetAdd(pCpuSet, iOldCpu) != 0)
|
---|
89 | return VERR_INTERNAL_ERROR_5;
|
---|
90 | }
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | }
|
---|
93 |
|
---|