1 | /* $Id: RTMpOnPair-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTMpOnPair, generic implementation using RTMpOnAll.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/mp.h>
|
---|
43 |
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/err.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | /**
|
---|
53 | * Argument package for the generic RTMpOnPair implemenetation.
|
---|
54 | */
|
---|
55 | typedef struct RTMPONPAIRGENERIC
|
---|
56 | {
|
---|
57 | RTCPUID idCpu1;
|
---|
58 | RTCPUID idCpu2;
|
---|
59 | PFNRTMPWORKER pfnWorker;
|
---|
60 | void *pvUser1;
|
---|
61 | void *pvUser2;
|
---|
62 | /** Count of how many CPUs actually showed up. */
|
---|
63 | uint32_t volatile cPresent;
|
---|
64 | } RTMPONPAIRGENERIC;
|
---|
65 | /** Pointer to the an argument package for the generic RTMpOnPair
|
---|
66 | * implemenation. */
|
---|
67 | typedef RTMPONPAIRGENERIC *PRTMPONPAIRGENERIC;
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * @callback_method_impl{FNRTMPWORKER,
|
---|
72 | * Used by RTMpOnPair to call the worker on the two specified CPUs.}
|
---|
73 | */
|
---|
74 | static DECLCALLBACK(void) rtMpOnPairGenericWorker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
|
---|
75 | {
|
---|
76 | RT_NOREF(pvUser2);
|
---|
77 | PRTMPONPAIRGENERIC pArgs = (PRTMPONPAIRGENERIC)pvUser1;
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Only the two choosen CPUs should call the worker function, count how
|
---|
81 | * many of them that showed up.
|
---|
82 | */
|
---|
83 | if ( idCpu == pArgs->idCpu1
|
---|
84 | || idCpu == pArgs->idCpu2)
|
---|
85 | {
|
---|
86 | ASMAtomicIncU32(&pArgs->cPresent);
|
---|
87 | pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(int) RTMpOnPair(RTCPUID idCpu1, RTCPUID idCpu2, uint32_t fFlags, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
|
---|
93 | {
|
---|
94 | int rc;
|
---|
95 | AssertReturn(idCpu1 != idCpu2, VERR_INVALID_PARAMETER);
|
---|
96 | AssertReturn(!(fFlags & RTMPON_F_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
97 | if ((fFlags & RTMPON_F_CONCURRENT_EXEC) && !RTMpOnAllIsConcurrentSafe())
|
---|
98 | return VERR_NOT_SUPPORTED;
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Check that both CPUs are online before doing the broadcast call.
|
---|
102 | */
|
---|
103 | if ( RTMpIsCpuOnline(idCpu1)
|
---|
104 | && RTMpIsCpuOnline(idCpu2))
|
---|
105 | {
|
---|
106 | RTMPONPAIRGENERIC Args;
|
---|
107 | Args.idCpu1 = idCpu1;
|
---|
108 | Args.idCpu2 = idCpu2;
|
---|
109 | Args.pfnWorker = pfnWorker;
|
---|
110 | Args.pvUser1 = pvUser1;
|
---|
111 | Args.pvUser2 = pvUser2;
|
---|
112 | Args.cPresent = 0;
|
---|
113 | rc = RTMpOnAll(rtMpOnPairGenericWorker, &Args, pvUser2);
|
---|
114 | if (RT_SUCCESS(rc))
|
---|
115 | {
|
---|
116 | /*
|
---|
117 | * Let's see if both of the CPUs showed up.
|
---|
118 | */
|
---|
119 | if (RT_LIKELY(Args.cPresent == 2))
|
---|
120 | { /* likely */ }
|
---|
121 | else if (Args.cPresent == 0)
|
---|
122 | rc = VERR_CPU_OFFLINE;
|
---|
123 | else if (Args.cPresent == 1)
|
---|
124 | rc = VERR_NOT_ALL_CPUS_SHOWED;
|
---|
125 | else
|
---|
126 | {
|
---|
127 | rc = VERR_CPU_IPE_1;
|
---|
128 | AssertMsgFailed(("cPresent=%#x\n", Args.cPresent));
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | /*
|
---|
133 | * A CPU must be present to be considered just offline.
|
---|
134 | */
|
---|
135 | else if ( RTMpIsCpuPresent(idCpu1)
|
---|
136 | && RTMpIsCpuPresent(idCpu2))
|
---|
137 | rc = VERR_CPU_OFFLINE;
|
---|
138 | else
|
---|
139 | rc = VERR_CPU_NOT_FOUND;
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(bool) RTMpOnPairIsConcurrentExecSupported(void)
|
---|
145 | {
|
---|
146 | return RTMpOnAllIsConcurrentSafe();
|
---|
147 | }
|
---|
148 |
|
---|