In these recent few months, we are busy with porting our project to Mac. Our project claims that it already takes cross platform into consideration as early as it’s in its frist release, because it is using some corss platform libraries(?!), such as ACE, which is famous. But it’s faint!! ACE eventually is not that very corss-platformed when we are fully dependent on ACE’s reactor pattern implementation, ACE_Reactor::register_handler to register event object and its handler for dispatch processing. Frankly speacking, it’s a side effect introduced by ACE_Reactor’s implementation on Windows. See:
#if defined (ACE_WIN32)
/**
* Register handler for OS events.
*
* Register an <event_handler> that will be notified when
* <event_handle> is signaled.
*
* Reactor will call ACE_Event_Handler::add_reference() for a new
* handler/handle pair.
*
* This interface is only available Win32 platforms because
* ACE_HANDLE is an int on non-Win32 platforms and compilers are not
* able to tell the difference between
* register_handler(ACE_Event_Handler*,ACE_Reactor_Mask) and
* register_handler(ACE_Event_Handler*,ACE_HANDLE).
*/
virtual int register_handler (ACE_Event_Handler *event_handler,
ACE_HANDLE event_handle = ACE_INVALID_HANDLE);
#endif /* ACE_WIN32 */
The comment here is lying. The reason is that event dispatching is implemented based on an API – WaitForMultipleObjects, which does not have its equivalent implementation on Linux/Unix/Mac. You would say "there’s select on Linux/Unix/Mac that can be used as a demultiplexer". No, select only supports I/O demulitplex, not event objects. In fact, there’s no event object on Linux/Unix/Mac. If you need it, you have to simulate it with other premitive synchronous objects on Linux/Unix/Mac. Refer to http://www.ibm.com/developerworks/linux/library/l-ipc2lin2.html.
So here comes simulating events on Mac. According to our requirement, we need a named event object, so on Mac, it’s a named semaphore. There’s a good semaphore implementation conforming to POSIX standard (and System V – there’s two semaphore implementation, one for Sys V, one for POSIX) on Linux/Unix. But without Mac. (F**K!!和谐~) Take care!! sem_init, sem_timedwait, sem_getvalue are not implemented on Mac. But, in semaphore.h, you still can see the declerations for sem_init, sem_getvalue, and you can make your code pass compilation and linking, but again, not working. I can’t help shouting "你TM根本就没实现,还声明个P啊!". 仕方がない, why? Because it’s Mac.
Anyway, I overcomed all the above issues, and get my own implementation of Reactor work. Surely, only supports limited functionalities, and is somehow buggy. Take a look at it if you have interest. http://cid-2d6ee2561565d31c.skydrive.live.com/self.aspx/Library/MyReactor.zip
-Austin.D