Commit 2aa80df1 by David 'Digit' Turner Committed by David Turner

[vulkan]: Support multiple external semaphore implementations.

This CL refactors the implementation of VkSemaphore objects in the following way: - Add the ability to support several external handle types concurrently. Before this CL, each platform could support a single handle type (e.g. on Linux, VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT only). The changes here will allow future CLs to support more than one type per platform (e.g. the Linux implementation may support OPAQUE_FD_BIT as well as SYNC_FD_BIT at the same time). - Better implementation of temporary imports. In particular, the following sequence now works properly: 1) Create exportable semaphore A. 2) Export A to an external handle/descriptor. 3) Signal A. 4) Temporarily import _another_ handle into A. 5) A.wait() // waits on the temporary payload, then discard it. Before the CL, A would end up, incorrectly, unsignalled. Because the export operation created an External instance that held the payload modified in 3), which was then discarded after the wait() in 5). - Improved and consistent handling of errors during import/export operations, through the use of templates. + Add a technical note in VkSemaphore.h explaining how everything works, since there are several subtle points in the spec. Bug: b/140421736 Change-Id: I9b6935db3238fec7af8e0c81666e2f5c72075756 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/39880Tested-by: 's avatarDavid Turner <digit@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Kokoro-Presubmit: David Turner <digit@google.com> Reviewed-by: 's avatarChris Forbes <chrisforbes@google.com>
parent 440fc995
...@@ -101,8 +101,6 @@ swiftshader_source_set("swiftshader_libvulkan_headers") { ...@@ -101,8 +101,6 @@ swiftshader_source_set("swiftshader_libvulkan_headers") {
] ]
} else if (is_fuchsia) { } else if (is_fuchsia) {
sources += [ "VkSemaphoreExternalFuchsia.hpp" ] sources += [ "VkSemaphoreExternalFuchsia.hpp" ]
} else {
sources += [ "VkSemaphoreExternalNone.hpp" ]
} }
} }
......
...@@ -58,14 +58,99 @@ public: ...@@ -58,14 +58,99 @@ public:
class External; class External;
private: private:
void allocateExternal(); // Small technical note on how semaphores are imported/exported with Vulkan:
void deallocateExternal(); //
// - A Vulkan Semaphore objects has a "payload", corresponding to a
// simple atomic boolean flag.
//
// - A Vulkan Semaphore object can be "exported": this creates a
// platform-specific handle / descriptor (which can be passed to other
// processes), and is linked in some way to the original semaphore's
// payload.
//
// - Similarly, said handle / descriptor can be "imported" into a Vulkan
// Semaphore object. By default, that semaphore loses its payload, and
// instead uses the one referenced / shared through the descriptor.
//
// Hence if semaphore A exports its payload through a descriptor that
// is later imported into semaphore B, then both A and B will use/share
// the same payload (i.e. signal flag), making cross-process
// synchronization possible.
//
// - There are also "temporary imports", where the target semaphore's
// payload is not lost, but is simply hidden/stashed. But the next wait()
// operation on the same semaphore should remove the temporary import,
// and restore the previous payload.
//
// - There are many handle / descriptor types, which are listed through
// the VkExternalSemaphoreHandleTypeFlagBits. A given Vulkan
// implementation might support onle one or several at the same time
// (e.g. on Linux or Android, it could support both OPAQUE_FD_BIT and
// SYNC_FD_BIT, while on Windows, it would be OPAQUE_WIN32_BIT +
// OPAQUE_WIN32_KMT_BIT + D3D12_FENCE_BIT).
//
// - To be able to export a semaphore, VkCreateSemaphore() must be called
// with a VkSemaphoreCreateInfo that lists the types of all possible
// platform-specific handles the semaphore could be exported to
// (e.g. on Linux, it is possible to specify that a semaphore might be
// exported as an opaque FD, or as a Linux Sync FD).
//
// However, which exact type is however only determined later by the
// export operation itself (e.g. vkGetSemaphoreFdKHR() could be called to export
// either a VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT or a
// VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT).
//
// Once a semaphore has been exported as one type, it is not possible
// to export the same payload with a different type (though the spec
// doesn't seem to be explicit about this, it's simply impossible in
// general).
//
// This leads to the following design:
//
// - |internal| is a simple marl::Event that represents the semaphore's
// payload when it is not exported, or imported non-temporarily.
//
// - |external| points to an external semaphore payload. It is created
// on demand if the semaphore is exported or imported non-temporarily.
// Note that once |external| is created, |internal| is ignored.
//
// - |tempExternal| points to a linked-list of temporary external
// semaphore payloads. The list head corresponds to the most recent
// temporary import.
//
// Internal template to allocate a new External implementation.
template<class EXTERNAL>
External *allocateExternal();
void deallocateExternal(External *ext);
// Used internally to import an external payload.
// |temporaryImport| is true iff the import is temporary.
// |alloc_func| is callable that allocates a new External instance of the
// appropriate type.
// |import_func| is callable that takes a single parameter, which
// corresponds to the external handle/descriptor, and returns a VkResult
// values.
template<typename ALLOC_FUNC, typename IMPORT_FUNC>
VkResult importPayload(bool temporaryImport,
ALLOC_FUNC alloc_func,
IMPORT_FUNC import_func);
// Used internally to export a given payload.
// |alloc_func| is a callable that allocates a new External instance of
// the appropriate type.
// |export_func| is a callable that takes a pointer to an External instance,
// and a pointer to a handle/descriptor, and returns a VkResult.
template<typename ALLOC_FUNC, typename EXPORT_FUNC>
VkResult exportPayload(ALLOC_FUNC alloc_func, EXPORT_FUNC export_func);
const VkAllocationCallbacks *allocator = nullptr; const VkAllocationCallbacks *allocator = nullptr;
marl::Event internal; VkExternalSemaphoreHandleTypeFlags exportableHandleTypes = (VkExternalSemaphoreHandleTypeFlags)0;
std::mutex mutex; std::mutex mutex;
marl::Event internal;
External *external = nullptr; External *external = nullptr;
bool temporaryImport = false; External *tempExternal = nullptr;
}; };
static inline Semaphore *Cast(VkSemaphore object) static inline Semaphore *Cast(VkSemaphore object)
......
...@@ -26,38 +26,44 @@ ...@@ -26,38 +26,44 @@
namespace vk { namespace vk {
class Semaphore::External class ZirconEventExternalSemaphore : public Semaphore::External
{ {
public: public:
// The type of external semaphore handle types supported by this implementation. ~ZirconEventExternalSemaphore()
static const VkExternalSemaphoreHandleTypeFlags kExternalSemaphoreHandleType =
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TEMP_ZIRCON_EVENT_BIT_FUCHSIA;
// Default constructor. Note that one should call either init() or
// importFd() before any call to wait() or signal().
External() = default;
~External()
{ {
zx_handle_close(handle); zx_handle_close(handle);
} }
void init() VkResult init(bool initialValue) override
{ {
zx_status_t status = zx_event_create(0, &handle); zx_status_t status = zx_event_create(0, &handle);
if(status != ZX_OK) if(status != ZX_OK)
{ {
ABORT("zx_event_create() returned %d", status); TRACE("zx_event_create() returned %d", status);
return VK_ERROR_INITIALIZATION_FAILED;
}
if(initialValue)
{
status = zx_object_signal(handle, 0, ZX_EVENT_SIGNALED);
if(status != ZX_OK)
{
TRACE("zx_object_signal() returned %d", status);
zx_handle_close(handle);
handle = ZX_HANDLE_INVALID;
return VK_ERROR_INITIALIZATION_FAILED;
}
} }
return VK_SUCCESS;
} }
void importHandle(zx_handle_t new_handle) VkResult importHandle(zx_handle_t new_handle) override
{ {
zx_handle_close(handle); zx_handle_close(handle);
handle = new_handle; handle = new_handle;
return VK_SUCCESS;
} }
VkResult exportHandle(zx_handle_t *pHandle) const VkResult exportHandle(zx_handle_t *pHandle) override
{ {
zx_handle_t new_handle = ZX_HANDLE_INVALID; zx_handle_t new_handle = ZX_HANDLE_INVALID;
zx_status_t status = zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &new_handle); zx_status_t status = zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &new_handle);
...@@ -70,7 +76,7 @@ public: ...@@ -70,7 +76,7 @@ public:
return VK_SUCCESS; return VK_SUCCESS;
} }
void wait() void wait() override
{ {
zx_signals_t observed = 0; zx_signals_t observed = 0;
zx_status_t status = zx_object_wait_one( zx_status_t status = zx_object_wait_one(
...@@ -91,7 +97,7 @@ public: ...@@ -91,7 +97,7 @@ public:
} }
} }
bool tryWait() bool tryWait() override
{ {
zx_signals_t observed = 0; zx_signals_t observed = 0;
zx_status_t status = zx_object_wait_one( zx_status_t status = zx_object_wait_one(
...@@ -113,7 +119,7 @@ public: ...@@ -113,7 +119,7 @@ public:
return true; return true;
} }
void signal() void signal() override
{ {
zx_status_t status = zx_object_signal(handle, 0, ZX_EVENT_SIGNALED); zx_status_t status = zx_object_signal(handle, 0, ZX_EVENT_SIGNALED);
if(status != ZX_OK) if(status != ZX_OK)
......
...@@ -44,7 +44,8 @@ ...@@ -44,7 +44,8 @@
class SharedSemaphore class SharedSemaphore
{ {
public: public:
SharedSemaphore() SharedSemaphore(bool initialValue)
: signaled(initialValue)
{ {
pthread_mutexattr_t mattr; pthread_mutexattr_t mattr;
pthread_mutexattr_init(&mattr); pthread_mutexattr_init(&mattr);
...@@ -129,20 +130,13 @@ private: ...@@ -129,20 +130,13 @@ private:
namespace vk { namespace vk {
class Semaphore::External class OpaqueFdExternalSemaphore : public Semaphore::External
{ {
public: public:
// The type of external semaphore handle types supported by this implementation. ~OpaqueFdExternalSemaphore() { unmapRegion(); }
static const VkExternalSemaphoreHandleTypeFlags kExternalSemaphoreHandleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
// Default constructor. Note that one should call either init() or
// importFd() before any call to wait() or signal().
External() = default;
~External() { close(); }
// Initialize instance by creating a new shared memory region. // Initialize instance by creating a new shared memory region.
void init() VkResult init(bool initialState) override
{ {
// Allocate or import the region's file descriptor. // Allocate or import the region's file descriptor.
const size_t size = sw::memoryPageSize(); const size_t size = sw::memoryPageSize();
...@@ -153,24 +147,30 @@ public: ...@@ -153,24 +147,30 @@ public:
snprintf(name, sizeof(name), "SwiftShader.Semaphore.%d", ++counter); snprintf(name, sizeof(name), "SwiftShader.Semaphore.%d", ++counter);
if(!memfd.allocate(name, size)) if(!memfd.allocate(name, size))
{ {
ABORT("memfd.allocate() returned %s", strerror(errno)); TRACE("memfd.allocate() returned %s", strerror(errno));
return VK_ERROR_INITIALIZATION_FAILED;
} }
mapRegion(size, true); if(!mapRegion(size, true, initialState))
return VK_ERROR_INITIALIZATION_FAILED;
return VK_SUCCESS;
} }
// Import an existing semaphore through its file descriptor. // Import an existing semaphore through its file descriptor.
VkResult importFd(int fd) VkResult importOpaqueFd(int fd) override
{ {
close(); unmapRegion();
memfd.importFd(fd); memfd.importFd(fd);
mapRegion(sw::memoryPageSize(), false); if(!mapRegion(sw::memoryPageSize(), false, false))
return VK_ERROR_INITIALIZATION_FAILED;
return VK_SUCCESS; return VK_SUCCESS;
} }
// Export the current semaphore as a duplicated file descriptor to the same // Export the current semaphore as a duplicated file descriptor to the same
// region. This can be consumed by importFd() running in a different // region. This can be consumed by importFd() running in a different
// process. // process.
VkResult exportFd(int *pFd) const VkResult exportOpaqueFd(int *pFd) override
{ {
int fd = memfd.exportFd(); int fd = memfd.exportFd();
if(fd < 0) if(fd < 0)
...@@ -181,24 +181,23 @@ public: ...@@ -181,24 +181,23 @@ public:
return VK_SUCCESS; return VK_SUCCESS;
} }
void wait() void wait() override
{ {
semaphore->wait(); semaphore->wait();
} }
bool tryWait() bool tryWait() override
{ {
return semaphore->tryWait(); return semaphore->tryWait();
} }
void signal() void signal() override
{ {
semaphore->signal(); semaphore->signal();
} }
private: private:
// Unmap the semaphore if needed and close its file descriptor. void unmapRegion()
void close()
{ {
if(semaphore) if(semaphore)
{ {
...@@ -213,23 +212,25 @@ private: ...@@ -213,23 +212,25 @@ private:
} }
// Remap the shared region and setup the semaphore or increment its reference count. // Remap the shared region and setup the semaphore or increment its reference count.
void mapRegion(size_t size, bool needInitialization) bool mapRegion(size_t size, bool needsInitialization, bool initialValue)
{ {
// Map the region into memory and point the semaphore to it. // Map the region into memory and point the semaphore to it.
void *addr = memfd.mapReadWrite(0, size); void *addr = memfd.mapReadWrite(0, size);
if(!addr) if(!addr)
{ {
ABORT("mmap() failed: %s", strerror(errno)); TRACE("mmap() failed: %s", strerror(errno));
return false;
} }
semaphore = reinterpret_cast<SharedSemaphore *>(addr); semaphore = reinterpret_cast<SharedSemaphore *>(addr);
if(needInitialization) if(needsInitialization)
{ {
new(semaphore) SharedSemaphore(); new(semaphore) SharedSemaphore(initialValue);
} }
else else
{ {
semaphore->addRef(); semaphore->addRef();
} }
return true;
} }
LinuxMemFd memfd; LinuxMemFd memfd;
......
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VK_SEMAPHORE_EXTERNAL_NONE_H_
#define VK_SEMAPHORE_EXTERNAL_NONE_H_
namespace vk {
// Empty external sempahore implementation.
class Semaphore::External
{
public:
// The type of external semaphore handle types supported by this implementation.
static const VkExternalSemaphoreHandleTypeFlags kExternalSemaphoreHandleType = 0;
void init() {}
void wait() {}
bool tryWait() { return true; }
void signal() {}
private:
int dummy;
};
} // namespace vk
#endif // VK_SEMAPHORE_EXTERNAL_NONE_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment