Not too long ago, Apple open sourced its Grand Central Dispatch library, which aids in developing multithreaded code. It was suggested that it could be ported to other platforms, and the FreeBSD team has done exactly that. They have also done a lot of work related to getting GCD to work in a POSIX environment.
Apart from actually porting the Gand Central Dispatch framework, a number of other things needed to be done do adapt the library for use outside of Mac OS X:
- Added new kqueue primitives required to support GCD, such as EVFILT_USER and EV_TRIGGER
- Created autoconf and automake build framework for libdispatch
- Modified libdispatch to use POSIX semaphores instead of Mach semaphores
- Adapted libdispatch to use portable POSIX time routines
The other important aspect of getting Gand Central Dispatch to work optimally is a blocks-aware compiler. Jordan Hubbard has worked on providing a blocks-aware clang compiler package for FreeBSD. “When compiled with clang, libdispatch provides blocks-based, as well as function-based callbacks.”
You can find the current port in the ports collection, but for the future, it’s going to work out-of-the-box. “After FreeBSD 8.0-RELEASE has shipped, the new kqueue primitives will be MFC’d so that libdispatch works out of the box on FreeBSD 8.1-RELEASE.” They are also investigating any possible benefits it might bring to modify FeeBSD base operating system tools to use GCD.
It’s great to see this happening. When GCD first came out, I thought it was cool, but I feared that this Apple-only thing wouldn’t go very far. Fortunately, Apple have realized that to get wide adoption of GCD, they would have to open-source it.
I’m a Linux user, but this also demonstrates one of the advantages of the BSD projects. Unlike Linux, which is just a kernel, FreeBSD is a whole OS distribution with tighter coupling between kernel and userspace devs. For Linux to get this, some distros devs will have to step up and port it, and I’m sure there will be some challenges getting the right hooks added to the kernel. Despite the FreeBSD folks doing most of the porting work already, I fear that it’ll be a while before this starts to appear in Linux distros.
Still, it’s nice to see this kind of really cool infrastructure being developed and spreading.