When developing ASYNC applications, I often had the need for locking primitives that could be fed inside a pollable device like select(2), poll(2) or epoll(2). Many syncronization primitives are available for Unix systems, but any of them expose a pollable interface to the user. In these cases, if the caller execute a down operation on a blocking semaphore, the application will sleep and whole ASYNC loop will starve because of the wait. To cope with this problem in my ASYNC software, I developed a pipe based semaphore implementation, that I called Pipe-Sem. The implementation is trivially simple and it uses the pipe internal mechanisms to expose a pollable wait interface. Besides the standard semaphore operations, the function psem_down_fd() can be used to retrieve a files descriptor that can be dropped inside a Unix pollable API like select(2), poll(2) or epoll(2). The caller will have to wait for input events (POLLIN), and when receiving the event itself, the caller should invoke the psem_try_down() function to try to acquire the semaphore. Note that receiving an event from the pollable interface, does not mean that we acquired to semaphore, and the function psem_try_down() will have to be called (and tested for zero return code) to correctly complete the down operation.
License and Software
Pipe-Sem is made available through the GNU GPL license. Please read carefully the license before using the software. The Pipe-Sem source is available here :