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

Daniel Blezek Blezek.Daniel at mayo.edu
Fri Feb 18 14:48:34 EST 2011


Hi Sean,

  The link below was very informative.

  But, that leaves us with some unanswered questions, namely, how do we
support semaphore-like functionality properly?

  I fixed the race condition via mutex, and an incrementing variable.

http://review.source.kitware.com/#change,990

-dan


On 2/18/11 12:49 PM, "Sean McBride" <sean at rogue-research.com> wrote:

> 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
> 
> 

-- 
Daniel Blezek, PhD
Medical Imaging Informatics Innovation Center

P 127 or (77) 8 8886
T 507 538 8886
E blezek.daniel at mayo.edu

Mayo Clinic
200 First St. S.W.
Harwick SL-44
Rochester, MN 55905
mayoclinic.org
"It is more complicated than you think." -- RFC 1925



More information about the Insight-developers mailing list