[libdispatch-dev] fastpath() and slowpath() usage
Mark Heily
mark at heily.com
Sat Jul 17 12:18:24 PDT 2010
I've noticed some strange usage of the fastpath() and slowpath() macros in
the libdispatch sources. These are wrappers for the __builtin_expect()
branch prediction macro and defined as:
#define fastpath(x) ((typeof(x))__builtin_expect((long)(x), ~0l))
#define slowpath(x) ((typeof(x))__builtin_expect((long)(x), 0l))
Everything I have read about __builtin_expect() says that it should be used
within a conditional expression, like this:
if (__builtin_expect(foo == NULL, 0)) {
/* fast path */
} else {
/* slow path */
}
There are several cases in libdispatch where the __builtin_expect() macro is
used separately from the conditional. Here's one example from semaphore.c:
dispatch_semaphore_t
dispatch_get_thread_semaphore(void)
{
dispatch_semaphore_t dsema;
dsema = fastpath(_dispatch_thread_getspecific(dispatch_sema4_key));
if (!dsema) {
while (!(dsema = dispatch_semaphore_create(0))) {
sleep(1);
}
}
_dispatch_thread_setspecific(dispatch_sema4_key, NULL);
return dsema;
}
Can anyone explain the reason for using __builtin_expect() in this manner,
and what the practical effect is?
Thanks,
- Mark
More information about the libdispatch-dev
mailing list