I'm trying to port some linux code to OSX, and have
the need for a cross-process timeoutable semaphore (semtimedop in particular
being in the original code). This has been extremely frustrating and I'm
about two weeks into trying to implement this basic mechanism (that took me
about 2 hours to port from win32 to linux originally).
POSIX semaphores won't work because OSX doesn't
support sharing them across processes. Carbon's "multiprocess services"
also, strangely, doesn't work across process boundaries. System V style
semaphores are missing the ability to timeout (on OSX) and try+sleep loops won't
work because of thread priority inversion. I actually think I may be close
to a working implementation using a mutex-locked homegrown semaphore file and
kqueues.....but it seems ridiculously complicated with too much overhead.
I may be missing the boat there anyway if kevent doesn't work exactly like I
think it does. I make one kevent call to start the monitoring, unlock the
mutex, allowing the semaphore file to be written, and then make another kevent
call to actually start waiting.) If that first call isn't actually
starting the monitoring, I could miss a signal on the semaphore file. Not
to mention spurious wakeups to be dealt with and the like...
I've found a couple open source projects that
implement these under OSX, such as Chromium and Firebird, but their
implementations assume a scheduling thread or are very complex and rely
extensively on a whole suite of supporting code.
My last attempt has been using mach
semaphores. My problem there is poor documentation. Even after
studying mach and its various headers for a couple solid days, I just have no
idea how to say to interested processes (which are anonymous, although with
significant work I could probably change that) "here's the global port/semaphore
name...request the access you need." I'm really not sure how well it will
work since only one task/process can have receive rights on a
semaphore/port at the same time anyway.
I haven't looked extensively into Apple Events or
these links, which looks like they *might* be helpful, as I'm a little tired
spending days following dead-ends:
Is this very basic functionality really this
difficult to create, or have I managed to miss a very obvious
solution?
Thanks,
Tyler