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