Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX Semaphores on Mac OS X: sem_timedwait alternative

I am trying to port a project (from linux) that uses Semaphores to Mac OS X however some of the posix semaphores are not implemented on Mac OS X

The one that I hit in this port is sem_timedwait()

I don't know much about semaphores but from the man pages sem_wait() seems to be close to sem_timedwait and it is implemented

From the man pages

sem_timedwait() function shall lock the semaphore referenced by
sem as in the sem_wait() function. However, if the semaphore cannot be
locked without waiting for another process or thread to unlock the
semaphore by performing a sem_post() function, this wait shall be ter-
minated when the specified timeout expires

From my limited understanding of how semphores work I can see that sem_timedwait() is safer, but I still should be able to use sem_wait()

Is this correct? If not what other alternatives do I have...

Thanks

like image 394
hhafez Avatar asked Mar 13 '09 01:03

hhafez


People also ask

Is POSIX compliant Mac?

Yes. POSIX is a group of standards that determine a portable API for Unix-like operating systems. Mac OS X is Unix-based (and has been certified as such), and in accordance with this is POSIX compliant.

What is POSIX semaphore?

POSIX semaphores allow processes and threads to synchronize their actions. A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)).


2 Answers

It's likely that the timeout is important to the operation of the algorithm. Therefore just using sem_wait() might not work.

You could use sem_trywait(), which returns right away in all cases. You can then loop, and use a sleep interval that you choose, each time decrementing the total timeout until you either run out of timeout or the semaphore is acquired.

A much better solution is to rewrite the algorithm to use a condition variable, and then you can use pthread_cond_timedwait() to get the appropriate timeout.

like image 143
Jared Oberhaus Avatar answered Oct 04 '22 00:10

Jared Oberhaus


Yet another alternative may be to use the sem_timedwait.c implementation by Keith Shortridge of the Australian Astronomical Observatory's software group.

From the source file:

/*
*                       s e m _ t i m e d w a i t
*
*  Function:
*     Implements a version of sem_timedwait().
*
*  Description:
*     Not all systems implement sem_timedwait(), which is a version of
*     sem_wait() with a timeout. Mac OS X is one example, at least up to
*     and including version 10.6 (Leopard). If such a function is needed,
*     this code provides a reasonable implementation, which I think is
*     compatible with the standard version, although possibly less
*     efficient. It works by creating a thread that interrupts a normal
*     sem_wait() call after the specified timeout.
*
* ...
*
*  Limitations:
*
*     The mechanism used involves sending a SIGUSR2 signal to the thread
*     calling sem_timedwait(). The handler for this signal is set to a null
*     routine which does nothing, and with any flags for the signal 
*     (eg SA_RESTART) cleared. Note that this effective disabling of the
*     SIGUSR2 signal is a side-effect of using this routine, and means it
*     may not be a completely transparent plug-in replacement for a
*     'normal' sig_timedwait() call. Since OS X does not declare the
*     sem_timedwait() call in its standard include files, the relevant 
*     declaration (shown above in the man pages extract) will probably have
*     to be added to any code that uses this.
* 
* ...
* 
*  Copyright (c) Australian Astronomical Observatory.
*  Commercial use requires permission.
*  This code comes with absolutely no warranty of any kind.
*/
like image 36
chad Avatar answered Oct 04 '22 01:10

chad