This is an old revision of the document!


Grand Central Dispatch (GCD) on FreeBSD

This is a status and quick-start page on using Apple's Grand Central Dispatch (GCD) technology on the FreeBSD operating system.

GCD support is experimental on FreeBSD 8.x and 9.x, and requires recent versions of the llvm-devel and compiler-rt ports. The kernel extensions required to support libdispatch are now in the FreeBSD 8-STABLE branch, and will appear in FreeBSD 8.1 and later.

Branch Date Minimum SVN revision
7-STABLE unsupported unsupported
8-STABLE 2009-11-01 r198732
9-CURRENT 2009-09-18 r197293
  1. Install FreeBSD 8.0-RC3.
  2. Perform a source upgraded to FreeBSD 8-STABLE or FreeBSD 9-CURRENT.
  3. Update to the most recently available ports collection.
  4. Install the devel/libdispatch port, which will install llvm-devel and compiler-rt as dependencies.

libdispatch works fine with gcc as shipped with FreeBSD for the function-centric interface dispatch APIs, but to use C Blocks you will need to build with clang -fblocks. Currently, clang does not support C++, so you are limited to using either C++ or blocks in a single source file.

To build applications with libdispatch, add -I/usr/local/include to compiler command lines, and -L/usr/local/lib -ldispatch to linker command lines.

This program schedules deferred execution of a C block that prints a message and exits:

#include <dispatch/dispatch.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
        dispatch_queue_t q;
        dispatch_time_t t;

        q = dispatch_get_main_queue();
        t = dispatch_time(DISPATCH_TIME_NOW, 5LL * NSEC_PER_SEC);

        // Print a message and exit after 5 seconds.
        dispatch_after(t, q, ^{
                printf("block_dispatch\n");
                exit(0);
            });

        dispatch_main();
        return (0);
}

Compile this with:

clang -Wall -Werror -fblocks -L/usr/local/lib -I/usr/local/include -o test test.c -ldispatch

Here's a similar program, performing the same work but using deferred functions rather than C Blocks:

#include <dispatch/dispatch.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>

void
deferred_code(__unused void *arg)
{

	printf("block_dispatch\n");
	exit(0);
}

int
main(int argc, char *argv[])
{
	dispatch_queue_t q;
	dispatch_time_t t;

	q = dispatch_get_main_queue();
	t = dispatch_time(DISPATCH_TIME_NOW, 5LL * NSEC_PER_SEC);

	dispatch_after_f(t, q, NULL, deferred_code);

	dispatch_main();
	return (0);
}

Compile this with:

gcc -Wall -Werror -I/usr/local/include -L/usr/local/lib -o test2 test2.c -ldispatch

libdispatch ships with informative man pages describing not just the API, but also how to structure programs around GCD.

April-September 2009 FreeBSD status report - Grand Central Dispatch port - status on the FreeBSD port of GCD

Mac OS X Forge - open source libdispatch home - libdispatch subversion repository and mailing lists

Grand Central Dispatch: Technology Brief - a white paper from Apple introducing GCD.

Mac OS X Reference Library: Concurrency Programming Guide - a guide to concurrent programming on Mac OS X, much of which also applies to FreeBSD with the GCD port installed.

Mac Dev Center: Grand Central Dispatch Reference - reference material on GCD from Apple.

Mac OS Reference Library: Dispatch_Samples - sample code using GCD from Apple.

  • grandcentraldispatch.1258114479.txt.gz
  • Last modified: 2009/11/13 12:14
  • by RobertWatson