1# Vulkan Timeline Semaphores 2 3[Vulkan Timeline 4Semaphores](https://www.khronos.org/blog/vulkan-timeline-semaphores) are a 5synchronization primitive accessible both from the device and the host. A 6timeline semaphore represents a monotonically increasing 64-bit unsigned 7value. Whereas binary Vulkan semaphores are waited on just to become signaled, 8timeline semaphores are waited on to reach a specific value. Once a timeline 9semaphore reaches a certain value, it is considered signaled for every value 10less than or equal to that 11value. [`vkWaitSemaphores`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkWaitSemaphores.html) 12is used to wait for semaphores on the host. It can operate in one of two modes: 13"wait for all" and "wait for any". 14 15In SwiftShader, Vulkan Timeline Semaphores are implemented as an unsigned 64-bit 16integer protected by a mutex with changes signaled by a condition 17variable. Waiting for all timeline semaphores in a set is implemented by simply 18waiting for each of the semaphores in turn. Waiting for any semaphore in a set 19is a bit more complex. 20 21## Wait for any semaphore 22 23A "wait for any" of a set of semaphores is represented by a 24`TimelineSemaphore::WaitForAny` object. Additionally, `TimelineSemaphore` 25contains an internal list of all `WaitForAny` objects that wait for it, as well 26as for which values they wait. When signaled, the timeline semaphore looks 27through this list and, in turn, signals any `WaitForAny` objects that are 28waiting for a value less than or equal to the timeline semaphore's new value. 29 30A `WaitForAny` object is created from a `VkSemaphoreWaitInfo`. During 31construction, it checks the value of each timeline semaphore provided against 32the value for which it is waiting. If it has not yet been reached, the wait 33object registers itself with the timeline semaphore. If it _has_ been reached, 34the wait object is immediately signaled and no further timeline semaphores are 35checked. 36 37Once a `WaitForAny` object is signaled, it remains signaled. There is no way to 38change what semaphores or values to wait for after construction. Any subsequent 39calls to `wait()` will return `VK_SUCCESS` immediately. 40 41When a `WaitForAny` object is destroyed, it unregisters itself from every 42`TimelineSemaphore` it was waiting for. It is expected that the number of 43concurrent waits are few, and that the wait objects are short-lived, so there 44should not be a build-up of wait objects in any timeline semaphore. 45