Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we obtain a file descriptor for a semaphore or condition variable?

I have an implementation of a bi-directional message channel which, to reduce overhead, I implemented as a couple of circular buffers of messages. To write from one end to the other you add the pointer to the message to one circular buffer and tweak the read and write indices for it. To write in the other direction you do the same for the other buffer and so on. The code is small and simple and it avoids the overheads of using a pipe or fifo, although possibly that might have been a better solution in some ways.

I implemented a poll on this by simply checking if there was a message waiting to be read, and if not doing a timed wait on a condition variable that gets signalled when a message is added to the relevant array.

Now I have an application that needs to wait on a socket (more or less) and on the message channel at the same time. I now wish I had used a fifo or pipe, but due to overhead in getting the code changed (long story), it's not really feasible to rewrite it to use a fifo or pipe.

Is there any way to get a file descriptor associated with a conditional variable? If so it is easier to implement a poll on two file descriptors at once, one for the conditional variable and one for the socket.

Out of curiosity, and making this question more useful to others with a similar problem, is it possible to get a file descriptor associated with a semaphore so you could poll on the semaphore and a regular file descriptor at the same time?

like image 286
AlastairG Avatar asked Dec 02 '10 13:12

AlastairG


People also ask

Is semaphore a file descriptor?

Think of a semaphore ID as like a file descriptor in UNIX: in that case, when you open a file, you get back a number (the file descriptor) that denotes that file.

Can condition variables be used with semaphores?

Also notice that with condition variables you can unlock all the waiting threads in the same instant by using the broadcast unlocking. This cannot be done with semaphores.

What is a condition variable mutex?

The associated mutex is used to ensure that a condition can be checked atomically, and that the thread can block on the associated condition variable without missing either a change to the condition or a signal that the condition has changed.

How do I know if a file descriptor is valid?

fcntl(fd, F_GETFD) is the canonical cheapest way to check that fd is a valid open file descriptor. If you need to batch-check a lot, using poll with a zero timeout and the events member set to 0 and checking for POLLNVAL in revents after it returns is more efficient.


1 Answers

Generally, no. But different OSes offer different solutions for the problem you have.

Windows

You can associate an event with a socket with WSAEventSelect and wait with WaitForMultipleObjectsEx for the data on the socket or mutex or semaphore event etc.

Linux

You can use the futex syscall with FUTEX_FD argument (however this has been removed from the kernel), or use eventfd to implement the condition variable.

And you can spawn a second thread that would be waiting on the condition variable, and signal the one waiting in select(). Or ask for signals when input is received on the socket, etc. See this related question.

like image 167
jpalecek Avatar answered Oct 14 '22 08:10

jpalecek