[Insight-developers] Mac Semaphores (Was: Failing tests and compile errors after your check in)

Sean McBride sean at rogue-research.com
Fri Feb 18 13:49:02 EST 2011


On Fri, 18 Feb 2011 11:59:07 -0600, Daniel Blezek said:

>POSIX semaphores are not fully supported on the Mac.

Mac OS X complies with POSIX 1003.1 and UNIX03.  The sem* functions are
optional according to that standard.  A pity they didn't implement them. :(

>I don't know why this
>works in the 64-bit version...
>
>We are using sem_destroy to destroy our semaphores.  On the 32-bit Mac, I
>get this return value (perror()):
>
>536: sem_destroy: Function not implemented
>
>Why this linked and run, I'll never know...  Looks like I need to add some
>APPLE specific code.  I suspect this will fix the problem on the Sun.

Apparently the standard at one point required failing stub functions,
which is presumably why it links & runs.

The correct test for the sem functions is:

#if _POSIX_SEMAPHORES && (_POSIX_SEMAPHORES - 200112L) >= 0

See here for more:
<http://lists.apple.com/archives/unix-porting/2009/May/msg00004.html>

An alternative is to use System V semaphores: semget().

>We also have a race-condition in our Semaphore code.  On the Mac, the
>semaphores need unique names.  We generate this using the time() function,
>but it has a one second resolution.  Thus if two semaphores are created
>within 1 second, they will collide.  Not very nice.

mach_absolute_time() gives the best precision, but it's not portable. 
Or maybe use uuid_generate()?

-- 
____________________________________________________________
Sean McBride, B. Eng                 sean at rogue-research.com
Rogue Research                        www.rogue-research.com 
Mac Software Developer              Montréal, Québec, Canada




More information about the Insight-developers mailing list