PXL
xif_interop.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2026 XCENA Inc.
3 #pragma once
4 
5 #include <cstddef>
6 #include <cstdint>
7 #include <pxl/v3_api_guard.hpp>
8 #include <vector>
9 
10 namespace xif
11 {
12 struct CoordinatorLeaseBinding;
13 }
14 
15 namespace pxl
16 {
17 // xif::CoordinatorBindingAdopter; src/resource/xif_interop.cpp checks they match.
18 using XifBindingAdopter = bool (*)(const std::vector<xif::CoordinatorLeaseBinding>&);
19 } // namespace pxl
20 
21 // SDK/CLI startup bridge, before that caller's first XIF device operation.
22 // The callback must come from the caller's own XIF runtime. Borrowed binding
23 // fds never cross a process boundary; adoptCoordinatorWorkloadBindings copies
24 // them. Normal mode is a no-op. A failed admission must NOT fall back to raw
25 // device access. C symbol + explicit layout version permits optional dlopen by
26 // Python bindings without making standalone XIF depend on PXL at link time.
27 // Normal mode follows PXL's own decision, not XPTI's. On false, errno:
28 // EPROTONOSUPPORT, the binding ABI is not this PXL's; ENOTCONN, no admitted
29 // coordinator workload; EIO, an internal error. A failed binding export keeps
30 // the errno XIF or the adopter set.
31 extern "C" bool pxl_admit_external_xif_v1(
32  uint32_t bindingVersion, size_t bindingSize, pxl::XifBindingAdopter adopter) noexcept;
33 
34 // The SDK installs no XIF headers: only a caller that builds with its own XIF
35 // gets this wrapper, which passes that XIF's layout version and adopter.
36 #if __has_include(<xif/coordinator.hpp>)
37 #include <cerrno>
38 #include <xif/coordinator.hpp>
39 #if __has_include(<xif/sdk_admission.hpp>)
40 #include <xif/sdk_admission.hpp>
41 #endif
42 
43 namespace pxl
44 {
45 namespace xif_interop_detail
46 {
47 // Internal linkage, as in xif/sdk_admission.hpp: a module with its own XIF must
48 // never reach another module's copy of the wrapper or adopter. The adopter runs
49 // on the calling thread (CoordinatorBindingAdopter), so the flag is per thread.
50 static thread_local bool adopted = false;
51 static inline bool adopt(const std::vector<xif::CoordinatorLeaseBinding>& bindings)
52 {
53  const bool done = xif::adoptCoordinatorWorkloadBindings(bindings);
54  adopted = adopted || done;
55  return done;
56 }
57 } // namespace xif_interop_detail
58 
59 // True in normal mode, or once this module's own XIF adopted the bindings.
60 // False (ENOTCONN) when that XIF selects coordinator mode but got none.
61 static inline bool initializeExternalXif()
62 {
63  xif_interop_detail::adopted = false;
64  if (!pxl_admit_external_xif_v1(xif::CoordinatorBindingInteropVersion, sizeof(xif::CoordinatorLeaseBinding),
65  &xif_interop_detail::adopt))
66  {
67  return false;
68  }
69 #if __has_include(<xif/sdk_admission.hpp>)
70  if (!xif_interop_detail::adopted && xif::sdk::sdkCoordinatorSelected())
71  {
72  errno = ENOTCONN;
73  return false;
74  }
75 #endif
76  return true;
77 }
78 } // namespace pxl
79 #endif
Definition: config.hpp:13
bool(*)(const std::vector< xif::CoordinatorLeaseBinding > &) XifBindingAdopter
Definition: xif_interop.hpp:18
bool pxl_admit_external_xif_v1(uint32_t bindingVersion, size_t bindingSize, pxl::XifBindingAdopter adopter) noexcept