2

I'm writing a program that uses libev to wait on many file descriptors. When data comes in, a vulkan compute shader gets run to process the data. The completion of that shader is signalled with a fence. I'd rather not block my event loop to wait for completion to return results.

I originally tried getting a file descriptor using VkGetFenceFdKHR, with the VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT set on the fence to get a sync file, but one of the computers that i'm targetting doesn't support that feature.

For the time being, i'm just polling the fences with a timer and vkGetFenceStatus, but that feels really inefficient. is there a better way to wake up my event loop?

Woodrow Douglass
  • 2,605
  • 3
  • 25
  • 41
  • I'm not sure I understand. Waiting on "many file descriptors" doesn't "block my event loop", but somehow waiting on a fence would? Can you just have the thread that waits on file descriptors also wait on the fence (separately from the FDs)? – Nicol Bolas Apr 29 '21 at 13:30
  • I can pass the file descriptors to `select` and wait on all of them at once, whereas there's no way to simultaneously wait on the fence. I'm sorry if i wasn't clear... – Woodrow Douglass Apr 30 '21 at 15:28
  • Ignoring the possibility of a deadlock, wouldn't it be equivalent to just wait on your FDs, then wait on the fence, since you're not supposed to progress until all of them have cleared? – Nicol Bolas Apr 30 '21 at 15:31
  • It's not really equivelant, because the f'd's represent data coming in from various sources (cameras, the network, etc) and that data has to be reacted to quickly. When i wait on the fence, incoming data from other sources isn't being handled – Woodrow Douglass Apr 30 '21 at 15:34
  • I guess I don't understand what `select` is doing. Is your thread waking up when *any* FD is set, or only when *all* of them are set? If it's the former, how do you decide which task to perform? Does `select` return which FD woke it up? – Nicol Bolas Apr 30 '21 at 15:36
  • Yes, that's exactly what select does; given a set of events, it waits until one or more of them has happened, and let's you know which one. – Woodrow Douglass Apr 30 '21 at 15:40

1 Answers1

0

I don't know if this is a better way, as I have no real experience with file-descriptor based synchronization. But here's an alternative.

You can create a thread whose sole purpose is to block on the fence, after which it will signal an FD. That's probably better in some respects than your timer&polling API, as it's not wasting CPU cycles. But it does require an extra thread wake-up before the thread that's waiting on the FD can see it, so that may delay responsiveness.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982